def test_first(self): data = [1, 2, 3] ret = Q(data).first() self.assertEqual(1, ret)
def test_single_on_empty(self): data = [] self.assertRaises(ValueError, lambda: Q(data).single())
def test_last(self): data = [1, 2, 3] ret = Q(data).last() self.assertEqual(3, ret)
def test_last_on_empty(self): data = [] self.assertRaises(ValueError, lambda: Q(data).last())
def test_without_value_selector(self): data = ("one", "two", "four") ret = Q(data).to_dict(lambda x: x[0]) self.assertIsInstance(ret, dict) self.assertEqual({"o": "one", "t": "two", "f": "four"}, ret)
def test_take_n(self): data = [1, 2, 3, 4, 5, 1, 2, 4, 8, 16, 32] ret = Q(data).take(5).to_list() self.assertSequenceEqual((1, 2, 3, 4, 5), ret)
def test_shorter_sequence(self): data = [1, 2, 3] self.assertFalse(Q(data).sequence_equal((1, 2)))
def test_default_on_empty(self): data = () ret = Q(data).default_if_empty("default").to_tuple() self.assertEqual(("default", ), ret)
def test_without_initializer(self): data = (1, 2, 3) ret = Q(data).reduce(lambda a, b: a + b) self.assertEqual(6, ret)
def test_with_initializer(self): data = (1, 2, 3) ret = Q(data).reduce([], lambda a, b: a + [b]) self.assertSequenceEqual(data, ret)
def test_list(self): data = [1, 2, 3] ret = Q(data).to_tuple() self.assertIsInstance(ret, tuple) self.assertSequenceEqual(data, ret)
def test_single(self): data = [1] ret = Q(data).single() self.assertEqual(1, ret)
def test_single_or_default_on_multi(self): data = [1, 2, 3] self.assertRaises(ValueError, lambda: Q(data).single_or_default(0))
def test_something(self): data = (1, 2, 3) ret = Q(data) self.assertTrue(all(a == b for a, b in zip_longest(data, ret)))
def test_longer_sequence(self): data = [1, 2, 3] self.assertFalse(Q(data).sequence_equal((1, 2, 3, 4)))
def test_default_on_non_empty(self): data = ("foo", "bar") ret = Q(data).default_if_empty("default").to_tuple() self.assertEqual(("foo", "bar"), ret)
def test_different_sequence(self): data = [1, 2, 3] self.assertFalse(Q(data).sequence_equal((4, 5, 6)))
def test_with_value_selector(self): data = ("one", "two", "four") ret = Q(data).to_dict(lambda x: x[0], lambda x: ord(x[1])) self.assertIsInstance(ret, dict) self.assertEqual({"o": 110, "t": 119, "f": 111}, ret)
def test_same_sequence(self): data = [1, 2, 3] self.assertTrue(Q(data).sequence_equal((1, 2, 3)))
def test_take_while(self): data = [1, 2, 3, 4, 5, 1, 2, 4, 8, 16, 32] ret = Q(data).take_while(lambda x: x < 5).to_list() self.assertSequenceEqual((1, 2, 3, 4), ret)
def test_distinct_with_key_selector(self): data = (1, 1, 2, 5, 1) ret = Q(data).distinct(lambda x: x % 2).to_set() self.assertSetEqual({1, 2}, ret)
def test_first_or_default(self): data = [1, 2, 3] ret = Q(data).first_or_default(0) self.assertEqual(1, ret)
def test_ordered_distinct(self): data = (1, 1, 2, 5, 1) ret = Q(data).distinct_ordered().to_list() self.assertSequenceEqual([1, 2, 5, 1], ret)
def test_last_or_default(self): data = [1, 2, 3] ret = Q(data).last_or_default(0) self.assertEqual(3, ret)
def test_ordered_distinct_with_key_selector(self): data = (1, 1, 2, 5, 1) ret = Q(data).distinct_ordered(lambda x: x % 2).to_list() self.assertSequenceEqual([1, 2, 5], ret)
def test_last_or_default_on_empty(self): data = [] ret = Q(data).last_or_default(0) self.assertEqual(0, ret)
def test_distinct(self): data = (1, 1, 2, 5, 1) ret = Q(data).distinct().to_set() self.assertSetEqual({1, 2, 5}, ret)
def test_join(self): data = ["hello", "world"] ret = Q(data).join(" and ") self.assertEqual("hello and world", ret)
def test_single_or_default(self): data = [1] ret = Q(data).single_or_default(0) self.assertEqual(1, ret)