Child: [695a26] (diff)

Download this file

test_schema.py    56 lines (46 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from datetime import datetime
from decimal import Decimal
from unittest import TestCase, main
import mock
from ming.base import Object, Document, Field, Cursor
from ming import schema as S
from pymongo.bson import ObjectId
class TestSchemaItem(TestCase):
def test_make_array(self):
si_any = S.SchemaItem.make([])
si_int = S.SchemaItem.make([int])
self.assertEqual(S.Array, si_any.__class__)
self.assertEqual(S.Anything, si_any.field_type.__class__)
self.assertEqual(S.Int, si_int.field_type.__class__)
self.assertRaises(ValueError, S.SchemaItem.make, [int, str])
def test_migrate(self):
si = S.Migrate(int, str, str)
self.assertEqual(si.validate(1), '1')
self.assertEqual(si.validate('1'), '1')
si = S.Migrate(
{str:{'a':int}},
[ dict(key=str, a=int) ],
S.Migrate.obj_to_list('key'))
self.assertEqual(si.validate(dict(foo=dict(a=1))),
[ dict(key='foo', a=1) ])
si = S.Migrate(
{str:int},
[ dict(key=str, value=int) ],
S.Migrate.obj_to_list('key', 'value'))
self.assertEqual(si.validate(dict(foo=1)),
[ dict(key='foo', value=1) ])
def test_deprecated(self):
si = S.SchemaItem.make(dict(
a=S.Deprecated(),
b=int))
self.assertEqual(si.validate(dict(a=5, b=6)),
dict(b=6))
def test_fancy(self):
si = S.SchemaItem.make(dict(
a=S.Int(required=True),
b=S.Int(if_missing=5)))
self.assertRaises(S.Invalid, si.validate, dict(b=10))
self.assertEqual(si.validate(dict(a=10)),
dict(a=10, b=5))
if __name__ == '__main__':
main()