utils_test.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. class TestStreamAsText(object):
  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('utf-8')]
  19. output, = utils.stream_as_text(stream)
  20. assert output == 'ěĝ'
  21. class TestJsonStream(object):
  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. ]