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(helper
.TmpPathTestCase
):
41 """Create a nested model for testing"""
42 helper
.TmpPathTestCase
.setUp(self
)
43 self
.nested
= NestedModel()
44 self
.nested_observer
= ModelObserver(self
.nested
)
45 path
= self
.test_path('test.data')
47 self
.nested
.save(path
)
48 self
.clone
= observable
.ObservableModel
.instance(path
)
50 def test_cloned_class(self
):
51 """Test equality for __class__"""
52 self
.failUnless( str(NestedModel
) ==
53 str(self
.clone
.__class
__) )
55 def test_inner_cloned_class(self
):
56 """Test an inner clone's __class__"""
57 self
.failUnless( str(InnerModel
) ==
58 str(self
.clone
.inner
.__class
__) )
60 def test_cloned_list_item(self
):
61 """Test a list item's __class__"""
62 self
.failUnless( str(self
.clone
.inner
.__class
__) ==
63 str(self
.clone
.innerlist
[0].__class
__) )
65 def test_list_2deep(self
):
66 """Test a list-inside-a-list"""
67 self
.failUnless( str(InnerModel
) ==
68 str(self
.clone
.innerlist
[1][0].__class
__) )
70 def test_list_3deep(self
):
71 """Test a 3-deep nested list"""
72 self
.failUnless( str(InnerModel
) ==
73 str(self
.clone
.innerlist
[2][0][0].__class
__))
75 def test_list_4deep(self
):
76 """Test a 4-deep nested list"""
77 self
.failUnless( str(InnerModel
) ==
78 str(self
.clone
.innerlist
[3][0][0][0].__class
__))
80 def test_dict_in_list(self
):
81 """Test a dict inside a list"""
82 self
.failUnless( str(dict) ==
83 str(self
.clone
.innerlist
[4].__class
__))
85 def test_obj_in_dict_in_list(self
):
86 """Test an object instance inside a dict inside a list"""
87 self
.failUnless( str(InnerModel
) ==
88 str(self
.clone
.innerlist
[-1]["foo"].__class
__))
90 def test_clone_of_clone(self
):
91 """Test cloning a reconstructed object with an attached observer."""
92 clone
= self
.clone
.clone()
93 self
.assertTrue(len(clone
.observers
) == 0)
94 self
.assertTrue(clone
.notification_enabled
)
95 self
.assertEqual(clone
.__class
__, self
.clone
.__class
__)
97 if __name__
== '__main__':