def test_all_method_short_circuit(self): data = [ Person('jake', 'samson', None, 32), Person('sam', 'james', None, 44), Person('sarah', 'smith', None, 41), Person('zoe', 'lee', None, 27), ] global counter counter = 0 linq_data = List(data).all(func_samson) self.assertEqual(linq_data, False) self.assertEqual(counter, 2) # One true, then false data = [ Person('jake', 'samson', None, 32), Person('sam', 'samson', None, 44), Person('sarah', 'samson', None, 41), Person('zoe', 'samson', None, 27), ] counter = 0 linq_data = List(data).all(func_samson) self.assertEqual(linq_data, True) self.assertEqual(counter, 4) # all 4 true
def _get_dynamic_list_with_mocks(self): a, b = Mock(), Mock() a.name = 'bob' a.age = 20 b.name = 'john' b.age = 20 return List(a, b)
def test_list_peoperties_with_unique_object_peoperty(self): a, b = Mock(), Mock() a.foo = [1] b.bar = [2] self.list = List(a, b) self.assertEqual(self.list.foo, [1]) self.assertEqual(self.list.bar, [2])
def test_concat_method(self): m = Mock() m.name = 'Avi' l2 = List(m) concatinated_list = self.list.concat(l2) self.assertEqual(len(concatinated_list), 3) self.assertEqual(concatinated_list[-1], m)
def test_get_list_properties_should_combine_values(self): a, b = Mock(), Mock() a.phones = [1, 2, 123321, 4] b.phones = [1, 2, 5, 789] self.list = List(a, b) self.assertEqual(self.list.phones.sort(), [1, 2, 123321, 4, 1, 2, 5, 789].sort()) self.assertEqual(len(self.list.phones), 8)
def test_different_types_sanity(self): a, b = Mock(), Mock() a.foo = 'a' b.foo = 1 self.list = List(a, b) self.assertTrue('a' in self.list.foo) self.assertTrue(1 in self.list.foo) self.assertTrue(len(self.list.foo) == 2)
def test_any_method_short_circuit(self): data = [ Person('jake', 'samson', None, 32), Person('sam', 'thompson', None, 44), Person('sarah', 'smith', None, 41), Person('zoe', 'lee', None, 27), ] global counter counter = 0 linq_data = List(data).any(func_samson) self.assertEqual(linq_data, True) self.assertEqual(counter, 1) counter = 0 linq_data = List(data).any(func_lee) self.assertEqual(linq_data, True) self.assertEqual(counter, 4)
def test_order_by_complex(self): data = [ Person('jake', 'samson', None, 32), Person('sam', 'thompson', None, 44), Person('sarah', 'smith', None, 41), Person('zoe', 'lee', None, 27), ] sorted_data = sorted(data, key=lambda p: p.age, reverse=True) linq_data = List(data).order_by(lambda p: -p.age) self.assertEqual(sorted_data, linq_data)
def test_runtime_changes_apply(self): john = Person('John', 'Doe', 'Coding', 27) bob = Person('Bob', 'Marley', 'Playing guitar', 33) # Creating a dynamic_list dynamic_list = List(john, bob) # Powerful functionality dynamic_list.first_name # ['John', 'Bob'] dynamic_list.full_name # ['John Doe', 'Bob Marley'] dynamic_list.do_something( ) # ['John Doe is coding', 'Bob Marley is playing guitar'] # Dynamic Runtime changes: bob.birthday = datetime(year=1945, month=2, day=6) dynamic_list.birthday # datetime.datetime(1945, 2, 6, 0, 0) john.birthday = datetime(year=1970, month=1, day=1) dynamic_list.birthday # <class 'list'>: [datetime.datetime(1970, 1, 1, 0, 0), datetime.datetime(1945, 2, 6, 0, 0)] self.assertTrue(len(dynamic_list.birthday) == 2)
def test_any_method_without_expression_and_empty_list(self): l = List() self.assertEqual(False, l.any())
def test_order_by_bare(self): data = [1, -1, 7, 200, 4, 3] sorted_data = sorted(data) linq_data = List(data).order_by() self.assertEqual(sorted_data, linq_data)
def test_sum_method(self): self.list = List(1, 2, 3, 4, 5) self.assertEqual(self.list.sum, 15)
def test_init_with_several_items(self): dynamic_list = List(Mock(), Mock(), Mock()) self.assertTrue(dynamic_list)
def test_non_list_properties_should_combine_values(self): a, b = Mock(), Mock() a.foo = 1 b.foo = 2 self.list = List(a, b) self.assertEqual(self.list.foo, [1, 2])
def test_min_method(self): self.list = List(1, 2, 3, 4, 5) self.assertEqual(self.list.min, 1)
def test_max_method(self): self.list = List(1, 2, 3, 4, 5) self.assertEqual(self.list.max, 5)
def test_intersect_method(self): e1 = self.list[0] l2 = List(e1) intersected = self.list.intersect(l2) self.assertEqual(len(intersected), 1) self.assertEqual(intersected[0], e1)
def test_avg_method(self): self.list = List(1, 2, 3, 4, 5) self.assertEqual(self.list.avg, 3)