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)
11
11
PATH = ["<pa", "th>"]
19
class BasicTypesTest(TestCase):
19
class SchemaErrorsTest(TestCase):
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>")
36
class ConstantTest(TestCase):
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'")
51
class AnyTest(TestCase):
51
53
def test_any(self):
53
55
self.assertEquals(Any().coerce(obj, object()), obj)
58
class OneOfTest(TestCase):
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>'")
72
class BoolTest(TestCase):
70
74
def test_bool(self):
71
75
self.assertEquals(Bool().coerce(True, PATH), True)
92
97
self.assertEquals(str(error), "<path>: expected int, got 3.0")
100
class FloatTest(TestCase):
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'")
116
class StringTest(TestCase):
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")
130
class UnicodeTest(TestCase):
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'")
140
class UnicodeOrStringTest(TestCase):
133
142
def test_unicode_or_str(self):
134
143
schema = UnicodeOrString("utf-8")
175
186
self.assertIn("expected regex", str(error))
189
class ListTest(TestCase):
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])
218
class TupleTest(TestCase):
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)")
255
class DictTest(TestCase):
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'")
302
class KeyDictTest(TestCase):
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'")
389
class SelectDictTest(TestCase):
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"})
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")
410
class BestErrorTest(TestCase):
374
412
def test_best_error(self):