-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_utils.py
96 lines (68 loc) · 2.1 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
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
import datetime
import collections
import pytest
from berserk import utils
TIME_FMT = '%Y-%m-%dT%H:%M:%S.%fZ'
Case = collections.namedtuple('Case', 'dt seconds millis text')
@pytest.fixture
def time_case():
dt = datetime.datetime(
2017, 12, 28, 23, 52, 30, tzinfo=datetime.timezone.utc
)
ts = dt.timestamp()
return Case(dt, ts, ts * 1000, dt.strftime(TIME_FMT))
def test_to_millis(time_case):
assert utils.to_millis(time_case.dt) == time_case.millis
def test_datetime_from_seconds(time_case):
assert utils.datetime_from_seconds(time_case.seconds) == time_case.dt
def test_datetime_from_millis(time_case):
assert utils.datetime_from_millis(time_case.millis) == time_case.dt
def test_datetime_from_str(time_case):
assert utils.datetime_from_str(time_case.text) == time_case.dt
def test_inner():
convert = utils.inner(lambda v: 2 * v, 'x', 'y')
result = convert({'x': 42})
assert result == {'x': 84}
def test_noop():
assert 'foo' == utils.noop('foo')
@pytest.fixture
def adapter_mapping():
return {
'foo_bar': 'foo.bar',
'baz': 'baz',
'qux': 'foo.qux',
'quux': 'foo.quux',
'corgeGrault': 'foo.corge.grault',
'corgeGarply': 'foo.corge.garply',
}
@pytest.fixture
def data_to_adapt():
return {
'foo': {
'bar': 'one',
'qux': 'three',
'corge': {'grault': 'four', 'garply': None},
},
'baz': 'two',
}
def test_adapt_with_fill(adapter_mapping, data_to_adapt):
adapt = utils.build_adapter(adapter_mapping)
default = object()
assert adapt(data_to_adapt, fill=True, default=default) == {
'foo_bar': 'one',
'baz': 'two',
'qux': 'three',
'quux': default,
'corgeGrault': 'four',
'corgeGarply': None,
}
def test_adapt(adapter_mapping, data_to_adapt):
adapt = utils.build_adapter(adapter_mapping)
assert adapt(data_to_adapt) == {
'foo_bar': 'one',
'baz': 'two',
'qux': 'three',
'corgeGrault': 'four',
'corgeGarply': None,
}