8c8f41458356bbff0fd1007825cf762abcd101c5
[matthijs/upstream/mobilegtd.git] / tests / specs / model / projects_spec.py
1 import unittest
2 from model.projects import Projects
3 from mock import Mock
4
5 class ProjectsBehaviour(unittest.TestCase):
6     def setUp(self):
7         self.projects = Projects()
8         self.observer = Mock()
9         self.projects.observers.append(self.observer)
10         
11     def create_project(self,name=None,status=None):
12         p = Mock()
13         if name != None:
14             p.name = name
15         if status != None:
16             p.status = status
17         return p
18     
19     def assert_observed(self,attribute,new=None,old=None):
20         self.observer.notify.assert_called_with(self.projects,attribute,new=new,old=old)
21     
22     def test_should_notify_observers_when_project_is_added(self):
23         p = self.create_project()
24         self.projects.append(p)
25         self.assert_observed('add_item',new=p,old=None)
26         
27
28 class EmptyProjectsBehaviour(ProjectsBehaviour):
29     def test_should_not_contain_any_projects(self):
30         self.assertEqual(self.projects,[])
31
32 class NonEmptyProjectsBehaviour(ProjectsBehaviour):
33
34     def setUp(self):
35         super(NonEmptyProjectsBehaviour,self).setUp()
36         self.searched_projects = [self.create_project(status=0),self.create_project(status=0)]
37         for p in self.searched_projects:
38             self.projects.append(p)
39         self.not_searched_projects = [self.create_project(status=1),self.create_project(status=2)]
40         for p in self.not_searched_projects:
41             self.projects.append(p)
42     
43     def test_should_remember_all_added_projects(self):
44         for p in self.searched_projects+self.not_searched_projects:
45             self.assertTrue(p in self.projects)
46
47     def test_should_be_able_to_filter_projects_by_status(self):
48         self.assertEqual(self.projects.with_status(0),self.searched_projects)
49
50     def test_should_notify_observers_when_project_is_removed(self):
51         p = self.projects[0]
52         self.projects.remove(p)
53         self.assert_observed('remove_item',new=p,old=None)