|
1 | | -from typing import Dict, List, Union, Callable |
| 1 | +from typing import Dict, List, Union, Callable, Optional |
2 | 2 |
|
3 | 3 | import operator as op |
4 | 4 | import pytest |
5 | 5 | import ulist as ul |
6 | 6 | from ulist.utils import check_test_result, compare_dtypes, expand_dtypes |
7 | 7 |
|
8 | | -ELEM_TYPE = Union[float, int, bool, str] |
9 | | -LIST_TYPE = Union[List[float], List[int], List[bool], List[str]] |
| 8 | +ELEM_TYPE = Union[Optional[float], |
| 9 | + Optional[int], Optional[bool], Optional[str]] |
| 10 | +LIST_TYPE = Union[List[Optional[float]], List[Optional[int]], |
| 11 | + List[Optional[bool]], List[Optional[str]]] |
10 | 12 | COUNTER = Union[Dict[int, int], Dict[bool, int]] |
11 | 13 | RESULT = Union[ELEM_TYPE, LIST_TYPE, COUNTER] |
12 | 14 |
|
|
88 | 90 | ('to_list', 'float', [1.0, 2.0], [1.0, 2.0]), |
89 | 91 | ('to_list', 'int', [1, 2], [1, 2]), |
90 | 92 | ('to_list', 'string', ['foo', 'bar'], ['foo', 'bar']), |
91 | | -
|
92 | | - ( |
93 | | - 'unique', |
94 | | - 'bool', |
95 | | - [True, False, True, False, True], |
96 | | - [False, True], |
97 | | - ), |
98 | | - ( |
99 | | - 'unique', |
100 | | - 'float', |
101 | | - [5.0, 3.0, 2.0, 4.0, 1.0, 3.0], |
102 | | - [1.0, 2.0, 3.0, 4.0, 5.0], |
103 | | - ), |
104 | | - ( |
105 | | - 'unique', |
106 | | - 'int', |
107 | | - [5, 3, 2, 4, 1, 3], |
108 | | - [1, 2, 3, 4, 5], |
109 | | - ), |
110 | | - ( |
111 | | - 'unique', |
112 | | - 'string', |
113 | | - ['foo', 'bar', 'foo'], |
114 | | - ['bar', 'foo'], |
115 | | - ), |
116 | 93 | ], |
117 | 94 | ) |
118 | 95 | def test_methods_no_arg( |
@@ -211,6 +188,73 @@ def test_methods_no_arg( |
211 | 188 | ("not_equal_scala", 'string', ['foo', 'bar', 'baz'], |
212 | 189 | [True, False, True], {"elem": 'bar'}), |
213 | 190 |
|
| 191 | + ('union_all', 'bool', [True, False], [True, False, False, True], { |
| 192 | + 'other': ul.from_seq([False, True], dtype='bool')}), |
| 193 | + ('union_all', 'float', [1.0, 2.0], [1.0, 2.0, 3.0, 4.0], { |
| 194 | + 'other': ul.from_seq([3.0, 4.0], dtype='float')}), |
| 195 | + ('union_all', 'int', [1, 2], [1, 2, 3, 4], { |
| 196 | + 'other': ul.from_seq([3, 4], dtype='int')}), |
| 197 | + ('union_all', 'string', ['foo', 'bar'], ['foo', 'bar', 'baz', 'zoo'], { |
| 198 | + 'other': ul.from_seq(['baz', 'zoo'], dtype='string')}), |
| 199 | +
|
| 200 | + ('var', 'bool', [True, False], 0.25, {}), |
| 201 | + ('var', 'bool', [True, True, True, False], 0.25, {"ddof": 1}), |
| 202 | + ('var', 'float', [1.0, 2.0, 3.0, 4.0], 1.25, {}), |
| 203 | + ('var', 'float', [1.0, 2.0, 3.0], 1.0, {"ddof": 1}), |
| 204 | + ('var', 'int', [1, 2, 3, 4], 1.25, {}), |
| 205 | + ('var', 'int', [1, 2, 3], 1.0, {"ddof": 1}), |
| 206 | +
|
| 207 | + ("where", "bool", [True, True, False, False], [ |
| 208 | + False, False], {"fn": lambda x: x == False},), # noqa: E712 |
| 209 | + ("where", "float", [1.0, 2.0, 3.0, 4.0], [ |
| 210 | + 1.0, 2.0], {"fn": lambda x: x < 3.0},), |
| 211 | + ("where", "int", [1, 2, 3, 4], [ |
| 212 | + 3, 4], {"fn": lambda x: x > 2},), |
| 213 | + ("where", "string", ['foo', 'bar', 'baz'], [ |
| 214 | + 'foo', 'baz'], {"fn": lambda x: x != 'bar'},), |
| 215 | + ], |
| 216 | +) |
| 217 | +def test_methods_with_args( |
| 218 | + test_method: str, |
| 219 | + dtype: str, |
| 220 | + nums: LIST_TYPE, |
| 221 | + expected_value: RESULT, |
| 222 | + kwargs: dict, |
| 223 | +) -> None: |
| 224 | + arr = ul.from_seq(nums, dtype) |
| 225 | + result = getattr(arr, test_method)(**kwargs) |
| 226 | + check_test_result(dtype, test_method, result, expected_value) |
| 227 | + |
| 228 | + |
| 229 | +@expand_dtypes |
| 230 | +@pytest.mark.parametrize( |
| 231 | + "test_method, dtype, nums, expected_value, kwargs", |
| 232 | + [ |
| 233 | + ('__setitem__', 'bool', [True, False], [ |
| 234 | + True, True], {'index': 1, 'elem': True}), |
| 235 | + ('__setitem__', 'float', [1.0, 2.0], [ |
| 236 | + 1.0, 3.0], {'index': 1, 'elem': 3.0}), |
| 237 | + ('__setitem__', 'int', [1, 2], [1, 3], {'index': 1, 'elem': 3}), |
| 238 | + ('__setitem__', 'string', ['foo', 'bar'], [ |
| 239 | + 'foo', 'baz'], {'index': 1, 'elem': 'baz'}), |
| 240 | +
|
| 241 | + ('append', 'bool', [True], [True, False], {'elem': False}), |
| 242 | + ('append', 'float', [1.0], [1.0, 2.0], {'elem': 2.0}), |
| 243 | + ('append', 'int', [1], [1, 2], {'elem': 2}), |
| 244 | + ('append', 'string', ['foo'], ['foo', 'bar'], {'elem': 'bar'}), |
| 245 | +
|
| 246 | + ('pop', 'bool', [True, False], [True], {}), |
| 247 | + ('pop', 'float', [1.0, 2.0], [1.0], {}), |
| 248 | + ('pop', 'int', [1, 2], [1], {}), |
| 249 | + ('pop', 'string', ['foo', 'bar'], ['foo'], {}), |
| 250 | +
|
| 251 | + ('set', 'bool', [True, False], [ |
| 252 | + True, True], {'index': 1, 'elem': True}), |
| 253 | + ('set', 'float', [1.0, 2.0], [1.0, 3.0], {'index': 1, 'elem': 3.0}), |
| 254 | + ('set', 'int', [1, 2], [1, 3], {'index': 1, 'elem': 3}), |
| 255 | + ('set', 'string', ['foo', 'bar'], [ |
| 256 | + 'foo', 'baz'], {'index': 1, 'elem': 'baz'}), |
| 257 | +
|
214 | 258 | ('replace', 'bool', [True, False, True], [ |
215 | 259 | False, False, False], {'old': True, 'new': False}), |
216 | 260 | ('replace', 'float', [1.0, 0.0, 1.0], [ |
@@ -283,73 +327,6 @@ def test_methods_no_arg( |
283 | 327 | {'ascending': False} |
284 | 328 | ), |
285 | 329 |
|
286 | | -
|
287 | | - ('union_all', 'bool', [True, False], [True, False, False, True], { |
288 | | - 'other': ul.from_seq([False, True], dtype='bool')}), |
289 | | - ('union_all', 'float', [1.0, 2.0], [1.0, 2.0, 3.0, 4.0], { |
290 | | - 'other': ul.from_seq([3.0, 4.0], dtype='float')}), |
291 | | - ('union_all', 'int', [1, 2], [1, 2, 3, 4], { |
292 | | - 'other': ul.from_seq([3, 4], dtype='int')}), |
293 | | - ('union_all', 'string', ['foo', 'bar'], ['foo', 'bar', 'baz', 'zoo'], { |
294 | | - 'other': ul.from_seq(['baz', 'zoo'], dtype='string')}), |
295 | | -
|
296 | | - ('var', 'bool', [True, False], 0.25, {}), |
297 | | - ('var', 'bool', [True, True, True, False], 0.25, {"ddof": 1}), |
298 | | - ('var', 'float', [1.0, 2.0, 3.0, 4.0], 1.25, {}), |
299 | | - ('var', 'float', [1.0, 2.0, 3.0], 1.0, {"ddof": 1}), |
300 | | - ('var', 'int', [1, 2, 3, 4], 1.25, {}), |
301 | | - ('var', 'int', [1, 2, 3], 1.0, {"ddof": 1}), |
302 | | -
|
303 | | - ("where", "bool", [True, True, False, False], [ |
304 | | - False, False], {"fn": lambda x: x == False},), # noqa: E712 |
305 | | - ("where", "float", [1.0, 2.0, 3.0, 4.0], [ |
306 | | - 1.0, 2.0], {"fn": lambda x: x < 3.0},), |
307 | | - ("where", "int", [1, 2, 3, 4], [ |
308 | | - 3, 4], {"fn": lambda x: x > 2},), |
309 | | - ("where", "string", ['foo', 'bar', 'baz'], [ |
310 | | - 'foo', 'baz'], {"fn": lambda x: x != 'bar'},), |
311 | | - ], |
312 | | -) |
313 | | -def test_methods_with_args( |
314 | | - test_method: str, |
315 | | - dtype: str, |
316 | | - nums: LIST_TYPE, |
317 | | - expected_value: RESULT, |
318 | | - kwargs: dict, |
319 | | -) -> None: |
320 | | - arr = ul.from_seq(nums, dtype) |
321 | | - result = getattr(arr, test_method)(**kwargs) |
322 | | - check_test_result(dtype, test_method, result, expected_value) |
323 | | - |
324 | | - |
325 | | -@expand_dtypes |
326 | | -@pytest.mark.parametrize( |
327 | | - "test_method, dtype, nums, expected_value, kwargs", |
328 | | - [ |
329 | | - ('__setitem__', 'bool', [True, False], [ |
330 | | - True, True], {'index': 1, 'elem': True}), |
331 | | - ('__setitem__', 'float', [1.0, 2.0], [ |
332 | | - 1.0, 3.0], {'index': 1, 'elem': 3.0}), |
333 | | - ('__setitem__', 'int', [1, 2], [1, 3], {'index': 1, 'elem': 3}), |
334 | | - ('__setitem__', 'string', ['foo', 'bar'], [ |
335 | | - 'foo', 'baz'], {'index': 1, 'elem': 'baz'}), |
336 | | -
|
337 | | - ('append', 'bool', [True], [True, False], {'elem': False}), |
338 | | - ('append', 'float', [1.0], [1.0, 2.0], {'elem': 2.0}), |
339 | | - ('append', 'int', [1], [1, 2], {'elem': 2}), |
340 | | - ('append', 'string', ['foo'], ['foo', 'bar'], {'elem': 'bar'}), |
341 | | -
|
342 | | - ('pop', 'bool', [True, False], [True], {}), |
343 | | - ('pop', 'float', [1.0, 2.0], [1.0], {}), |
344 | | - ('pop', 'int', [1, 2], [1], {}), |
345 | | - ('pop', 'string', ['foo', 'bar'], ['foo'], {}), |
346 | | -
|
347 | | - ('set', 'bool', [True, False], [ |
348 | | - True, True], {'index': 1, 'elem': True}), |
349 | | - ('set', 'float', [1.0, 2.0], [1.0, 3.0], {'index': 1, 'elem': 3.0}), |
350 | | - ('set', 'int', [1, 2], [1, 3], {'index': 1, 'elem': 3}), |
351 | | - ('set', 'string', ['foo', 'bar'], [ |
352 | | - 'foo', 'baz'], {'index': 1, 'elem': 'baz'}), |
353 | 330 | ], |
354 | 331 | ) |
355 | 332 | def test_multable_methods( |
@@ -483,3 +460,41 @@ def test_operators( |
483 | 460 | other = kwargs["other"] |
484 | 461 | result = test_method(arr, other) |
485 | 462 | check_test_result(dtype, test_method, result, expected_value) |
| 463 | + |
| 464 | + |
| 465 | +@expand_dtypes |
| 466 | +@pytest.mark.parametrize( |
| 467 | + "dtype, nums, expected_value", |
| 468 | + [ |
| 469 | + ( |
| 470 | + 'bool', |
| 471 | + [True, False, True, False, True], |
| 472 | + [False, True], |
| 473 | + ), |
| 474 | + ( |
| 475 | + 'float', |
| 476 | + [5.0, 3.0, 2.0, 4.0, 1.0, 3.0], |
| 477 | + [1.0, 2.0, 3.0, 4.0, 5.0], |
| 478 | + ), |
| 479 | + ( |
| 480 | + 'int', |
| 481 | + [5, 3, 2, 4, 1, 3], |
| 482 | + [1, 2, 3, 4, 5], |
| 483 | + ), |
| 484 | + ( |
| 485 | + 'string', |
| 486 | + ['foo', 'bar', 'foo'], |
| 487 | + ['bar', 'foo'], |
| 488 | + ), |
| 489 | + ], |
| 490 | +) |
| 491 | +def test_unique( |
| 492 | + dtype: str, |
| 493 | + nums: LIST_TYPE, |
| 494 | + expected_value: RESULT, |
| 495 | +) -> None: |
| 496 | + test_method = "unique" |
| 497 | + arr = ul.from_seq(nums, dtype) |
| 498 | + result = getattr(arr, test_method)() |
| 499 | + result.sort(True) |
| 500 | + check_test_result(dtype, test_method, result, expected_value) |
0 commit comments