4 from model.filtered_list import FilteredList
6 class FilteredListBehaviour(unittest.TestCase):
9 self.list = FilteredList([])
11 def test_should_return_filtered_lists(self):
12 l = self.list.with_property(lambda x:True)
13 self.assertEqual(type(l),FilteredList)
16 class EmptyFilteredListBehaviour(FilteredListBehaviour):
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,[])
22 class NonEmptyFilteredListBehaviour(FilteredListBehaviour):
25 super(NonEmptyFilteredListBehaviour,self).setUp()
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)
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):
36 item.has_property.return_value = True
37 items_with_property.append(item)
39 items_without_property = []
40 for i in range(0,number_of_items_without_property):
42 item.has_property.return_value = False
43 items_without_property.append(item)
44 items = items_with_property+items_without_property
46 return (items,items_with_property,items_without_property)
48 def filter_results(self):
49 return self.list.with_property(self.property())
51 def test_should_test_the_property_on_its_items(self):
53 for item in self.items:
54 item.has_property.assert_called_with(0)
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)
61 self.assertTrue(item in self.items_with_property)
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)
69 self.assertFalse(item in self.items_without_property)