~linaro-infrastructure/lava-dashboard/stable

« back to all changes in this revision

Viewing changes to launch_control/tests/utils_json_package.py

  • Committer: Zygmunt Krynicki
  • Date: 2010-08-11 20:27:53 UTC
  • Revision ID: zygmunt.krynicki@linaro.org-20100811202753-s86n56bjt2jagilm
Add new utils package.

This package contains the brand-new utils.json package with comprehensive json
support. Miscellaneous testing support modules (import_prohibitor and
call_helper) are also relocated to the utils package.

The utils_json module is _not_ removed yet. For review simplicity changes that
remove this module and adapt everything that depended on it are applied in
separate patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Test cases for launch_control.json_utils module
 
3
"""
 
4
 
 
5
from unittest import TestCase
 
6
 
 
7
from launch_control.thirdparty.mocker import (
 
8
        Mocker,
 
9
        MockerTestCase,
 
10
        expect)
 
11
from launch_control.utils.import_prohibitor \
 
12
        import ImportMockingTestCase
 
13
from launch_control.utils.json import (
 
14
        ClassRegistry,
 
15
        IFundamentalJSONType,
 
16
        ISimpleJSONType,
 
17
        IComplexJSONType,
 
18
        json)
 
19
from launch_control.utils.json.encoder import PluggableJSONEncoder
 
20
from launch_control.utils.json.decoder import PluggableJSONDecoder
 
21
 
 
22
 
 
23
__inhibit_protect__ = True
 
24
 
 
25
 
 
26
class IJSONSupportTestCase(ImportMockingTestCase):
 
27
 
 
28
    def test_json_import_failure(self):
 
29
        """ Make sure we import simplejson if json is not available """
 
30
        self.prohibit_importing('json')
 
31
        self.mock_imports()
 
32
        mocker = Mocker()
 
33
        obj = mocker.replace('simplejson')
 
34
        mocker.replay()
 
35
        # Needs explicit reimport after import mocking
 
36
        import launch_control.utils.json as imported_module
 
37
        self.assertTrue(imported_module.json is obj)
 
38
        mocker.verify()
 
39
        mocker.restore()
 
40
 
 
41
 
 
42
class ClassRegistryTestCase(TestCase):
 
43
 
 
44
    def setUp(self):
 
45
        self.registry = ClassRegistry()
 
46
 
 
47
    def test_register(self):
 
48
        class C(IComplexJSONType):
 
49
            @classmethod
 
50
            def get_json_class_name(self):
 
51
                return "class_c"
 
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)
 
55
 
 
56
    def test_register_checks_base_class(self):
 
57
        class C(object):
 
58
            pass
 
59
        self.assertRaises(TypeError, self.registry.register, C)
 
60
 
 
61
 
 
62
class IFundamentalJSONTypeTestCase(TestCase):
 
63
 
 
64
    def test_unimplemented_methods(self):
 
65
        class C(IFundamentalJSONType):
 
66
            pass
 
67
        self.assertRaises(NotImplementedError, C().to_raw_json)
 
68
        self.assertRaises(NotImplementedError, C.from_json, None)
 
69
 
 
70
    def test_register_rejects_fundamental_types(self):
 
71
        self.assertRaises(TypeError, ClassRegistry.register, IFundamentalJSONType)
 
72
 
 
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):
 
78
                return self.raw_data
 
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), "#")
 
83
 
 
84
 
 
85
class ISimpleJSONTypeTestCase(TestCase):
 
86
 
 
87
    def test_unimplemented_methods(self):
 
88
        class C(ISimpleJSONType):
 
89
            pass
 
90
        self.assertRaises(NotImplementedError, C().to_json)
 
91
        self.assertRaises(NotImplementedError, C.from_json, None)
 
92
 
 
93
    def test_register_rejects_simple_types(self):
 
94
        self.assertRaises(TypeError, ClassRegistry.register, ISimpleJSONType)
 
95
 
 
96
    def test_encoding_and_decoding(self):
 
97
        class Enum(ISimpleJSONType):
 
98
            VALUES = [] # need to subclass
 
99
            def __init__(self, value):
 
100
                self.value = value
 
101
            def to_json(self):
 
102
                return self.VALUES[self.value]
 
103
            @classmethod
 
104
            def from_json(cls, json_string):
 
105
                return cls(cls.VALUES.index(json_string))
 
106
        class Weekday(Enum):
 
107
            VALUES = ["Monday", "Tuesday", "Wednesday", "Thursday",
 
108
                    "Friday", "Saturday", "Sunday"]
 
109
        for i in range(len(Weekday.VALUES)):
 
110
            day = Weekday(i)
 
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,
 
114
                    type_expr=Weekday)
 
115
            self.assertEqual(day_loaded.value, day.value)
 
116
 
 
117
 
 
118
class IComplexJSONTypeTestCase(TestCase):
 
119
 
 
120
    def setUp(self):
 
121
        class C(IComplexJSONType):
 
122
            pass
 
123
        self.C = C
 
124
 
 
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)
 
130
 
 
131
 
 
132
class PluggableJSONEncoderTestCase(TestCase):
 
133
 
 
134
    def test_encoder_checks_base_class(self):
 
135
        class C(object):
 
136
            pass
 
137
        self.assertRaises(TypeError, json.dumps, C(),
 
138
                cls=PluggableJSONEncoder)
 
139
 
 
140
 
 
141
class PluggableJSONDecoderTetsCase(TestCase):
 
142
 
 
143
    def setUp(self):
 
144
        self.registry = ClassRegistry()
 
145
 
 
146
    def test_decoder_raises_TypeError_with_unregistered_class(self):
 
147
        self.assertRaises(TypeError, json.loads, '{"__class__": "C"}',
 
148
                cls=PluggableJSONDecoder, registry=self.registry)
 
149
 
 
150