utils_test.py 2.2 KB

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