Add files from the old svn, r101.
[matthijs/upstream/mobilegtd.git] / tests / specs / persistence / action_file_spec.py
1 import sys,os,unittest
2 sys.path.append(os.path.abspath(os.path.join(os.getcwd(),'..','..','..')))
3 #print sys.path
4 import file_based_spec
5 import persistence.action_file
6 #from model.model import *
7 #from model.action import *
8 from model import action
9
10
11
12 class ActionFileBasedBehaviour(file_based_spec.FileBasedBehaviour):
13
14     def setUp(self):
15         super(ActionFileBasedBehaviour,self).setUp()
16         self.context = 'context/sub_context'
17         self.description = 'some action'
18         self.action = action.Action(self.description, self.context)
19         self.action_file()
20
21     def action_file(self):
22         self.action_file = persistence.action_file.ActionFile(self.action)
23
24     def path(self):
25         return os.path.join(self.action.context,self.action.description+'.act')
26
27     def test_should_register_as_an_observer_for_the_action(self):
28         self.assertTrue(self.action_file in self.action.observers)
29         
30 #    def test_should_overwrite_active_status_with_own_implementation(self):
31 #        self.assertEqual(action.active,persistence.action_file.active_status)
32
33 class ActiveActionFileBehaviour(ActionFileBasedBehaviour):
34
35     def setUp(self):
36         super(ActiveActionFileBehaviour,self).setUp()
37         self.action.status = action.active
38
39     def test_should_remove_the_file_when_action_is_set_to_done(self):
40         self.action.status = action.done
41         assert not os.path.isfile(self.path())
42
43     def test_should_remove_the_file_when_action_is_set_to_inactive(self):
44         self.action.status = action.inactive
45         assert not os.path.isfile(self.path())
46
47     def test_should_rename_the_file_when_description_is_changed(self):
48         self.action.description = 'other action'
49         assert os.path.isfile(self.path())
50
51     def test_should_move_the_file_when_context_is_changed(self):
52         old_path = self.path()
53         self.action.context = 'other_context'
54         assert os.path.isfile(self.path())
55         self.assertFalse(os.path.isfile(old_path))
56
57     def test_should_write_the_action_description_in_file(self):
58         content = self.file_content()
59         self.assertTrue(len(content) > 0)
60         self.assertEqual(content, '- %s %s'%(self.context,self.description))
61
62     def test_should_write_if_the_info_changed(self):
63         info = 'new info'
64         self.action.info = info 
65         content = self.file_content()
66         self.assertTrue(len(content) > 0)
67         self.assertEqual(content, '- %s %s (%s)'%(self.context,self.description,info))
68
69     def test_should_set_action_status_to_done_on_update_if_file_does_not_exist(self):
70         self.action_file.remove()
71         self.assertEqual(self.action.status.update(self.action),action.done)
72
73
74 def generate_test_for_write_on_change_notification(field):
75     def test_should_write_if_notified_of_changes(self):
76         d = 'new %s'%field
77         setattr(self.action,field, d)
78         self.action_file.notify(self.action, field, d)
79         self.assertTrue(d in self.file_content())
80     return test_should_write_if_notified_of_changes
81
82 for field in ['description','info','context']:
83     test_name = 'test_should_write_when_notified_of_changed_%s' % field
84     test = generate_test_for_write_on_change_notification(field)
85     setattr(ActiveActionFileBehaviour, test_name, test)
86
87
88
89 class UnprocessedActionFileBehaviour(ActionFileBasedBehaviour):
90
91     def setUp(self):
92         super(UnprocessedActionFileBehaviour,self).setUp()
93         self.action.status = action.unprocessed
94
95     def test_should_not_have_created_a_file(self):
96         assert not os.path.isfile(self.path())          
97        
98     def test_should_create_a_file_when_action_is_set_active(self):
99         self.action.status = action.active
100         assert os.path.isfile(self.path())
101                 
102     def test_should_not_create_the_file_when_description_is_changed(self):
103         self.action.description = 'other action'
104         assert not os.path.isfile(self.path())
105
106     def test_should_move_the_file_when_context_is_changed(self):
107         self.action.context = 'other_context'
108         assert not os.path.isfile(self.path())
109
110 def generate_test_for_not_writing_on_change_notification(field):
111     def test_should_not_write_if_notified_of_changes(self):
112         d = 'new %s'%field
113         setattr(self.action,field, d)
114         self.action_file.notify(self.action, field, d)
115         self.assertFalse(os.path.isfile(self.path()))
116     return test_should_not_write_if_notified_of_changes
117
118 for field in ['description','info','context']:
119     test_name = 'test_should_not_write_when_notified_of_changed_%s' % field
120     test = generate_test_for_not_writing_on_change_notification(field)
121     setattr(UnprocessedActionFileBehaviour, test_name, test)
122
123
124 class ActiveDeletedActionFileBehaviour(ActionFileBasedBehaviour):
125     
126     def setUp(self):
127         super(ActiveDeletedActionFileBehaviour,self).setUp()
128         self.action.status = action.active
129 #        os.remove(self.path())
130         ActionFileBasedBehaviour.action_file(self)
131
132     def action_file(self):
133         pass
134
135
136 class DoneActionFileBehaviour(UnprocessedActionFileBehaviour):
137     
138     def setUp(self):
139         super(DoneActionFileBehaviour,self).setUp()
140         self.action.status = action.done
141
142
143     
144 class InactiveActionFileBehaviour(UnprocessedActionFileBehaviour):
145     def setUp(self):
146         super(InactiveActionFileBehaviour,self).setUp()
147         self.action.status = action.inactive
148