Debian Codegraph @

petl/test/transform/test_dedup.py

raw
1 from __future__ import absolute_import, print_function, division
2
3 import pytest
4
5 from petl.errors import FieldSelectionError
6 from petl.test.helpers import ieq
7 from petl.transform.dedup import duplicates, unique, conflicts, distinct, \
8 isunique
9
10
11 def test_duplicates():
12
13 table = (('foo', 'bar', 'baz'),
14 ('A', 1, 2),
15 ('B', '2', '3.4'),
16 ('D', 'xyz', 9.0),
17 ('B', u'3', u'7.8', True),
18 ('B', '2', 42),
19 ('E', None),
20 ('D', 4, 12.3))
21
22 result = duplicates(table, 'foo')
23 expectation = (('foo', 'bar', 'baz'),
24 ('B', '2', '3.4'),
25 ('B', u'3', u'7.8', True),
26 ('B', '2', 42),
27 ('D', 'xyz', 9.0),
28 ('D', 4, 12.3))
29 ieq(expectation, result)
30
31 # test with compound key
32 result = duplicates(table, key=('foo', 'bar'))
33 expectation = (('foo', 'bar', 'baz'),
34 ('B', '2', '3.4'),
35 ('B', '2', 42))
36 ieq(expectation, result)
37
38
39 def test_duplicates_headerless_no_keys():
40 """Removing the duplicates from an empty table without specifying which
41 columns shouldn't be a problem.
42 """
43 table = []
44 actual = duplicates(table)
45 expect = []
46 ieq(expect, actual)
47
48
49 def test_duplicates_headerless_explicit():
50 table = []
51 with pytest.raises(FieldSelectionError):
52 for i in duplicates(table, 'foo'):
53 pass
54
55
56 def test_duplicates_empty():
57 table = (('foo', 'bar'),)
58 expect = (('foo', 'bar'),)
59 actual = duplicates(table, key='foo')
60 ieq(expect, actual)
61
62
63 def test_duplicates_wholerow():
64
65 table = (('foo', 'bar', 'baz'),
66 ('A', 1, 2),
67 ('B', '2', '3.4'),
68 ('B', '2', '3.4'),
69 ('D', 4, 12.3))
70
71 result = duplicates(table)
72 expectation = (('foo', 'bar', 'baz'),
73 ('B', '2', '3.4'),
74 ('B', '2', '3.4'))
75 ieq(expectation, result)
76
77
78 def test_unique():
79
80 table = (('foo', 'bar', 'baz'),
81 ('A', 1, 2),
82 ('B', '2', '3.4'),
83 ('D', 'xyz', 9.0),
84 ('B', u'3', u'7.8', True),
85 ('B', '2', 42),
86 ('E', None),
87 ('D', 4, 12.3),
88 ('F', 7, 2.3))
89
90 result = unique(table, 'foo')
91 expectation = (('foo', 'bar', 'baz'),
92 ('A', 1, 2),
93 ('E', None),
94 ('F', 7, 2.3))
95 ieq(expectation, result)
96 ieq(expectation, result)
97
98 # test with compound key
99 result = unique(table, key=('foo', 'bar'))
100 expectation = (('foo', 'bar', 'baz'),
101 ('A', 1, 2),
102 ('B', u'3', u'7.8', True),
103 ('D', 4, 12.3),
104 ('D', 'xyz', 9.0),
105 ('E', None),
106 ('F', 7, 2.3))
107 ieq(expectation, result)
108 ieq(expectation, result)
109
110
111 def test_unique_empty():
112 table = (('foo', 'bar'),)
113 expect = (('foo', 'bar'),)
114 actual = unique(table, key='foo')
115 ieq(expect, actual)
116
117
118 def test_unique_wholerow():
119
120 table = (('foo', 'bar', 'baz'),
121 ('A', 1, 2),
122 ('B', '2', '3.4'),
123 ('B', '2', '3.4'),
124 ('D', 4, 12.3))
125
126 result = unique(table)
127 expectation = (('foo', 'bar', 'baz'),
128 ('A', 1, 2),
129 ('D', 4, 12.3))
130 ieq(expectation, result)
131
132
133 def test_conflicts():
134
135 table = (('foo', 'bar', 'baz'),
136 ('A', 1, 2),
137 ('B', '2', None),
138 ('D', 'xyz', 9.4),
139 ('B', None, u'7.8', True),
140 ('E', None),
141 ('D', 'xyz', 12.3),
142 ('A', 2, None))
143
144 result = conflicts(table, 'foo', missing=None)
145 expectation = (('foo', 'bar', 'baz'),
146 ('A', 1, 2),
147 ('A', 2, None),
148 ('D', 'xyz', 9.4),
149 ('D', 'xyz', 12.3))
150 ieq(expectation, result)
151 ieq(expectation, result)
152
153 result = conflicts(table, 'foo', missing=None, exclude='baz')
154 expectation = (('foo', 'bar', 'baz'),
155 ('A', 1, 2),
156 ('A', 2, None))
157 ieq(expectation, result)
158 ieq(expectation, result)
159
160 result = conflicts(table, 'foo', missing=None, exclude=('bar', 'baz'))
161 expectation = (('foo', 'bar', 'baz'),)
162 ieq(expectation, result)
163 ieq(expectation, result)
164
165 result = conflicts(table, 'foo', missing=None, include='bar')
166 expectation = (('foo', 'bar', 'baz'),
167 ('A', 1, 2),
168 ('A', 2, None))
169 print(expectation)
170 print(list(result))
171 ieq(expectation, result)
172 ieq(expectation, result)
173
174 result = conflicts(table, 'foo', missing=None, include=('bar', 'baz'))
175 expectation = (('foo', 'bar', 'baz'),
176 ('A', 1, 2),
177 ('A', 2, None),
178 ('D', 'xyz', 9.4),
179 ('D', 'xyz', 12.3))
180 ieq(expectation, result)
181 ieq(expectation, result)
182
183
184 def test_conflicts_empty():
185 table = (('foo', 'bar'),)
186 expect = (('foo', 'bar'),)
187 actual = conflicts(table, key='foo')
188 ieq(expect, actual)
189
190
191 def test_distinct():
192
193 table = (('foo', 'bar', 'baz'),
194 ('A', 1, 2),
195 ('B', '2', '3.4'),
196 ('B', '2', '3.4'),
197 ('D', 4, 12.3),
198 (None, None, None))
199
200 result = distinct(table)
201 expect = (('foo', 'bar', 'baz'),
202 (None, None, None),
203 ('A', 1, 2),
204 ('B', '2', '3.4'),
205 ('D', 4, 12.3))
206 ieq(expect, result)
207
208
209 def test_distinct_count():
210
211 table = (('foo', 'bar', 'baz'),
212 (None, None, None),
213 ('A', 1, 2),
214 ('B', '2', '3.4'),
215 ('B', '2', '3.4'),
216 ('D', 4, 12.3))
217
218 result = distinct(table, count='count')
219 expect = (('foo', 'bar', 'baz', 'count'),
220 (None, None, None, 1),
221 ('A', 1, 2, 1),
222 ('B', '2', '3.4', 2),
223 ('D', 4, 12.3, 1))
224 ieq(expect, result)
225
226
227 def test_key_distinct():
228
229 table = (('foo', 'bar', 'baz'),
230 (None, None, None),
231 ('A', 1, 2),
232 ('B', '2', '3.4'),
233 ('B', '2', '5'),
234 ('D', 4, 12.3))
235
236 result = distinct(table, key='foo')
237 expect = (('foo', 'bar', 'baz'),
238 (None, None, None),
239 ('A', 1, 2),
240 ('B', '2', '3.4'),
241 ('D', 4, 12.3))
242 ieq(expect, result)
243
244
245 def test_key_distinct_2():
246 # test for https://github.com/alimanfoo/petl/issues/318
247
248 tbl = (('a', 'b'),
249 ('x', '1'),
250 ('x', '3'),
251 ('y', '1'),
252 (None, None))
253
254 result = distinct(tbl, key='b')
255 expect = (('a', 'b'),
256 (None, None),
257 ('x', '1'),
258 ('x', '3'))
259 ieq(expect, result)
260
261
262 def test_key_distinct_count():
263
264 table = (('foo', 'bar', 'baz'),
265 ('A', 1, 2),
266 ('B', '2', '3.4'),
267 ('B', '2', '5'),
268 ('D', 4, 12.3),
269 (None, None, None))
270
271 result = distinct(table, key='foo', count='count')
272 expect = (('foo', 'bar', 'baz', 'count'),
273 (None, None, None, 1),
274 ('A', 1, 2, 1),
275 ('B', '2', '3.4', 2),
276 ('D', 4, 12.3, 1))
277 ieq(expect, result)
278
279
280 def test_isunique():
281
282 table = (('foo', 'bar'), ('a', 1), ('b',), ('b', 2), ('c', 3, True))
283 assert not isunique(table, 'foo')
284 assert isunique(table, 'bar')