-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_mergeable.py
80 lines (60 loc) · 1.75 KB
/
test_mergeable.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
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import unittest
import mergeable
class MyMergeableObject(mergeable.MergeableObject):
pass
class MergeableTest(unittest.TestCase):
def testBasic(self):
a = MyMergeableObject()
a.foo = 3
b = MyMergeableObject()
b.foo = 7
a.merge(b)
self.assertEquals(a.foo, 10)
def testCopyMissingAttr(self):
a = MyMergeableObject()
b = MyMergeableObject()
a.foo = 'bar'
b.baz = 'x'
a.merge(b)
self.assertEquals(a.foo, 'bar')
self.assertEquals(a.baz, 'x')
def testNesting(self):
a = MyMergeableObject()
b = MyMergeableObject()
b.c = MyMergeableObject()
b.c.foo = 5
a.c = MyMergeableObject()
a.c.foo = 10
b.only_here = MyMergeableObject()
b.only_here.bar = 20
a.merge(b)
self.assertEquals(a.c.foo, 15)
self.assertEquals(a.only_here.bar, 20)
class MyMergeableDict(dict, mergeable.MergeableDict):
pass
class MergeDictTest(unittest.TestCase):
def testSimple(self):
a = MyMergeableDict()
b = MyMergeableDict()
a['foo'] = 3
b['bar'] = 3
a.merge(b)
self.assertEquals(a['bar'], 3)
class MixedMergeTest(unittest.TestCase):
def testMixed(self):
a = MyMergeableObject()
a.b = MyMergeableDict()
a.b['c'] = 'bar'
a.b['d'] = MyMergeableObject()
a.b['d'].e = 1 # yo dawg
b = MyMergeableObject()
b.b = MyMergeableDict()
b.b['d'] = MyMergeableObject()
b.b['d'].e = 3
b.b['f'] = 'foo'
a.merge(b)
self.assertEquals(a.b['d'].e, 4)
self.assertEquals(a.b['f'], 'foo')
if __name__ == '__main__':
unittest.main()