-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
84 lines (58 loc) · 1.76 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from random import randint
import pytest
from linq import linq
def random_ints(length=100):
return [randint(0, 10000) for _ in range(length)]
def test_linq_select():
data = [1, 2]
expected = [2, 4]
actual = linq(data).select(lambda x: x * 2).to_list()
assert actual == expected
def test_linq_where():
data = [1, 2, 3, 4]
expected = [2, 4]
actual = linq(data).where(lambda x: x % 2 == 0).to_list()
assert actual == expected
def test_linq_take():
data = [1, 2, 3, 4]
expected = [1, 2]
actual = linq(data).take(2).to_list()
assert actual == expected
def test_linq_skip():
data = [1, 2, 3, 4]
expected = [3, 4]
actual = linq(data).skip(2).to_list()
assert actual == expected
def test_select_many():
data = [1, 2, 3]
expected = [1, 1, 2, 2, 3, 3]
actual = linq(data).select_many(lambda x: [x] * 2).to_list()
assert actual == expected
def test_linq_iter():
data = [1, 2, 3, 4]
actual = list(x for x in linq(data))
expected = data
assert actual == expected
def test_linq_skip_while():
data = [1, 2, 3, 4]
actual = linq(data).skip_while(lambda x: x < 3).to_list()
expected = [3, 4]
assert actual == expected
def test_linq_contains():
data = [1, 3, 4, 5]
l = linq(data)
assert all(l.contains(x) for x in data)
def test_linq_count():
data = [1, 2, 3, 4]
expected = 4
actual = linq(data).count()
assert actual == expected
def test_linq_to_dict():
data = [(1, "a"), (2, "b")]
expected = {1: "a", 2: "b"}
actual = linq(data).to_dict(key=lambda x: x[0], value=lambda x: x[1])
assert actual == expected
def test_linq_single_raises_value_error():
data = [1, 2, 3, 4]
with pytest.raises(ValueError):
linq(data).single()