class JsonFormatTest(unittest.TestCase): def setUp(self): from tootwi.formats import JsonFormat self.format = JsonFormat() def test_creation(self): from tootwi.formats import Format self.assertIsInstance(self.format, Format) self.assertEqual(self.format.extension, 'json') def test_decoding_of_none_sample_fails(self): from tootwi.errors import FormatValueIsNotStringError with self.assertRaises(FormatValueIsNotStringError): result = self.format.decode(None) def test_decoding_of_empty_sample(self): sample = ' ' result = self.format.decode(sample) self.assertEqual(result, None) def test_decoding_of_unicode_sample(self): output = u'\u043f\u0440\u0438\u0432\u0435\u0442' # russian 'privet' ('hello') result = self.format.decode('"' + output.encode('utf8') + '"') self.assertEqual(result, output) def test_decoding_of_dict_sample(self): sample = ''' {"a":123, "b":456, "c":[1,2,3], "hello": "world"} ''' result = self.format.decode(sample) self.assertEqual(result, {'a':123, 'b':456, 'c':[1,2,3], 'hello':'world'}) def test_decoding_of_list_sample(self): sample = ''' [1,2,"hello", "world", 1,2] ''' result = self.format.decode(sample) self.assertEqual(result, [1,2,'hello','world',1,2])
def setUp(self): from tootwi.formats import JsonFormat self.format = JsonFormat()