controllers.remote: Warn when creating a new branch
[git-cola.git] / test / test_cola_model_serialization.py
blobf401b8a4331d1b7a0f033e1b52ac22826bfbd6cb
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(helper.TmpPathTestCase):
40 def setUp(self):
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')
46 # save & reconstitute
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__':
98 unittest.main()