def test_skip_is_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).skip(3) self.assertEqual(a.trace, []) c = b.take().to_list() self.assertEqual(a.trace, [0, 1, 2, 3])
def test_select_with_corresponding_deferred(self): a = TracingGenerator() self.assertListEqual(a.trace, []) b = Queryable(a).select_with_correspondence(lambda x: x * 2) self.assertListEqual(a.trace, []) b.take(3).to_list() self.assertListEqual(a.trace, list(range(3)))
def test_group_join_is_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = [2, 3, 4, 5, 6] c = Queryable(a).group_join(b) self.assertEqual(a.trace, []) d = c.take(3).to_list()
def test_select_with_index_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).select_with_index() self.assertEqual(a.trace, []) b.take(3).to_list() self.assertEqual(a.trace, [0, 1, 2])
def test_select_many_with_index_closed(self): a = [{'name' : 'Alice', 'flowers' : ['Agapanthus', 'Allium', 'Alpina', 'Alstroemeria', 'Amaranthus', 'Amarylis' ] }, {'name' : 'Bob', 'flowers' : ['Bouvardia' ]}, {'name' : 'Chris', 'flowers' : ['Carnations', 'Cattleya', 'Celosia', 'Chincherinchee', 'Chrysanthemum']}] b = Queryable(a) b.close() self.assertRaises(ValueError, lambda: b.select_many_with_index(lambda i, x: [str(i) + flower for flower in x['flowers']]))
def test_select_many_with_index_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).select_many_with_index(lambda index, source_element: [source_element] * index) self.assertEqual(a.trace, []) b.take(10).to_list() self.assertEqual(a.trace, [0, 1, 2, 3, 4])
def test_select_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).select(lambda x: x*2) self.assertEqual(a.trace, []) c = b.take(3).to_list() self.assertEqual(a.trace, [0, 1, 2])
def test_take_is_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).take() self.assertEqual(a.trace, []) c = b.to_list() self.assertEqual(a.trace, [0])
def test_where_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).where(lambda x: x % 3 == 0) self.assertEqual(a.trace, []) c = b.take(2).to_list() self.assertEqual(a.trace, [0, 1, 2, 3])
def test_default_if_empty_is_deferred_not_empty(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).default_if_empty(42) self.assertEqual(a.trace, []) c = b.take(3).to_list() self.assertEqual(a.trace, [0, 1, 2])
def test_select_many_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).select_many(lambda x: [x] * x) self.assertEqual(a.trace, []) b.take(10).to_list() self.assertEqual(a.trace, [0, 1, 2, 3, 4])
def test_join_is_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = [2, 3, 4, 5, 6] c = Queryable(a).join(b) self.assertEqual(a.trace, []) d = c.take(3).to_list() e = [(2, 2), (3, 3), (4, 4)] self.assertEqual(d, e)
def test_difference_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = [3, 7, 2, 9, 10] c = Queryable(a).difference(b) self.assertEqual(a.trace, []) d = c.take(10).to_list() e = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] self.assertEqual(a.trace, e)
def test_union_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = [3, 7, 2, 9, 10] c = Queryable(a).union(b) self.assertEqual(a.trace, []) d = c.take(5).to_list() e = [0, 1, 2, 3, 4] self.assertEqual(a.trace, e)
def test_intersect_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = [3, 7, 2, 9, 10] c = Queryable(a).intersect(b) self.assertEqual(a.trace, []) d = c.take(5).to_list() e = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(a.trace, e)
def test_take_while_is_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Queryable(a).take_while(lambda x: x < 3) self.assertEqual(a.trace, []) c = b.to_list() # 3 is included here in the trace because it must have been consumed in order to test # whether it satisfies the predicate self.assertEqual(a.trace, [0, 1, 2, 3])
def test_zip_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = TracingGenerator() self.assertEqual(b.trace, []) c = Queryable(a).zip(b) self.assertEqual(a.trace, []) self.assertEqual(b.trace, []) d = c.take(4).to_list() self.assertEqual(d, [(0, 0), (1, 1), (2, 2), (3, 3)])
def test_min_selector_not_callable(self): a = [5, 7, -3, 2, 1, 9, 3, 2, 1, -13, 7] self.assertRaises(TypeError, lambda: Queryable(a).min("not callable"))
def test_single_or_default_predicate_not_callable(self): a = ["Aardvark", "Cat", "Elephant"] self.assertRaises(TypeError, lambda: Queryable(a).single_or_default('Animal', "not callable"))
def test_min_closed(self): b = Queryable([1]) b.close() self.assertRaises(ValueError, lambda: b.min())
def test_aggregate_closed(self): b = Queryable([]) b.close() self.assertRaises(ValueError, lambda: b.aggregate(operator.add, 72))
def test_zip_non_iterable(self): a = [1, 2, 3] b = 5 self.assertRaises(TypeError, lambda: Queryable(a).zip(b))
def test_zip_longer_shorter(self): a = [1, 2, 3] b = [4, 5] c = Queryable(a).zip(b).to_list() self.assertEqual(c, [(1, 4), (2, 5)])
def test_last_closed(self): b = Queryable([]) b.close() self.assertRaises(ValueError, lambda: b.last())
def test_single_or_default_empty(self): a = [] b = Queryable(a).single_or_default(7) self.assertEqual(b, 7)
def test_zip_func(self): a = [1, 2, 3] b = [4, 5, 6] c = Queryable(a).zip(b, lambda x, y: int(str(x) + str(y))).to_list() self.assertEqual(c, [(14), (25), (36)])
def test_single_or_default(self): a = [5] b = Queryable(a).single_or_default(7) self.assertEqual(b, 5)
def test_single_multiple(self): a = [4, 7] self.assertRaises(ValueError, lambda: Queryable(a).single())
def test_single_empty(self): a = [] self.assertRaises(ValueError, lambda: Queryable(a).single())
def test_zip(self): a = [1, 2, 3] b = [4, 5, 6] c = Queryable(a).zip(b).to_list() self.assertEqual(c, [(1, 4), (2, 5), (3, 6)])
def test_single_or_default_predicate_negative(self): a = ["Aardvark", "Cat", "Elephant"] b = Queryable(a).single_or_default('Animal', lambda x: x.startswith('D')) self.assertEqual(b, "Animal")
def test_zip_infinite(self): c = Queryable(infinite()).zip(infinite()).take(4).to_list() self.assertEqual(c, [(0, 0), (1, 1), (2, 2), (3, 3)])
def test_single_or_default_multiple(self): a = [5, 7, 2, 3, 1] self.assertRaises(ValueError, lambda: Queryable(a).single_or_default(13))
def test_zip_longer_empty(self): a = [1, 2, 3] b = [] c = Queryable(a).zip(b).to_list() self.assertEqual(c, [])
def test_min(self): a = [5, 7, -3, 2, 1, 9, 3, 2, 1, -8, 7] b = Queryable(a).min() self.assertEqual(b, -8)
def test_zip_closed(self): a = Queryable([1, 2, 3]) a.close() self.assertRaises(ValueError, lambda: a.zip([4, 5, 6]))
def test_select_many_closed(self): b = Queryable([1, 2, 4, 8]) b.close() self.assertRaises(ValueError, lambda: b.select_many(lambda x: x * 2))
def test_zip_empty_longer(self): a = [] b = [4, 5, 6] c = Queryable(a).zip(b).to_list() self.assertEqual(c, [])
def test_single_or_default_predicate_empty(self): a = [] b = Queryable(a).single_or_default('foo', lambda x: x.startswith('D')) self.assertEqual(b, 'foo')
def test_first_closed(self): a = ['Agapanthus', 'Allium', 'Alpina', 'Alstroemeria', 'Amaranthus', 'Amarylis', 'Bouvardia', 'Carnations', 'Cattleya', 'Celosia', 'Chincherinchee', 'Chrysanthemum'] b = Queryable(a) b.close() self.assertRaises(ValueError, lambda: b.group_by(lambda x: x[0]))
def test_single_predicate(self): a = ["Aardvark", "Cat", "Dog", "Elephant"] b = Queryable(a).single(lambda x: x.startswith('D')) self.assertEqual(b, "Dog")
def test_zip_not_callable(self): a = [1, 2, 3] b = [4, 5, 6] self.assertRaises(TypeError, lambda: Queryable(a).zip(b, "not callable"))
def test_first_or_default_closed(self): b = Queryable([]) b.close() self.assertRaises(ValueError, lambda: b.first_or_default(37))
def test_single_predicate_not_callable(self): a = ["Aardvark", "Cat", "Dog", "Elephant"] self.assertRaises(TypeError, lambda: Queryable(a).single("not callable"))
def test_select_with_index_closed(self): a = [27, 74, 18, 48, 57, 97, 76, 20, 91, 8, 80, 59, 20, 32, 58, 12, 74, 78, 4] b = Queryable(a) b.close() self.assertRaises(ValueError, lambda: b.select_with_index(lambda x, y: x*y))
def test_single_predicate_empty(self): a = [] self.assertRaises( ValueError, lambda: Queryable(a).single(lambda x: x.startswith('D')))
def test_single_predicate_multiple(self): a = ["Aardvark", "Cat", "Dog", "Elephant", "Dolphin"] self.assertRaises( ValueError, lambda: Queryable(a).single(lambda x: x.startswith('D')))
def test_single_closed(self): a = [5] b = Queryable(a) b.close() self.assertRaises(ValueError, lambda: b.single())
def test_to_str_closed(self): a = ['Aardvark', 'Balloon', 'Carrot', 'Daisy', 'Ecological'] b = Queryable(a) b.close() self.assertRaises(ValueError, lambda: b.to_str())
def test_skip_closed(self): a = ['a', 'b', 'c'] b = Queryable(a) b.close() self.assertRaises(ValueError, lambda: b.skip(1))
def test_group_join_closed(self): a = [1, 2] b = [2, 3] c = Queryable(a) c.close() self.assertRaises(ValueError, lambda: c.group_join(b))
def test_contains_closed(self): b = Queryable([1]) b.close() self.assertRaises(ValueError, lambda: b.contains(5))
def test_single(self): a = [5] b = Queryable(a).single() self.assertEqual(b, 5)
def test_min_empty(self): self.assertRaises(ValueError, lambda: Queryable([]).min())
def test_getitem_closed(self): b = Queryable([1]) b.close() self.assertRaises(ValueError, lambda: b[0])
def test_group_join_non_iterable(self): a = [1, 2, 3] b = None self.assertRaises(TypeError, lambda: Queryable(a).group_join(b))
def test_any_closed(self): b = Queryable([1]) b.close() self.assertRaises(ValueError, lambda: b.any())
def test_min_selector(self): a = [5, 7, -3, 2, 1, 9, 3, 2, 1, -8, 7] b = Queryable(a).min(abs) self.assertEqual(b, 1)