test: Use local classes in the serialization test suite
[git-cola.git] / test / test_cola_model_serialization.py
blobb9eb91cd5566dfa0079180721449033072ee7191
1 #!/usr/bin/env python
2 """Tests model serialization methods"""
3 import os
4 import sys
5 import unittest
7 from PyQt4 import QtCore
9 from cola import observer
10 from cola.models import observable
11 from cola.models import base
13 import helper
16 class InnerModel(base.BaseModel):
17 def __init__(self):
18 base.BaseModel.__init__(self)
19 self.foo = 'bar'
22 class NestedModel(observable.ObservableModel):
23 def __init__(self):
24 observable.ObservableModel.__init__(self)
25 self.inner = InnerModel()
26 self.innerlist = []
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):
40 def setUp(self):
41 """Create a nested model for testing"""
42 helper.create_dir()
43 self.nested = NestedModel()
44 self.nested_observer = ModelObserver(self.nested)
45 path = os.path.join(helper.get_dir(), 'test.data')
46 # save & reconstitute
47 self.nested.save(path)
48 self.clone = observable.ObservableModel.instance(path)
50 def tearDown(self):
51 """Remove test directories"""
52 testdir = helper.get_dir()
53 if os.path.exists(testdir):
54 helper.remove_dir()
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__':
104 unittest.main()