2 sys.path.append(os.path.abspath(os.path.join(os.getcwd(),'..','..','..')))
5 import persistence.action_file
6 #from model.model import *
7 #from model.action import *
8 from model import action
12 class ActionFileBasedBehaviour(file_based_spec.FileBasedBehaviour):
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)
21 def action_file(self):
22 self.action_file = persistence.action_file.ActionFile(self.action)
25 return os.path.join(self.action.context,self.action.description+'.act')
27 def test_should_register_as_an_observer_for_the_action(self):
28 self.assertTrue(self.action_file in self.action.observers)
30 # def test_should_overwrite_active_status_with_own_implementation(self):
31 # self.assertEqual(action.active,persistence.action_file.active_status)
33 class ActiveActionFileBehaviour(ActionFileBasedBehaviour):
36 super(ActiveActionFileBehaviour,self).setUp()
37 self.action.status = action.active
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())
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())
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())
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))
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))
62 def test_should_write_if_the_info_changed(self):
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))
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)
74 def generate_test_for_write_on_change_notification(field):
75 def test_should_write_if_notified_of_changes(self):
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
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)
89 class UnprocessedActionFileBehaviour(ActionFileBasedBehaviour):
92 super(UnprocessedActionFileBehaviour,self).setUp()
93 self.action.status = action.unprocessed
95 def test_should_not_have_created_a_file(self):
96 assert not os.path.isfile(self.path())
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())
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())
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())
110 def generate_test_for_not_writing_on_change_notification(field):
111 def test_should_not_write_if_notified_of_changes(self):
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
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)
124 class ActiveDeletedActionFileBehaviour(ActionFileBasedBehaviour):
127 super(ActiveDeletedActionFileBehaviour,self).setUp()
128 self.action.status = action.active
129 # os.remove(self.path())
130 ActionFileBasedBehaviour.action_file(self)
132 def action_file(self):
136 class DoneActionFileBehaviour(UnprocessedActionFileBehaviour):
139 super(DoneActionFileBehaviour,self).setUp()
140 self.action.status = action.done
144 class InactiveActionFileBehaviour(UnprocessedActionFileBehaviour):
146 super(InactiveActionFileBehaviour,self).setUp()
147 self.action.status = action.inactive