7
7
from miro import schema
8
# much easier to type this way..
9
from miro.schema import SchemaString, SchemaInt, SchemaFloat, SchemaBool
10
from miro.schema import SchemaDateTime, SchemaList, SchemaDict, SchemaObject
11
from miro.schema import SchemaReprContainer, ValidationError
8
from miro.schema import (SchemaString, SchemaInt, SchemaFloat, SchemaBool,
9
SchemaDateTime, SchemaList, SchemaDict, SchemaObject,
10
SchemaReprContainer, ValidationError)
12
11
from miro.test.framework import MiroTestCase
14
13
class TestValidation(MiroTestCase):
15
def testModuleVariablesDefined(self):
14
def test_module_variables_defined(self):
16
15
self.assert_(hasattr(schema, 'VERSION'))
17
16
self.assert_(hasattr(schema, 'object_schemas'))
19
def testNoneValidation(self):
18
def test_none_validation(self):
20
19
self.assertRaises(ValidationError, SchemaInt(noneOk=False).validate,
22
21
self.assertRaises(ValidationError, SchemaInt().validate, None)
23
22
SchemaInt(noneOk=True).validate(None)
25
def testBoolValiation(self):
24
def test_bool_validation(self):
26
25
schemabool = SchemaBool()
27
26
self.assertRaises(ValidationError, schemabool.validate, 1)
28
27
self.assertRaises(ValidationError, schemabool.validate, 0)
31
30
schemabool.validate(True)
32
31
schemabool.validate(False)
34
def testDateTimeValiation(self):
33
def test_date_time_validation(self):
35
34
schemadatetime = SchemaDateTime()
36
35
self.assertRaises(ValidationError, schemadatetime.validate, 1)
37
36
self.assertRaises(ValidationError, schemadatetime.validate, 0)
39
38
self.assertRaises(ValidationError, schemadatetime.validate, delta)
40
39
schemadatetime.validate(datetime.datetime(1980, 8, 1))
42
def testIntValiation(self):
41
def test_int_validation(self):
43
42
schemaint = SchemaInt()
44
43
self.assertRaises(ValidationError, schemaint.validate, "One")
45
44
self.assertRaises(ValidationError, schemaint.validate, 1.4)
46
45
schemaint.validate(1)
47
46
schemaint.validate(1L)
49
def testFloatValiation(self):
48
def test_float_validation(self):
50
49
schemafloat = SchemaFloat()
51
50
self.assertRaises(ValidationError, schemafloat.validate, "One half")
52
51
self.assertRaises(ValidationError, schemafloat.validate, 1)
53
52
schemafloat.validate(1.4)
55
def testStringValidation(self):
54
def test_string_validation(self):
56
55
schemastring = SchemaString()
57
56
self.assertRaises(ValidationError, schemastring.validate, 10123)
58
57
self.assertRaises(ValidationError, schemastring.validate, "10123")
59
58
schemastring.validate(u"10123")
61
def testReprContainerValidation(self):
60
def test_repr_container_validatoin(self):
62
61
schemasimple = SchemaReprContainer()
63
62
schemasimple.validate({1: u"Ben", u"pie": 3.1415})
64
63
schemasimple.validate([1, 1, u"two", u"three", 5])
65
64
schemasimple.validate({u'y2k': datetime.datetime(2000, 1, 1),
66
'now': time.localtime()})
65
'now': time.localtime()})
67
66
schemasimple.validate({
68
'fib': (1, 1, u"two", u"three", 5),
69
'square': (1, 4, u"nine", 16),
70
'fact': (1, 2.0, 6, u"twenty-four"),
67
'fib': (1, 1, u"two", u"three", 5),
68
'square': (1, 4, u"nine", 16),
69
'fact': (1, 2.0, 6, u"twenty-four"),
72
#make sure circular references doesn't screw it up
71
# make sure circular references doesn't screw it up
80
79
class TestObject(object):
82
81
self.assertRaises(ValidationError, schemasimple.validate,
84
83
self.assertRaises(ValidationError, schemasimple.validate,
86
85
self.assertRaises(ValidationError, schemasimple.validate,
87
{'object': TestObject()})
86
{'object': TestObject()})
89
def testListValidation(self):
88
def test_list_validation(self):
90
89
schemalist = SchemaList(SchemaInt())
91
90
self.assertRaises(ValidationError, schemalist.validate,
93
92
schemalist.validate([1, 2, 3, 4])
95
def testDictValidation(self):
94
def test_dict_validation(self):
96
95
schemadict = SchemaDict(SchemaInt(), SchemaString())
97
96
self.assertRaises(ValidationError, schemadict.validate,
99
98
schemadict.validate({12: u"Buckle my shoe"})
101
def testObjectValidation(self):
100
def test_object_validation(self):
102
101
class TestObject(object):
104
103
class ChildObject(TestObject):