-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_utils.py
89 lines (61 loc) · 2.04 KB
/
test_utils.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
"""
Tests for builders
"""
from datetime import datetime
from time import sleep
import pytest
from maggma.utils import (
Timeout, # dt_to_isoformat_ceil_ms,; isostr_to_dt,
dynamic_import,
grouper,
primed,
recursive_update,
to_dt,
to_isoformat_ceil_ms,
)
def test_recursiveupdate():
d = {"a": {"b": 3}, "c": [4]}
recursive_update(d, {"c": [5]})
assert d["c"] == [5]
recursive_update(d, {"a": {"b": 5}})
assert d["a"]["b"] == 5
recursive_update(d, {"a": {"b": [6]}})
assert d["a"]["b"] == [6]
recursive_update(d, {"a": {"b": [7]}})
assert d["a"]["b"] == [7]
def test_timeout():
def takes_too_long():
with Timeout(seconds=1):
sleep(2)
with pytest.raises(TimeoutError):
takes_too_long()
def test_primed():
global is_primed # noqa: PLW0603
is_primed = False
def unprimed_iter():
global is_primed # noqa: PLW0603
is_primed = True
yield from range(10)
iterator = unprimed_iter()
# iterator is still unprimed
assert is_primed is False
iterator = primed(iterator)
assert is_primed is True
assert list(iterator) == list(range(10))
# test stop iteration
with pytest.raises(StopIteration):
next(primed(iterator))
def test_datetime_utils():
assert to_isoformat_ceil_ms(datetime(2019, 12, 13, 0, 23, 11, 9515)) == "2019-12-13T00:23:11.010"
assert to_isoformat_ceil_ms("2019-12-13T00:23:11.010") == "2019-12-13T00:23:11.010"
assert to_dt("2019-12-13T00:23:11.010") == datetime(2019, 12, 13, 0, 23, 11, 10000)
assert to_dt(datetime(2019, 12, 13, 0, 23, 11, 10000)) == datetime(2019, 12, 13, 0, 23, 11, 10000)
def test_dynamic_import():
assert dynamic_import("maggma.stores", "MongoStore").__name__ == "MongoStore"
def test_grouper():
my_iterable = list(range(100))
assert len(list(grouper(my_iterable, 10))) == 10
my_iterable = [*list(range(100)), None]
my_groups = list(grouper(my_iterable, 10))
assert len(my_groups) == 11
assert len(my_groups[10]) == 1