utils_test.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # encoding: utf-8
  2. from __future__ import absolute_import
  3. from __future__ import unicode_literals
  4. from compose import utils
  5. class TestJsonSplitter(object):
  6. def test_json_splitter_no_object(self):
  7. data = '{"foo": "bar'
  8. assert utils.json_splitter(data) is None
  9. def test_json_splitter_with_object(self):
  10. data = '{"foo": "bar"}\n \n{"next": "obj"}'
  11. assert utils.json_splitter(data) == ({'foo': 'bar'}, '{"next": "obj"}')
  12. def test_json_splitter_leading_whitespace(self):
  13. data = '\n \r{"foo": "bar"}\n\n {"next": "obj"}'
  14. assert utils.json_splitter(data) == ({'foo': 'bar'}, '{"next": "obj"}')
  15. class TestStreamAsText(object):
  16. def test_stream_with_non_utf_unicode_character(self):
  17. stream = [b'\xed\xf3\xf3']
  18. output, = utils.stream_as_text(stream)
  19. assert output == '���'
  20. def test_stream_with_utf_character(self):
  21. stream = ['ěĝ'.encode('utf-8')]
  22. output, = utils.stream_as_text(stream)
  23. assert output == 'ěĝ'
  24. class TestJsonStream(object):
  25. def test_with_falsy_entries(self):
  26. stream = [
  27. '{"one": "two"}\n{}\n',
  28. "[1, 2, 3]\n[]\n",
  29. ]
  30. output = list(utils.json_stream(stream))
  31. assert output == [
  32. {'one': 'two'},
  33. {},
  34. [1, 2, 3],
  35. [],
  36. ]
  37. def test_with_leading_whitespace(self):
  38. stream = [
  39. '\n \r\n {"one": "two"}{"x": 1}',
  40. ' {"three": "four"}\t\t{"x": 2}'
  41. ]
  42. output = list(utils.json_stream(stream))
  43. assert output == [
  44. {'one': 'two'},
  45. {'x': 1},
  46. {'three': 'four'},
  47. {'x': 2}
  48. ]
  49. class TestParseBytes(object):
  50. def test_parse_bytes(self):
  51. assert utils.parse_bytes('123kb') == 123 * 1024
  52. assert utils.parse_bytes(123) == 123
  53. assert utils.parse_bytes('foobar') is None
  54. assert utils.parse_bytes('123') == 123
  55. class TestMoreItertools(object):
  56. def test_unique_everseen(self):
  57. unique = utils.unique_everseen
  58. assert list(unique([2, 1, 2, 1])) == [2, 1]
  59. assert list(unique([2, 1, 2, 1], hash)) == [2, 1]
  60. assert list(unique([2, 1, 2, 1], lambda x: 'key_%s' % x)) == [2, 1]