1
0

test_json_out.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. """
  3. unittests for json outputter
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.output.json_out as json_out
  9. import salt.utils.stringutils
  10. from salt.ext import six
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import patch
  14. from tests.support.unit import TestCase
  15. class JsonTestCase(TestCase, LoaderModuleMockMixin):
  16. """
  17. Test cases for salt.output.json_out
  18. """
  19. def setup_loader_modules(self):
  20. return {json_out: {}}
  21. def setUp(self):
  22. self.data = {"test": "two", "example": "one"}
  23. self.addCleanup(delattr, self, "data")
  24. def test_default_output(self):
  25. ret = json_out.output(self.data)
  26. self.assertIn('"test": "two"', ret)
  27. self.assertIn('"example": "one"', ret)
  28. def test_pretty_output(self):
  29. with patch.dict(json_out.__opts__, {"output_indent": "pretty"}):
  30. ret = json_out.output(self.data)
  31. self.assertIn('"test": "two"', ret)
  32. self.assertIn('"example": "one"', ret)
  33. def test_indent_output(self):
  34. with patch.dict(json_out.__opts__, {"output_indent": 2}):
  35. ret = json_out.output(self.data)
  36. self.assertIn('"test": "two"', ret)
  37. self.assertIn('"example": "one"', ret)
  38. def test_negative_zero_output(self):
  39. with patch.dict(json_out.__opts__, {"output_indent": 0}):
  40. ret = json_out.output(self.data)
  41. self.assertIn('"test": "two"', ret)
  42. self.assertIn('"example": "one"', ret)
  43. def test_negative_int_output(self):
  44. with patch.dict(json_out.__opts__, {"output_indent": -1}):
  45. ret = json_out.output(self.data)
  46. self.assertIn('"test": "two"', ret)
  47. self.assertIn('"example": "one"', ret)
  48. def test_unicode_output(self):
  49. with patch.dict(json_out.__opts__, {"output_indent": "pretty"}):
  50. decoded = {"test": "Д", "example": "one"}
  51. encoded = {"test": salt.utils.stringutils.to_str("Д"), "example": "one"}
  52. # json.dumps on Python 2 adds a space before a newline while in the
  53. # process of dumping a dictionary.
  54. if six.PY2:
  55. expected = salt.utils.stringutils.to_str(
  56. '{\n "example": "one", \n "test": "Д"\n}'
  57. )
  58. else:
  59. expected = '{\n "example": "one",\n "test": "Д"\n}'
  60. self.assertEqual(json_out.output(decoded), expected)
  61. self.assertEqual(json_out.output(encoded), expected)