1
0

utils_test.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. ]