utils_test.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. from compose import utils
  4. class TestJsonSplitter(object):
  5. def test_json_splitter_no_object(self):
  6. data = '{"foo": "bar'
  7. assert utils.json_splitter(data) is None
  8. def test_json_splitter_with_object(self):
  9. data = '{"foo": "bar"}\n \n{"next": "obj"}'
  10. assert utils.json_splitter(data) == ({'foo': 'bar'}, '{"next": "obj"}')
  11. class TestStreamAsText(object):
  12. def test_stream_with_non_utf_unicode_character(self):
  13. stream = [b'\xed\xf3\xf3']
  14. output, = utils.stream_as_text(stream)
  15. assert output == '���'
  16. def test_stream_with_utf_character(self):
  17. stream = ['ěĝ'.encode('utf-8')]
  18. output, = utils.stream_as_text(stream)
  19. assert output == 'ěĝ'
  20. class TestJsonStream(object):
  21. def test_with_falsy_entries(self):
  22. stream = [
  23. '{"one": "two"}\n{}\n',
  24. "[1, 2, 3]\n[]\n",
  25. ]
  26. output = list(utils.json_stream(stream))
  27. assert output == [
  28. {'one': 'two'},
  29. {},
  30. [1, 2, 3],
  31. [],
  32. ]