forked from IDSIA/sacred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
executable file
·218 lines (168 loc) · 5.7 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
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
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# coding=utf-8
import pytest
from sacred.utils import (
PATHCHANGE,
convert_to_nested_dict,
get_by_dotted_path,
is_prefix,
iter_prefixes,
iterate_flattened,
iterate_flattened_separately,
join_paths,
recursive_update,
set_by_dotted_path,
get_inheritors,
convert_camel_case_to_snake_case,
apply_backspaces_and_linefeeds,
module_exists,
module_is_in_cache,
get_package_version,
parse_version,
rel_path,
)
def test_recursive_update():
d = {"a": {"b": 1}}
res = recursive_update(d, {"c": 2, "a": {"d": 3}})
assert d is res
assert res == {"a": {"b": 1, "d": 3}, "c": 2}
def test_iterate_flattened_separately():
d = {
"a1": 1,
"b2": {"bar": "foo", "foo": "bar"},
"c1": "f",
"d1": [1, 2, 3],
"e2": {},
}
res = list(iterate_flattened_separately(d, ["foo", "bar"]))
assert res == [
("a1", 1),
("c1", "f"),
("d1", [1, 2, 3]),
("e2", {}),
("b2", PATHCHANGE),
("b2.foo", "bar"),
("b2.bar", "foo"),
]
def test_iterate_flattened():
d = {"a": {"aa": 1, "ab": {"aba": 8}}, "b": 3}
assert list(iterate_flattened(d)) == [("a.aa", 1), ("a.ab.aba", 8), ("b", 3)]
def test_set_by_dotted_path():
d = {"foo": {"bar": 7}}
set_by_dotted_path(d, "foo.bar", 10)
assert d == {"foo": {"bar": 10}}
def test_set_by_dotted_path_creates_missing_dicts():
d = {"foo": {"bar": 7}}
set_by_dotted_path(d, "foo.d.baz", 3)
assert d == {"foo": {"bar": 7, "d": {"baz": 3}}}
def test_get_by_dotted_path():
assert get_by_dotted_path({"a": 12}, "a") == 12
assert get_by_dotted_path({"a": 12}, "") == {"a": 12}
assert get_by_dotted_path({"foo": {"a": 12}}, "foo.a") == 12
assert get_by_dotted_path({"foo": {"a": 12}}, "foo.b") is None
def test_iter_prefixes():
assert list(iter_prefixes("foo.bar.baz")) == ["foo", "foo.bar", "foo.bar.baz"]
def test_join_paths():
assert join_paths() == ""
assert join_paths("foo") == "foo"
assert join_paths("foo", "bar") == "foo.bar"
assert join_paths("a", "b", "c", "d") == "a.b.c.d"
assert join_paths("", "b", "", "d") == "b.d"
assert join_paths("a.b", "c.d.e") == "a.b.c.d.e"
assert join_paths("a.b.", "c.d.e") == "a.b.c.d.e"
def test_is_prefix():
assert is_prefix("", "foo")
assert is_prefix("foo", "foo.bar")
assert is_prefix("foo.bar", "foo.bar.baz")
assert not is_prefix("a", "foo.bar")
assert not is_prefix("a.bar", "foo.bar")
assert not is_prefix("foo.b", "foo.bar")
assert not is_prefix("foo.bar", "foo.bar")
def test_convert_to_nested_dict():
dotted_dict = {"foo.bar": 8, "foo.baz": 7}
assert convert_to_nested_dict(dotted_dict) == {"foo": {"bar": 8, "baz": 7}}
def test_convert_to_nested_dict_nested():
dotted_dict = {"a.b": {"foo.bar": 8}, "a.b.foo.baz": 7}
assert convert_to_nested_dict(dotted_dict) == {
"a": {"b": {"foo": {"bar": 8, "baz": 7}}}
}
def test_get_inheritors():
class A:
pass
class B(A):
pass
class C(B):
pass
class D(A):
pass
class E:
pass
assert get_inheritors(A) == {B, C, D}
@pytest.mark.parametrize(
"name,expected",
[
("CamelCase", "camel_case"),
("snake_case", "snake_case"),
("CamelCamelCase", "camel_camel_case"),
("Camel2Camel2Case", "camel2_camel2_case"),
("getHTTPResponseCode", "get_http_response_code"),
("get2HTTPResponseCode", "get2_http_response_code"),
("HTTPResponseCode", "http_response_code"),
("HTTPResponseCodeXYZ", "http_response_code_xyz"),
],
)
def test_convert_camel_case_to_snake_case(name, expected):
assert convert_camel_case_to_snake_case(name) == expected
@pytest.mark.parametrize(
"text,expected",
[
("", ""),
("\b", ""),
("\r", "\r"),
("\r\n", "\n"),
("ab\bc", "ac"),
("\ba", "a"),
("ab\nc\b\bd", "ab\nd"),
("abc\rdef", "def"),
("abc\r", "abc\r"),
("abc\rd", "dbc"),
("abc\r\nd", "abc\nd"),
("abc\ndef\rg", "abc\ngef"),
("abc\ndef\r\rg", "abc\ngef"),
("abcd\refg\r", "efgd\r"),
("abcd\refg\r\n", "efgd\n"),
],
)
def test_apply_backspaces_and_linefeeds(text, expected):
assert apply_backspaces_and_linefeeds(text) == expected
def test_module_exists_base_level_modules():
assert module_exists("pytest")
assert not module_exists("clearly_non_existing_module_name")
def test_module_exists_does_not_import_module():
assert module_exists("tests.donotimport")
def test_module_is_in_cache():
assert module_is_in_cache("pytest")
assert module_is_in_cache("pkgutil")
assert not module_is_in_cache("does_not_even_exist")
def test_get_package_version():
package_version = get_package_version("pytest")
assert str(package_version) == "4.3.0"
def test_parse_version():
parsed_version = parse_version("4.3.0")
assert str(parsed_version) == "4.3.0"
def test_get_package_version_comparison():
package_version = get_package_version("pytest")
current_version = parse_version("4.3.0")
old_version = parse_version("4.2.1")
new_version = parse_version("4.4.1")
assert package_version == current_version
assert not package_version < current_version
assert not package_version > current_version
assert package_version <= new_version
assert package_version >= old_version
def test_rel_path():
assert rel_path("", "foo.bar.baz") == "foo.bar.baz"
assert rel_path("foo", "foo.bar.baz") == "bar.baz"
assert rel_path("foo.bar", "foo.bar.baz") == "baz"
assert rel_path("foo.bar.baz", "foo.bar.baz") == ""
assert rel_path("", "") == ""