1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# coding=utf-8 |
3
|
|
|
from __future__ import division, print_function, unicode_literals |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from sacred.utils import (PATHCHANGE, convert_to_nested_dict, |
8
|
|
|
get_by_dotted_path, is_prefix, is_subdir, |
9
|
|
|
iter_path_splits, iter_prefixes, iterate_flattened, |
10
|
|
|
iterate_flattened_separately, join_paths, |
11
|
|
|
recursive_update, set_by_dotted_path, get_inheritors, |
12
|
|
|
convert_camel_case_to_snake_case, |
13
|
|
|
apply_backspaces_and_linefeeds, module_exists, |
14
|
|
|
module_is_imported, module_is_in_cache, rel_path) |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def test_recursive_update(): |
18
|
|
|
d = {'a': {'b': 1}} |
19
|
|
|
res = recursive_update(d, {'c': 2, 'a': {'d': 3}}) |
20
|
|
|
assert d is res |
21
|
|
|
assert res == {'a': {'b': 1, 'd': 3}, 'c': 2} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_iterate_flattened_separately(): |
25
|
|
|
d = {'a1': 1, |
26
|
|
|
'b2': {'bar': 'foo', 'foo': 'bar'}, |
27
|
|
|
'c1': 'f', |
28
|
|
|
'd1': [1, 2, 3], |
29
|
|
|
'e2': {}} |
30
|
|
|
res = list(iterate_flattened_separately(d, ['foo', 'bar'])) |
31
|
|
|
assert res == [('a1', 1), ('c1', 'f'), ('d1', [1, 2, 3]), ('e2', {}), |
32
|
|
|
('b2', PATHCHANGE), ('b2.foo', 'bar'), ('b2.bar', 'foo')] |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_iterate_flattened(): |
36
|
|
|
d = {'a': {'aa': 1, 'ab': {'aba': 8}}, 'b': 3} |
37
|
|
|
assert list(iterate_flattened(d)) == \ |
38
|
|
|
[('a.aa', 1), ('a.ab.aba', 8), ('b', 3)] |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def test_set_by_dotted_path(): |
42
|
|
|
d = {'foo': {'bar': 7}} |
43
|
|
|
set_by_dotted_path(d, 'foo.bar', 10) |
44
|
|
|
assert d == {'foo': {'bar': 10}} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_set_by_dotted_path_creates_missing_dicts(): |
48
|
|
|
d = {'foo': {'bar': 7}} |
49
|
|
|
set_by_dotted_path(d, 'foo.d.baz', 3) |
50
|
|
|
assert d == {'foo': {'bar': 7, 'd': {'baz': 3}}} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_get_by_dotted_path(): |
54
|
|
|
assert get_by_dotted_path({'a': 12}, 'a') == 12 |
55
|
|
|
assert get_by_dotted_path({'a': 12}, '') == {'a': 12} |
56
|
|
|
assert get_by_dotted_path({'foo': {'a': 12}}, 'foo.a') == 12 |
57
|
|
|
assert get_by_dotted_path({'foo': {'a': 12}}, 'foo.b') is None |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def test_iter_path_splits(): |
61
|
|
|
assert list(iter_path_splits('foo.bar.baz')) ==\ |
62
|
|
|
[('', 'foo.bar.baz'), |
63
|
|
|
('foo', 'bar.baz'), |
64
|
|
|
('foo.bar', 'baz')] |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_iter_prefixes(): |
68
|
|
|
assert list(iter_prefixes('foo.bar.baz')) == \ |
69
|
|
|
['foo', 'foo.bar', 'foo.bar.baz'] |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_join_paths(): |
73
|
|
|
assert join_paths() == '' |
74
|
|
|
assert join_paths('foo') == 'foo' |
75
|
|
|
assert join_paths('foo', 'bar') == 'foo.bar' |
76
|
|
|
assert join_paths('a', 'b', 'c', 'd') == 'a.b.c.d' |
77
|
|
|
assert join_paths('', 'b', '', 'd') == 'b.d' |
78
|
|
|
assert join_paths('a.b', 'c.d.e') == 'a.b.c.d.e' |
79
|
|
|
assert join_paths('a.b.', 'c.d.e') == 'a.b.c.d.e' |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def test_is_prefix(): |
83
|
|
|
assert is_prefix('', 'foo') |
84
|
|
|
assert is_prefix('foo', 'foo.bar') |
85
|
|
|
assert is_prefix('foo.bar', 'foo.bar.baz') |
86
|
|
|
|
87
|
|
|
assert not is_prefix('a', 'foo.bar') |
88
|
|
|
assert not is_prefix('a.bar', 'foo.bar') |
89
|
|
|
assert not is_prefix('foo.b', 'foo.bar') |
90
|
|
|
assert not is_prefix('foo.bar', 'foo.bar') |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
def test_convert_to_nested_dict(): |
94
|
|
|
dotted_dict = {'foo.bar': 8, 'foo.baz': 7} |
95
|
|
|
assert convert_to_nested_dict(dotted_dict) == {'foo': {'bar': 8, 'baz': 7}} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def test_convert_to_nested_dict_nested(): |
99
|
|
|
dotted_dict = {'a.b': {'foo.bar': 8}, 'a.b.foo.baz': 7} |
100
|
|
|
assert convert_to_nested_dict(dotted_dict) == \ |
101
|
|
|
{'a': {'b': {'foo': {'bar': 8, 'baz': 7}}}} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
@pytest.mark.parametrize('path,parent,expected', [ |
105
|
|
|
('/var/test2', '/var/test', False), |
106
|
|
|
('/var/test', '/var/test2', False), |
107
|
|
|
('var/test2', 'var/test', False), |
108
|
|
|
('var/test', 'var/test2', False), |
109
|
|
|
('/var/test/sub', '/var/test', True), |
110
|
|
|
('/var/test', '/var/test/sub', False), |
111
|
|
|
('var/test/sub', 'var/test', True), |
112
|
|
|
('var/test', 'var/test', True), |
113
|
|
|
('var/test', 'var/test/fake_sub/..', True), |
114
|
|
|
('var/test/sub/sub2/sub3/../..', 'var/test', True), |
115
|
|
|
('var/test/sub', 'var/test/fake_sub/..', True), |
116
|
|
|
('var/test', 'var/test/sub', False) |
117
|
|
|
]) |
118
|
|
|
def test_is_subdirectory(path, parent, expected): |
119
|
|
|
assert is_subdir(path, parent) == expected |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def test_get_inheritors(): |
123
|
|
|
class A(object): |
124
|
|
|
pass |
125
|
|
|
|
126
|
|
|
class B(A): |
127
|
|
|
pass |
128
|
|
|
|
129
|
|
|
class C(B): |
130
|
|
|
pass |
131
|
|
|
|
132
|
|
|
class D(A): |
133
|
|
|
pass |
134
|
|
|
|
135
|
|
|
class E(object): |
136
|
|
|
pass |
137
|
|
|
|
138
|
|
|
assert get_inheritors(A) == {B, C, D} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
@pytest.mark.parametrize('name,expected', [ |
142
|
|
|
('CamelCase', 'camel_case'), |
143
|
|
|
('snake_case', 'snake_case'), |
144
|
|
|
('CamelCamelCase', 'camel_camel_case'), |
145
|
|
|
('Camel2Camel2Case', 'camel2_camel2_case'), |
146
|
|
|
('getHTTPResponseCode', 'get_http_response_code'), |
147
|
|
|
('get2HTTPResponseCode', 'get2_http_response_code'), |
148
|
|
|
('HTTPResponseCode', 'http_response_code'), |
149
|
|
|
('HTTPResponseCodeXYZ', 'http_response_code_xyz') |
150
|
|
|
]) |
151
|
|
|
def test_convert_camel_case_to_snake_case(name, expected): |
152
|
|
|
assert convert_camel_case_to_snake_case(name) == expected |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
@pytest.mark.parametrize('text,expected', [ |
156
|
|
|
('', ''), |
157
|
|
|
('\b', ''), |
158
|
|
|
('\r', '\r'), |
159
|
|
|
('\r\n', '\n'), |
160
|
|
|
('ab\bc', 'ac'), |
161
|
|
|
('\ba', 'a'), |
162
|
|
|
('ab\nc\b\bd', 'ab\nd'), |
163
|
|
|
('abc\rdef', 'def'), |
164
|
|
|
('abc\r', 'abc\r'), |
165
|
|
|
('abc\rd', 'dbc'), |
166
|
|
|
('abc\r\nd', 'abc\nd'), |
167
|
|
|
('abc\ndef\rg', 'abc\ngef'), |
168
|
|
|
('abc\ndef\r\rg', 'abc\ngef'), |
169
|
|
|
('abcd\refg\r', 'efgd\r'), |
170
|
|
|
('abcd\refg\r\n', 'efgd\n') |
171
|
|
|
]) |
172
|
|
|
def test_apply_backspaces_and_linefeeds(text, expected): |
173
|
|
|
assert apply_backspaces_and_linefeeds(text) == expected |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
def test_module_exists_base_level_modules(): |
177
|
|
|
assert module_exists('pytest') |
178
|
|
|
assert not module_exists('clearly_non_existing_module_name') |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
def test_module_exists_does_not_import_module(): |
182
|
|
|
assert module_exists('tests.donotimport') |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
def test_module_is_in_cache(): |
186
|
|
|
assert module_is_in_cache('pytest') |
187
|
|
|
assert module_is_in_cache('pkgutil') |
188
|
|
|
assert not module_is_in_cache('does_not_even_exist') |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
def test_module_is_imported(): |
192
|
|
|
globs = globals() |
193
|
|
|
assert module_is_imported('pytest', scope=globs) |
194
|
|
|
assert not module_is_imported('pkgutil', scope=globs) |
195
|
|
|
assert not module_is_imported('does_not_even_exist', scope=globs) |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def test_module_is_imported_uses_caller_globals_by_default(): |
199
|
|
|
assert module_is_imported('pytest') |
200
|
|
|
assert not module_is_imported('pkgutil') |
201
|
|
|
assert not module_is_imported('does_not_even_exist') |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
def test_rel_path(): |
205
|
|
|
assert rel_path('', 'foo.bar.baz') == 'foo.bar.baz' |
206
|
|
|
assert rel_path('foo', 'foo.bar.baz') == 'bar.baz' |
207
|
|
|
assert rel_path('foo.bar', 'foo.bar.baz') == 'baz' |
208
|
|
|
assert rel_path('foo.bar.baz', 'foo.bar.baz') == '' |
209
|
|
|
assert rel_path('', '') == '' |
210
|
|
|
|