3 from mock import Mock,patch_object,patch
4 from persistence.projects_directory import ProjectsDirectory
5 from file_based_spec import FileSystemBasedBehaviour
6 from model.project import Project,active
7 from persistence.project_file import ProjectFile
8 from persistence import project_file
10 class ProjectsDirectoryBehaviour(FileSystemBasedBehaviour):
13 super(ProjectsDirectoryBehaviour,self).setUp()
14 self.projects = Mock()
15 self.project_list = []
16 self.projects.__iter__ = self.project_list.__iter__
17 self.projects_directory = ProjectsDirectory(self.projects)
18 self.projects_directory.add_directory('.')
20 def create_project_file(self,name,subdir=None):
21 file_name = name+'.prj'
23 filename = os.path.join(subdir,file_name)
24 self.create_file(file_name)
26 def assert_project_added(self,project_name):
27 calls = self.projects.append.call_args_list
28 self.assertTrue(((Project(project_name,active),),{}) in calls,u"Project %s was not created:\n%s"%(repr(Project(project_name)),calls))
29 # self.projects.append.assert_called_with(Project(project_name))
31 def test_should_register_itself_as_observer(self):
32 self.projects.observers.append.assert_called_with(self.projects_directory)
35 self.projects_directory.read()
39 class EmptyProjectsDirectoryBehaviour(ProjectsDirectoryBehaviour):
41 def test_should_not_read_any_projects(self):
43 self.assertFalse(self.projects.append.called)
47 class NonEmptyProjectsDirectoryBehaviour(ProjectsDirectoryBehaviour):
50 super(NonEmptyProjectsDirectoryBehaviour,self).setUp()
51 self.project_names = Set([u'Fürst Project',u'other project',u'third something'])
52 self.create_project_files(self.project_names)
55 def create_project_files(self,names):
57 self.create_project_file(name)
59 def test_should_read_all_projects_in_this_directory(self):
61 for p in self.project_names:
62 self.assert_project_added(p)
64 def test_should_read_only_project_files(self):
65 self.create_file('First something.txt')
67 self.assertEqual(len(self.projects.append.call_args_list),len(self.project_names))
68 self.assertFalse(((Project(u'First something.txt'),),{}) in self.projects.append.call_args_list)
70 def has_project_file(self,p):
71 has_project_file = False
73 if type(o) == ProjectFile:
75 self.fail("Added ProjectFile twice")
76 has_project_file = True
77 return has_project_file
79 def test_should_add_read_projects_to_projects(self):
80 # self.projects.append.side_effect = self.mock_notify
82 read_project_names = Set()
83 for call in self.projects.append.call_args_list:
84 read_project_names.add(call[0][0].name)
85 self.assertEqual(read_project_names,self.project_names)
87 def test_should_clear_the_projects_before_reading(self):
90 self.assertEqual(len(self.projects.pop.call_args_list),len(self.project_names))
92 @patch('persistence.project_file.ProjectFile')
93 def test_should_create_project_files_when_notified_of_added_projects(self,project_file_constructor):
94 p = self.create_and_notify_of_new_project()
95 project_file_constructor.assert_called_with(p)
97 @patch('persistence.project_file.ProjectFile')
98 def test_should_immediately_write_added_project_files(self,project_file_constructor):
99 self.create_and_notify_of_new_project()
100 project_file_constructor().write.assert_called_with()
102 def create_and_notify_of_new_project(self):
104 self.projects_directory.notify(self.projects,'add_item',p, None)
107 @patch('persistence.project_file.read')
108 def test_should_make_the_project_file_read_the_file_contents(self,read_method):
109 read_method.return_value=(None,[],[])
111 self.assertEqual(len(read_method.call_args_list),len(self.project_names))