utils_test.py 2.1 KB

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