5 from persistence import project_file
6 from persistence.project_file import ProjectFile
7 from model import project
8 from model import action
10 from model import datetime
11 from persistence.action_file import ActionFile
18 class ProjectFileBehaviour(file_based_spec.FileBasedBehaviour):
21 super(ProjectFileBehaviour,self).setUp()
22 self.project = self.create_project()
23 self.project_file = ProjectFile(self.project)
25 def create_project(self):
27 project.status = self.status()
28 project.name = u'some projectüß'
29 project.actions = self.create_actions()
30 project.observers = []
32 self.info.file_string.return_value = 'important info'
33 project.infos = [self.info]
35 def create_actions(self):
37 self.action1.status = action.active
38 self.action1.project_file_string.return_value = 'first action'
40 self.action2.status = action.inactive
41 self.action2.project_file_string.return_value = 'second action'
42 return [self.action1,self.action2]
47 # def test_should_have_registered_itself_as_observer(self):
48 # self.assertTrue(self.project_file in self.project.observers)
50 def test_should_calc_file_name_as_project_name_plus_extension(self):
51 self.assertEqual(self.project_file.file_name(),self.project.name+'.prj')
54 return self.path_in_subdirectory(self.subdir())
56 def path_in_subdirectory(self,subdir):
57 project_file_name = self.project.name+'.prj'
58 if subdir and len(subdir) > 0:
59 return os.path.join('@Projects',subdir,project_file_name)
61 return os.path.join('@Projects',project_file_name)
63 def test_should_create_an_action_file_if_notified_of_added_action(self):
66 a.status = action.inactive
67 self.project_file.notify(self.project, 'add_action', a, None)
68 self.assertTrue(has_added_action_file_as_observer(a))
71 class ProjectFileWithNonAsciiCharacterName(ProjectFileBehaviour):
72 def create_project(self):
73 project = super(ProjectFileWithNonAsciiCharacterName,self).create_project()
74 project.name = u'some project with ümläutß'
79 class ExistingProjectFileBehaviour:
82 super(ExistingProjectFileBehaviour,self).setUp()
84 def assert_moved_file_to_correct_directory_if_status_changes(self,status,subdir):
86 old_status = self.project.status
87 self.project.status = status
88 self.project_file.notify(self.project, 'status', status,old_status)
89 self.assertTrue(os.path.isfile(io.os_encode(self.path_in_subdirectory(subdir))),"Should have moved file to %s"%self.path_in_subdirectory(subdir))
91 def test_should_move_file_correctly_to_review_directory(self):
92 self.assert_moved_file_to_correct_directory_if_status_changes(project.inactive,'@Review')
94 def test_should_move_file_correctly_to_active_directory(self):
95 self.assert_moved_file_to_correct_directory_if_status_changes(project.active,'')
97 def test_should_move_file_correctly_to_someday_directory(self):
98 self.assert_moved_file_to_correct_directory_if_status_changes(project.someday,'@Someday')
100 def test_should_move_file_correctly_to_tickled_with_date_directory(self):
101 self.assert_moved_file_to_correct_directory_if_status_changes(project.Tickled(datetime.date(2009,12,31)), os.path.join('@Tickled','12 December','31 Thursday'))
102 def test_should_move_file_correctly_to_tickled_with_date_in_another_year_directory(self):
103 self.assert_moved_file_to_correct_directory_if_status_changes(project.Tickled(datetime.date(2012,12,31)), os.path.join('@Tickled','2012','12 December','31 Monday'))
104 def test_should_rename_file_if_project_name_changes(self):
107 self.project_file.notify(self.project, 'name', name)
108 self.project.name = name
109 assert os.path.isfile(self.path())
111 def test_should_calc_path_correctly(self):
112 self.assertEqual(self.project_file.path(),self.path())
114 def test_should_write_if_notified_of_changes(self):
115 self.project_file.notify(self.project, 'add_action', Mock(),None)
116 self.assertCreatedFile(self.path())
121 class WritingProjectFileBehaviour(ExistingProjectFileBehaviour):
122 def test_should_write_the_project_description_in_file(self):
124 self.project_file.write()
125 content = self.file_content()
126 assert len(content) > 0
127 assert self.info.file_string() in content
128 assert self.action1.project_file_string() in content
129 assert self.action2.project_file_string() in content
133 class ActiveProjectFileBehaviour(ProjectFileBehaviour,ExistingProjectFileBehaviour):
136 return project.active
144 class SomedayProjectFileBehaviour(ProjectFileBehaviour,ExistingProjectFileBehaviour):
147 return project.someday
153 class InactiveProjectFileBehaviour(ProjectFileBehaviour,ExistingProjectFileBehaviour):
155 return project.inactive
162 class ProjectFileReaderBehaviour(ProjectFileBehaviour,ExistingProjectFileBehaviour):
165 # super(ProjectFileReaderBehaviour,self).setUp()
166 # self.project.add_action.side_effect = lambda a:self.project_file.notify(self.project, 'add_action', a, None)
168 def create_project(self):
169 self.original_project = self.create_original_project()
170 self.original_project.add_info(info.Info('some info'))
171 active_action = action.Action('active action','Online/Google',status=action.inactive)
172 self.original_project.add_action(active_action)
173 p_file = ProjectFile(self.original_project)
174 self.write(p_file.file_string(),p_file.path())
175 # self.original_project.observers.remove(p_file)
176 p,self.actions,self.infos = project_file.read(p_file.path())
177 for a in self.actions:
183 def create_original_project(self):
184 self.project_name = u'Exämple Project'
185 return project.Project(self.project_name)
187 def create_actions(self):
191 return self.project_file.path()
193 def test_should_read_the_project_name_correctly(self):
194 self.assertEqual(self.project.name,self.project_name)
196 def test_should_infer_the_status_from_the_path(self):
197 self.assertEqual(self.project.status,self.original_project.status)
199 def test_should_read_the_infos_correctly(self):
200 self.assertEqual(self.project.infos,[info.Info('some info')])
202 def test_should_read_the_actions_correctly(self):
203 a = action.Action('active action','Online/Google',status=action.inactive)
204 a.project = self.project
205 self.assertEqual(self.project.actions,[a])
207 # def test_should_create_action_files_for_all_actions(self):
208 # for a in self.project.actions:
209 # self.assertTrue(has_added_action_file_as_observer(a))
211 class DoneProjectFileReaderBehaviour(ProjectFileReaderBehaviour):
212 def create_original_project(self):
213 p = ProjectFileReaderBehaviour.create_original_project(self)
214 p.status = project.done
218 def has_added_action_file_as_observer(a):
219 has_action_file=False
220 for o in a.observers:
221 if type(o) == ActionFile and o.action == a:
223 return has_action_file