Add files from the old svn, r101.
[matthijs/upstream/mobilegtd.git] / tests / specs / model / filtered_list_spec.py
1 import unittest
2 from mock import Mock
3 import random
4 from model.filtered_list import FilteredList
5
6 class FilteredListBehaviour(unittest.TestCase):
7
8     def setUp(self):
9         self.list = FilteredList([])
10
11     def test_should_return_filtered_lists(self):
12         l = self.list.with_property(lambda x:True)
13         self.assertEqual(type(l),FilteredList)
14         
15
16 class EmptyFilteredListBehaviour(FilteredListBehaviour):
17     
18     def test_should_a_new_empty_filtered_list_on_with(self):
19         l = self.list.with_property(lambda x:True)
20         self.assertEqual(l,[])
21
22 class NonEmptyFilteredListBehaviour(FilteredListBehaviour):
23
24     def setUp(self):
25         super(NonEmptyFilteredListBehaviour,self).setUp()
26         
27         self.items,self.items_with_property,self.items_without_property = self.create_items(random.randint(0, 20) ,random.randint(0, 20))
28         for item in self.items:
29             self.list.append(item)
30     def property(self):
31         return lambda x:x.has_property(0)
32     def create_items(self,number_of_items_with_property=1,number_of_items_without_property=1):
33         items_with_property = []
34         for i in range(0,number_of_items_with_property):
35             item = Mock()
36             item.has_property.return_value = True
37             items_with_property.append(item)
38         
39         items_without_property = []
40         for i in range(0,number_of_items_without_property):
41             item = Mock()
42             item.has_property.return_value = False
43             items_without_property.append(item)
44         items = items_with_property+items_without_property
45         random.shuffle(items)
46         return (items,items_with_property,items_without_property)
47
48     def filter_results(self):
49         return self.list.with_property(self.property())
50
51     def test_should_test_the_property_on_its_items(self):
52         self.filter_results()
53         for item in self.items:
54             item.has_property.assert_called_with(0)
55
56     def test_should_return_all_items_which_fulfill_the_property(self):
57         filtered = self.filter_results()
58         for item in self.items_with_property:
59             self.assertTrue(item in filtered)
60         for item in filtered:
61             self.assertTrue(item in self.items_with_property)
62             
63
64     def test_should_return_none_of_the_items_which_dont_fulfill_the_property(self):
65         filtered = self.filter_results()
66         for item in self.items_without_property:
67             self.assertFalse(item in filtered)
68         for item in filtered:
69             self.assertFalse(item in self.items_without_property)