Don't let View call methods on its superclass (object).
[matthijs/upstream/mobilegtd.git] / tests / specs / persistence / projects_directory_spec.py
1 # coding: utf-8
2 import unittest
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
9 from sets import Set
10 class ProjectsDirectoryBehaviour(FileSystemBasedBehaviour):
11
12     def setUp(self):
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('.')
19
20     def create_project_file(self,name,subdir=None):
21         file_name = name+'.prj'
22         if subdir:
23             filename = os.path.join(subdir,file_name)
24         self.create_file(file_name)
25
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))
30
31     def test_should_register_itself_as_observer(self):
32         self.projects.observers.append.assert_called_with(self.projects_directory)
33
34     def read(self):
35         self.projects_directory.read()
36         
37
38
39 class EmptyProjectsDirectoryBehaviour(ProjectsDirectoryBehaviour):
40
41     def test_should_not_read_any_projects(self):
42         self.read()
43         self.assertFalse(self.projects.append.called)
44
45
46         
47 class NonEmptyProjectsDirectoryBehaviour(ProjectsDirectoryBehaviour):
48
49     def setUp(self):
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)
53
54     
55     def create_project_files(self,names):
56         for name in names:
57             self.create_project_file(name)
58
59     def test_should_read_all_projects_in_this_directory(self):
60         self.read()
61         for p in self.project_names:
62             self.assert_project_added(p)
63
64     def test_should_read_only_project_files(self):
65         self.create_file('First something.txt')
66         self.read()
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)
69
70     def has_project_file(self,p):
71         has_project_file = False
72         for o in p.observers:
73             if type(o) == ProjectFile:
74                 if has_project_file:
75                     self.fail("Added ProjectFile twice")
76                 has_project_file = True
77         return has_project_file
78
79     def test_should_add_read_projects_to_projects(self):
80 #        self.projects.append.side_effect = self.mock_notify
81         self.read()
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)
86
87     def test_should_clear_the_projects_before_reading(self):
88         self.read()
89         self.read()
90         self.assertEqual(len(self.projects.pop.call_args_list),len(self.project_names))
91
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)
96
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()
101
102     def create_and_notify_of_new_project(self):
103         p = Mock()
104         self.projects_directory.notify(self.projects,'add_item',p, None)
105         return p 
106
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,[],[])
110         self.read()
111         self.assertEqual(len(read_method.call_args_list),len(self.project_names))
112