Assert different outcome under Py3

This commit is contained in:
Pedro Algarvio 2017-02-15 18:55:44 +00:00
parent 90adbe925f
commit 33d8832e40
No known key found for this signature in database
GPG Key ID: BB36BF6584A298FF

View File

@ -5,6 +5,7 @@ unittests for json outputter
# Import Python Libs
from __future__ import absolute_import
import json
# Import Salt Testing Libs
from salttesting import TestCase
@ -13,7 +14,10 @@ from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
from salt.output import json_out as json
from salt.output import json_out
# Import 3rd-party libs
import salt.ext.six as six
class JsonTestCase(TestCase):
@ -21,45 +25,48 @@ class JsonTestCase(TestCase):
Test cases for salt.output.json_out
'''
def setUp(self):
json.__opts__ = {}
json_out.__opts__ = {}
self.data = {'test': 'two', 'example': 'one'}
def test_default_output(self):
ret = json.output(self.data)
ret = json_out.output(self.data)
self.assertIn('"test": "two"', ret)
self.assertIn('"example": "one"', ret)
def test_pretty_output(self):
json.__opts__['output_indent'] = 'pretty'
ret = json.output(self.data)
json_out.__opts__['output_indent'] = 'pretty'
ret = json_out.output(self.data)
self.assertIn('"test": "two"', ret)
self.assertIn('"example": "one"', ret)
def test_indent_output(self):
json.__opts__['output_indent'] = 2
ret = json.output(self.data)
json_out.__opts__['output_indent'] = 2
ret = json_out.output(self.data)
self.assertIn('"test": "two"', ret)
self.assertIn('"example": "one"', ret)
def test_negative_zero_output(self):
json.__opts__['output_indent'] = 0
ret = json.output(self.data)
json_out.__opts__['output_indent'] = 0
ret = json_out.output(self.data)
self.assertIn('"test": "two"', ret)
self.assertIn('"example": "one"', ret)
def test_negative_int_output(self):
json.__opts__['output_indent'] = -1
ret = json.output(self.data)
json_out.__opts__['output_indent'] = -1
ret = json_out.output(self.data)
self.assertIn('"test": "two"', ret)
self.assertIn('"example": "one"', ret)
def test_unicode_output(self):
json.__opts__['output_indent'] = 'pretty'
json_out.__opts__['output_indent'] = 'pretty'
data = {'test': '\xe1', 'example': 'one'}
expect = ('{"message": "\'utf8\' codec can\'t decode byte 0xe1 in position 0: unexpected end of data", '
'"error": "Unable to serialize output to json"}')
ret = json.output(data)
self.assertEqual(expect, ret)
ret = json_out.output(data)
if six.PY2:
self.assertEqual(expect, ret)
else:
self.assertEqual(json.loads(ret), data)
if __name__ == '__main__':