~hazmat/pyjuju/security-groups

« back to all changes in this revision

Viewing changes to ensemble/lib/tests/test_schema.py

  • Committer: kapil.thangavelu at canonical
  • Date: 2011-08-04 18:19:33 UTC
  • mfrom: (284.1.3 security-connection)
  • Revision ID: kapil.thangavelu@canonical.com-20110804181933-ei5v1dpzpsd1ov5s
Merged security-connection into security-groups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
from ensemble.lib.schema import (
6
6
    SchemaError, SchemaExpectationError, Constant, Bool,
7
7
    Int, Float, String, Unicode, UnicodeOrString, List, KeyDict,
8
 
    Dict, Tuple, OneOf, Any, Regex)
 
8
    SelectDict, Dict, Tuple, OneOf, Any, Regex)
9
9
 
10
10
 
11
11
PATH = ["<pa", "th>"]
16
16
        return "hello!"
17
17
 
18
18
 
19
 
class BasicTypesTest(TestCase):
 
19
class SchemaErrorsTest(TestCase):
20
20
 
21
21
    def test_invalid_schema(self):
22
22
        error = SchemaError(["a", ".", "b"], "error message")
33
33
        self.assertEquals(str(error), "a.b: expected <e>, got <g>")
34
34
 
35
35
 
 
36
class ConstantTest(TestCase):
36
37
 
37
38
    def test_constant(self):
38
39
        self.assertEquals(Constant("hello").coerce("hello", PATH), "hello")
47
48
        self.assertEquals(str(error), "<path>: expected 'foo', got 'bar'")
48
49
 
49
50
 
 
51
class AnyTest(TestCase):
50
52
 
51
53
    def test_any(self):
52
54
        obj = object()
53
55
        self.assertEquals(Any().coerce(obj, object()), obj)
54
56
 
55
57
 
 
58
class OneOfTest(TestCase):
56
59
 
57
60
    def test_one_of(self):
58
61
        schema = OneOf(Constant(None), Unicode())
66
69
        self.assertEquals(str(error), "<path>: expected None, got '<obj>'")
67
70
 
68
71
 
 
72
class BoolTest(TestCase):
69
73
 
70
74
    def test_bool(self):
71
75
        self.assertEquals(Bool().coerce(True, PATH), True)
76
80
        self.assertEquals(str(error), "<path>: expected bool, got 1")
77
81
 
78
82
 
 
83
class IntTest(TestCase):
79
84
 
80
85
    def test_int(self):
81
86
        self.assertEquals(Int().coerce(3, PATH), 3)
92
97
        self.assertEquals(str(error), "<path>: expected int, got 3.0")
93
98
 
94
99
 
 
100
class FloatTest(TestCase):
95
101
 
96
102
    def test_float(self):
97
103
        self.assertEquals(Float().coerce(3.3, PATH), 3.3)
107
113
        self.assertEquals(str(error), "<path>: expected number, got '3.0'")
108
114
 
109
115
 
 
116
class StringTest(TestCase):
110
117
 
111
118
    def test_string(self):
112
119
        self.assertEquals(String().coerce("foo", PATH), "foo")
120
127
        self.assertEquals(str(error), "<path>: expected string, got 1")
121
128
 
122
129
 
 
130
class UnicodeTest(TestCase):
123
131
 
124
132
    def test_unicode(self):
125
133
        self.assertEquals(Unicode().coerce(u"foo", PATH), u"foo")
129
137
        self.assertEquals(str(error), "<path>: expected unicode, got 'foo'")
130
138
 
131
139
 
 
140
class UnicodeOrStringTest(TestCase):
132
141
 
133
142
    def test_unicode_or_str(self):
134
143
        schema = UnicodeOrString("utf-8")
163
172
                          r"got '\xff'")
164
173
 
165
174
 
 
175
class RegexTest(TestCase):
 
176
 
166
177
    def test_regex(self):
167
178
        exp = "\w+"
168
179
        pat = re.compile(exp)
175
186
        self.assertIn("expected regex", str(error))
176
187
 
177
188
 
 
189
class ListTest(TestCase):
 
190
 
178
191
    def test_list(self):
179
192
        schema = List(Int())
180
193
        self.assertEquals(schema.coerce([1], PATH), [1])
202
215
        self.assertEquals(schema.coerce([a, a.encode("utf-8")], PATH), [a, a])
203
216
 
204
217
 
 
218
class TupleTest(TestCase):
205
219
 
206
220
    def test_tuple(self):
207
221
        self.assertEquals(Tuple(Int()).coerce((1,), PATH), (1,))
238
252
                          "<path>: expected tuple with 1 elements, got (1, 2)")
239
253
 
240
254
 
 
255
class DictTest(TestCase):
241
256
 
242
257
    def test_dict(self):
243
258
        self.assertEquals(Dict(Int(), String()).coerce({32: "hello."}, PATH),
284
299
        self.assertEquals(str(error), "1.2: expected int, got 'hi'")
285
300
 
286
301
 
 
302
class KeyDictTest(TestCase):
287
303
 
288
304
    def test_key_dict(self):
289
305
        self.assertEquals(KeyDict({"foo": Int()}).coerce({"foo": 1}, PATH),
370
386
        self.assertEquals(str(error), "1.2: expected int, got 'hi'")
371
387
 
372
388
 
 
389
class SelectDictTest(TestCase):
 
390
 
 
391
    def test_select_dict(self):
 
392
        schema = SelectDict("name", {"foo": KeyDict({"value": Int()}),
 
393
                                     "bar": KeyDict({"value": String()})})
 
394
        self.assertEquals(schema.coerce({"name": "foo", "value": 1}, PATH),
 
395
                          {"name": "foo", "value": 1})
 
396
        self.assertEquals(schema.coerce({"name": "bar", "value": "one"}, PATH),
 
397
                          {"name": "bar", "value": "one"})
 
398
 
 
399
    def test_select_dict_errors(self):
 
400
        schema = SelectDict("name", {"foo": KeyDict({"foo_": Int()}),
 
401
                                     "bar": KeyDict({"bar_": Int()})})
 
402
        error = self.assertRaises(SchemaError,
 
403
                schema.coerce, {"name": "foo"}, PATH)
 
404
        self.assertEquals(str(error), "<path>.foo_: required value not found")
 
405
        error = self.assertRaises(SchemaError,
 
406
                schema.coerce, {"name": "bar"}, PATH)
 
407
        self.assertEquals(str(error), "<path>.bar_: required value not found")
 
408
 
 
409
 
 
410
class BestErrorTest(TestCase):
373
411
 
374
412
    def test_best_error(self):
375
413
        """