-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_linq.py
209 lines (166 loc) · 6.85 KB
/
test_linq.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import unittest
from src.ipyquery import Linq
class Info():
def __init__(self, name, age, favorites=[]):
self.name = name
self.age = age
self.favorites = favorites
class LinqTests(unittest.TestCase):
def test_select_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertEqual(
linq.select(lambda x: x * 2).tolist(), [2, 4, 6, 8, 10, 12])
def test_enum_select_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertEqual(
linq.enum_select(lambda i, x: x * i).tolist(),
[0, 2, 6, 12, 20, 30])
def test_enum_select_2(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertEqual(linq.enum_select().tolist(), [(0, 1), (1, 2), (2, 3),
(3, 4), (4, 5), (5, 6)])
def test_enum_1(self):
linq = list(enumerate(Linq([1, 2, 3, 4, 5, 6])))
self.assertEqual(linq, [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5),
(5, 6)])
def test_where_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertEqual(linq.where(lambda x: x > 3).tolist(), [4, 5, 6])
def test_first_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertEqual(linq.first(lambda x: x > 3), 4)
def test_first_2(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertEqual(linq.first(), 1)
def test_any_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertTrue(linq.any())
def test_any_2(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertFalse(linq.any(lambda x: x > 6))
def test_any_3(self):
linq = Linq([1, 2, 3, 4, 5, 6])
where = linq.where(lambda x: x > 6)
self.assertFalse(where.any())
def test_all_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertFalse(linq.all(lambda x: x > 3))
def test_all_2(self):
linq = Linq([1, 2, 3, 4, 5, 6])
self.assertTrue(linq.all(lambda x: x < 7))
def test_order_by_1(self):
linq = Linq([4, 2, 3, 1, 6, 5])
self.assertEqual(linq.orderby().tolist(), [1, 2, 3, 4, 5, 6])
def test_order_by_desc_1(self):
linq = Linq([4, 2, 3, 1, 6, 5])
self.assertEqual(linq.orderby_desc().tolist(), [6, 5, 4, 3, 2, 1])
def test_order_by_2(self):
linq = Linq([Info("Arash", 24), Info("Sara", 22), Info("Kiarash", 16)])
query = linq.orderby(lambda x: x.age).select(lambda x: x.name)
self.assertEqual(query.tolist(), ["Kiarash", "Sara", "Arash"])
def test_max_1(self):
linq = Linq([Info("Arash", 24), Info("Sara", 22), Info("Kiarash", 16)])
max = linq.max(lambda x: x.age)
self.assertEqual(max.name, "Arash")
def test_min_1(self):
linq = Linq([Info("Arash", 24), Info("Sara", 22), Info("Kiarash", 16)])
min = linq.min(lambda x: x.age)
self.assertEqual(min.name, "Kiarash")
def test_sum_1(self):
linq = Linq([1, 2, 3, 4, 5, 6])
sum = linq.sum()
self.assertEqual(sum, 21)
def test_sum_2(self):
linq = Linq([Info("Arash", 24), Info("Sara", 22), Info("Kiarash", 16)])
sum = linq.sum(lambda x: x.age)
self.assertEqual(sum, 62)
def test_len_1(self):
linq = Linq([1, 2, 3])
self.assertEqual(len(linq), 3)
def test_avg_1(self):
linq = Linq([1, 2, 3])
self.assertEqual(linq.average(), 2.0)
def test_distinct_1(self):
linq = Linq([1, 2, 3, 3, 3, 2, 2, 1, 4])
self.assertEqual(linq.distinct().tolist(), [1, 2, 3, 4])
def test_distinct_2(self):
linq = Linq([Info("Arash", 24), Info("Sara", 24), Info("Kiarash", 16)])
query = linq.distinct(lambda x: x.age).select(lambda x: x.name)
self.assertEqual(linq.tolist(), ["Arash", "Kiarash"])
def test_select_many_1(self):
linq = Linq([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
query = linq.select_many()
self.assertEqual(linq.tolist(), [1, 2, 3, 3, 4, 5, 5, 6, 7])
def test_select_many_2(self):
linq = Linq([[1, 2, 3], [3, 4, 5], [5, 6, 7]])
query = linq.select_many().distinct()
self.assertEqual(linq.tolist(), [1, 2, 3, 4, 5, 6, 7])
def test_select_many_2(self):
linq = Linq([[3, 4, 5], [1, 2, 3], [5, 6, 7]])
query = linq.select_many().distinct().orderby()
self.assertEqual(linq.tolist(), [1, 2, 3, 4, 5, 6, 7])
def test_select_many_3(self):
linq = Linq([
Info("Arash", 24, ["Programming", "Sports"]),
Info("Sara", 22, ["Reading", "Sports", "Watching"]),
])
query = linq.select_many(lambda x: x.favorites).distinct()
self.assertEqual(linq.tolist(),
["Programming", "Sports", "Reading", "Watching"])
def test_todict_1(self):
linq = Linq([1, 2, 3, 4])
self.assertEqual(linq.todict(lambda x: str(x)), {
'1': 1,
'2': 2,
'3': 3,
'4': 4,
})
def test_todict_1(self):
linq = Linq([1, 2, 3, 4])
self.assertEqual(
linq.todict(key_selector=lambda x: str(x),
value_selector=lambda y: y**y), {
'1': 1,
'2': 4,
'3': 27,
'4': 256,
})
def test_groupby_1(self):
linq = Linq([1, 2, 3, 4])
query = linq.groupby(lambda x: x**0)
self.assertEqual(query.tolist(), [(1, [1, 2, 3, 4])])
def test_groupby_2(self):
linq = Linq([
Info("Arash", 24, "Programming"),
Info("Sara", 22, "Programming"),
])
query = linq.groupby(lambda x: x.favorites,
value_selector=lambda x: x.name)
self.assertEqual(query.single(), ('Programming', ['Arash', 'Sara']))
def test_single_1(self):
linq = Linq([1, 2, 3, 4])
query = linq.groupby(lambda x: x**0)
self.assertEqual(query.single(), (1, [1, 2, 3, 4]))
def test_reverse_1(self):
linq = Linq([1, 2, 3, 4])
query = linq.reverse()
self.assertEqual(query.tolist(), [4, 3, 2, 1])
def test_remove_all_1(self):
linq = Linq([1, 2, 3, 4])
query = linq.remove_all(lambda x: x > 2)
self.assertEqual(query.tolist(), [1, 2])
def test_skip_1(self):
linq = Linq([1, 2, 3, 4])
query = linq.skip(2)
self.assertEqual(query.tolist(), [3, 4])
def test_take_1(self):
linq = Linq([1, 2, 3, 4])
query = linq.take(2)
self.assertEqual(query.tolist(), [1, 2])
def test_example_1(self):
my_list = Linq([5, 1, 7, 2, 3, 10, 1, 4, 5])
powered_cleaned = my_list.distinct().where(
lambda x: x <= 5).orderby().select(lambda x: x**2).tolist()
self.assertEqual(powered_cleaned, [1, 4, 9, 16, 25])
if __name__ == '__main__':
unittest.main()