2 """Tests model serialization methods"""
7 from PyQt4
import QtCore
9 from cola
.models
.observable
import ObservableModel
10 from cola
.observer
import Observer
13 from helper
import InnerModel
14 from helper
import NestedModel
16 class ModelObserver(Observer
, QtCore
.QObject
):
17 def __init__(self
, model
):
18 Observer
.__init
__(self
, model
)
19 QtCore
.QObject
.__init
__(self
)
21 class TestSaveRestore(unittest
.TestCase
):
23 """Create a nested model for testing"""
25 self
.nested
= NestedModel()
26 self
.nested_observer
= ModelObserver(self
.nested
)
27 path
= os
.path
.join(helper
.get_dir(), 'test.data')
29 self
.nested
.save(path
)
30 self
.clone
= ObservableModel
.instance(path
)
31 self
.clone_observer
= ModelObserver(self
.clone
)
34 """Remove test directories"""
35 testdir
= helper
.get_dir()
36 if os
.path
.exists(testdir
):
39 def test_cloned_class(self
):
40 """Test equality for __class__"""
41 self
.failUnless( str(NestedModel
) ==
42 str(self
.clone
.__class
__) )
44 def test_inner_cloned_class(self
):
45 """Test an inner clone's __class__"""
46 self
.failUnless( str(InnerModel
) ==
47 str(self
.clone
.inner
.__class
__) )
49 def test_cloned_list_item(self
):
50 """Test a list item's __class__"""
51 self
.failUnless( str(self
.clone
.inner
.__class
__) ==
52 str(self
.clone
.innerlist
[0].__class
__) )
54 def test_list_2deep(self
):
55 """Test a list-inside-a-list"""
56 self
.failUnless( str(InnerModel
) ==
57 str(self
.clone
.innerlist
[1][0].__class
__) )
59 def test_list_3deep(self
):
60 """Test a 3-deep nested list"""
61 self
.failUnless( str(InnerModel
) ==
62 str(self
.clone
.innerlist
[2][0][0].__class
__))
64 def test_list_4deep(self
):
65 """Test a 4-deep nested list"""
66 self
.failUnless( str(InnerModel
) ==
67 str(self
.clone
.innerlist
[3][0][0][0].__class
__))
69 def test_dict_in_list(self
):
70 """Test a dict inside a list"""
71 self
.failUnless( str(dict) ==
72 str(self
.clone
.innerlist
[4].__class
__))
74 def test_obj_in_dict_in_list(self
):
75 """Test an object instance inside a dict inside a list"""
76 self
.failUnless( str(InnerModel
) ==
77 str(self
.clone
.innerlist
[-1]["foo"].__class
__))
79 def test_clone_of_clone(self
):
80 """Test cloning a reconstructed object with an attached observer."""
81 clone
= self
.clone
.clone()
82 self
.assertTrue(len(clone
.observers
) == 0)
83 self
.assertTrue(clone
.notification_enabled
)
84 self
.assertEqual(clone
.__class
__, self
.clone
.__class
__)
86 if __name__
== '__main__':