-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_primitive_util.py
68 lines (53 loc) · 2.02 KB
/
test_primitive_util.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
import unittest
import primitive_util
class SimpleConversion(unittest.TestCase):
def test(self):
class A(primitive_util.PrimitiveConversion):
def __init__(self):
self.foo = int()
self.bar = str()
a = A()
a.foo = 3
a.bar = 'baz'
prim_a = a.to_primitive_object()
returned_a = A()
returned_a.from_primitive_object(prim_a)
self.assertEquals(returned_a.foo, a.foo)
self.assertEquals(returned_a.bar, a.bar)
class ConvertibleDefaultDictTest(unittest.TestCase):
def test(self):
a = primitive_util.ConvertibleDefaultDict(str)
a['foo'] = 3
prim_a = a.to_primitive_object()
returned_a = primitive_util.ConvertibleDefaultDict(str)
returned_a.from_primitive_object(prim_a)
self.assertEquals(a, returned_a)
def testNestedCDD(self):
CDD = primitive_util.ConvertibleDefaultDict
x = CDD(value_type = lambda: CDD(value_type = int))
x['foo']['bar'] = 2
prim_x = x.to_primitive_object()
self.assertEquals(prim_x, {'foo': {'bar': 2}})
returned_x = CDD(value_type = lambda: CDD(value_type = int))
returned_x.from_primitive_object(prim_x)
self.assertEquals(x, returned_x)
class NestedClassPrimTest(unittest.TestCase):
def test(self):
class B(primitive_util.PrimitiveConversion):
def __init__(self):
self.foo = int()
class A(primitive_util.PrimitiveConversion):
def __init__(self):
self.d = primitive_util.ConvertibleDefaultDict(B)
a = A()
a.d['1'].foo = 1
a.d['2'].foo = 4
prim_a = a.to_primitive_object()
returned_a = A()
returned_a.from_primitive_object(prim_a)
self.assertEquals(a.d['1'].foo, returned_a.d['1'].foo)
again_prim_a = returned_a.to_primitive_object()
self.assertEquals(prim_a, again_prim_a)
if __name__ == '__main__':
unittest.main()