2
Test cases for launch_control.json_utils module
5
from unittest import TestCase
7
from launch_control.thirdparty.mocker import (
11
from launch_control.utils.import_prohibitor \
12
import ImportMockingTestCase
13
from launch_control.utils.json import (
19
from launch_control.utils.json.encoder import PluggableJSONEncoder
20
from launch_control.utils.json.decoder import PluggableJSONDecoder
23
__inhibit_protect__ = True
26
class IJSONSupportTestCase(ImportMockingTestCase):
28
def test_json_import_failure(self):
29
""" Make sure we import simplejson if json is not available """
30
self.prohibit_importing('json')
33
obj = mocker.replace('simplejson')
35
# Needs explicit reimport after import mocking
36
import launch_control.utils.json as imported_module
37
self.assertTrue(imported_module.json is obj)
42
class ClassRegistryTestCase(TestCase):
45
self.registry = ClassRegistry()
47
def test_register(self):
48
class C(IComplexJSONType):
50
def get_json_class_name(self):
52
self.registry.register(C)
53
self.assertTrue('class_c' in self.registry.registered_types)
54
self.assertEqual(self.registry.registered_types['class_c'], C)
56
def test_register_checks_base_class(self):
59
self.assertRaises(TypeError, self.registry.register, C)
62
class IFundamentalJSONTypeTestCase(TestCase):
64
def test_unimplemented_methods(self):
65
class C(IFundamentalJSONType):
67
self.assertRaises(NotImplementedError, C().to_raw_json)
68
self.assertRaises(NotImplementedError, C.from_json, None)
70
def test_register_rejects_fundamental_types(self):
71
self.assertRaises(TypeError, ClassRegistry.register, IFundamentalJSONType)
73
def test_encoding(self):
74
class C(IFundamentalJSONType):
75
def __init__(self, raw_data):
76
self.raw_data = raw_data
77
def to_raw_json(self):
79
self.assertEqual(json.dumps(C("foo"), cls=PluggableJSONEncoder), "foo")
80
self.assertEqual(json.dumps(C("15"), cls=PluggableJSONEncoder), "15")
81
self.assertEqual(json.dumps(C("{"), cls=PluggableJSONEncoder), "{")
82
self.assertEqual(json.dumps(C("#"), cls=PluggableJSONEncoder), "#")
85
class ISimpleJSONTypeTestCase(TestCase):
87
def test_unimplemented_methods(self):
88
class C(ISimpleJSONType):
90
self.assertRaises(NotImplementedError, C().to_json)
91
self.assertRaises(NotImplementedError, C.from_json, None)
93
def test_register_rejects_simple_types(self):
94
self.assertRaises(TypeError, ClassRegistry.register, ISimpleJSONType)
96
def test_encoding_and_decoding(self):
97
class Enum(ISimpleJSONType):
98
VALUES = [] # need to subclass
99
def __init__(self, value):
102
return self.VALUES[self.value]
104
def from_json(cls, json_string):
105
return cls(cls.VALUES.index(json_string))
107
VALUES = ["Monday", "Tuesday", "Wednesday", "Thursday",
108
"Friday", "Saturday", "Sunday"]
109
for i in range(len(Weekday.VALUES)):
111
json_text = json.dumps(day, cls=PluggableJSONEncoder)
112
self.assertEqual(json_text, '"' + Weekday.VALUES[i] + '"')
113
day_loaded = json.loads(json_text, cls=PluggableJSONDecoder,
115
self.assertEqual(day_loaded.value, day.value)
118
class IComplexJSONTypeTestCase(TestCase):
121
class C(IComplexJSONType):
125
def test_unimplemented_methods(self):
126
self.assertRaises(NotImplementedError, self.C.get_json_class_name)
127
self.assertRaises(NotImplementedError, self.C().to_json)
128
self.assertRaises(NotImplementedError, self.C().from_json, '')
129
self.assertRaises(NotImplementedError, self.C.get_json_attr_types)
132
class PluggableJSONEncoderTestCase(TestCase):
134
def test_encoder_checks_base_class(self):
137
self.assertRaises(TypeError, json.dumps, C(),
138
cls=PluggableJSONEncoder)
141
class PluggableJSONDecoderTetsCase(TestCase):
144
self.registry = ClassRegistry()
146
def test_decoder_raises_TypeError_with_unregistered_class(self):
147
self.assertRaises(TypeError, json.loads, '{"__class__": "C"}',
148
cls=PluggableJSONDecoder, registry=self.registry)