2 """Tests model serialization methods"""
7 from PyQt4
import QtCore
9 from cola
import observer
10 from cola
.models
import observable
11 from cola
.models
import base
16 class InnerModel(base
.BaseModel
):
18 base
.BaseModel
.__init
__(self
)
22 class NestedModel(observable
.ObservableModel
):
24 observable
.ObservableModel
.__init
__(self
)
25 self
.inner
= InnerModel()
27 self
.innerlist
.append(InnerModel())
28 self
.innerlist
.append([InnerModel()])
29 self
.innerlist
.append([[InnerModel()]])
30 self
.innerlist
.append([[[InnerModel(),InnerModel()]]])
31 self
.innerlist
.append({'foo': InnerModel()})
34 class ModelObserver(observer
.Observer
):
35 def __init__(self
, model
):
36 observer
.Observer
.__init
__(self
, model
)
39 class TestSaveRestore(unittest
.TestCase
):
41 """Create a nested model for testing"""
43 self
.nested
= NestedModel()
44 self
.nested_observer
= ModelObserver(self
.nested
)
45 path
= os
.path
.join(helper
.get_dir(), 'test.data')
47 self
.nested
.save(path
)
48 self
.clone
= observable
.ObservableModel
.instance(path
)
51 """Remove test directories"""
52 testdir
= helper
.get_dir()
53 if os
.path
.exists(testdir
):
56 def test_cloned_class(self
):
57 """Test equality for __class__"""
58 self
.failUnless( str(NestedModel
) ==
59 str(self
.clone
.__class
__) )
61 def test_inner_cloned_class(self
):
62 """Test an inner clone's __class__"""
63 self
.failUnless( str(InnerModel
) ==
64 str(self
.clone
.inner
.__class
__) )
66 def test_cloned_list_item(self
):
67 """Test a list item's __class__"""
68 self
.failUnless( str(self
.clone
.inner
.__class
__) ==
69 str(self
.clone
.innerlist
[0].__class
__) )
71 def test_list_2deep(self
):
72 """Test a list-inside-a-list"""
73 self
.failUnless( str(InnerModel
) ==
74 str(self
.clone
.innerlist
[1][0].__class
__) )
76 def test_list_3deep(self
):
77 """Test a 3-deep nested list"""
78 self
.failUnless( str(InnerModel
) ==
79 str(self
.clone
.innerlist
[2][0][0].__class
__))
81 def test_list_4deep(self
):
82 """Test a 4-deep nested list"""
83 self
.failUnless( str(InnerModel
) ==
84 str(self
.clone
.innerlist
[3][0][0][0].__class
__))
86 def test_dict_in_list(self
):
87 """Test a dict inside a list"""
88 self
.failUnless( str(dict) ==
89 str(self
.clone
.innerlist
[4].__class
__))
91 def test_obj_in_dict_in_list(self
):
92 """Test an object instance inside a dict inside a list"""
93 self
.failUnless( str(InnerModel
) ==
94 str(self
.clone
.innerlist
[-1]["foo"].__class
__))
96 def test_clone_of_clone(self
):
97 """Test cloning a reconstructed object with an attached observer."""
98 clone
= self
.clone
.clone()
99 self
.assertTrue(len(clone
.observers
) == 0)
100 self
.assertTrue(clone
.notification_enabled
)
101 self
.assertEqual(clone
.__class
__, self
.clone
.__class
__)
103 if __name__
== '__main__':