1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# coding=utf-8 |
3
|
|
|
from __future__ import division, print_function, unicode_literals |
4
|
|
|
"""Global Docstring""" |
5
|
|
|
|
6
|
|
|
from mock import patch |
7
|
|
|
import pytest |
8
|
|
|
import sys |
9
|
|
|
|
10
|
|
|
from sacred.experiment import Experiment |
11
|
|
|
from sacred.utils import apply_backspaces_and_linefeeds |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
@pytest.fixture |
15
|
|
|
def ex(): |
16
|
|
|
return Experiment('ator3000') |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
def test_main(ex): |
20
|
|
|
@ex.main |
21
|
|
|
def foo(): |
22
|
|
|
pass |
23
|
|
|
|
24
|
|
|
assert 'foo' in ex.commands |
25
|
|
|
assert ex.commands['foo'] == foo |
26
|
|
|
assert ex.default_command == 'foo' |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_automain_imported(ex): |
30
|
|
|
main_called = [False] |
31
|
|
|
|
32
|
|
|
with patch.object(sys, 'argv', ['test.py']): |
33
|
|
|
|
34
|
|
|
@ex.automain |
35
|
|
|
def foo(): |
36
|
|
|
main_called[0] = True |
37
|
|
|
|
38
|
|
|
assert 'foo' in ex.commands |
39
|
|
|
assert ex.commands['foo'] == foo |
40
|
|
|
assert ex.default_command == 'foo' |
41
|
|
|
assert main_called[0] is False |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_automain_script_runs_main(ex): |
45
|
|
|
global __name__ |
46
|
|
|
oldname = __name__ |
47
|
|
|
main_called = [False] |
48
|
|
|
|
49
|
|
|
try: |
50
|
|
|
__name__ = '__main__' |
51
|
|
|
with patch.object(sys, 'argv', ['test.py']): |
52
|
|
|
@ex.automain |
53
|
|
|
def foo(): |
54
|
|
|
main_called[0] = True |
55
|
|
|
|
56
|
|
|
assert 'foo' in ex.commands |
57
|
|
|
assert ex.commands['foo'] == foo |
58
|
|
|
assert ex.default_command == 'foo' |
59
|
|
|
assert main_called[0] is True |
60
|
|
|
finally: |
61
|
|
|
__name__ = oldname |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
def test_fails_on_unused_config_updates(ex): |
65
|
|
|
@ex.config |
66
|
|
|
def cfg(): |
67
|
|
|
a = 1 |
68
|
|
|
c = 3 |
69
|
|
|
|
70
|
|
|
@ex.main |
71
|
|
|
def foo(a, b=2): |
72
|
|
|
return a + b |
73
|
|
|
|
74
|
|
|
# normal config updates work |
75
|
|
|
assert ex.run(config_updates={'a': 3}).result == 5 |
76
|
|
|
# not in config but used works |
77
|
|
|
assert ex.run(config_updates={'b': 8}).result == 9 |
78
|
|
|
# unused but in config updates work |
79
|
|
|
assert ex.run(config_updates={'c': 9}).result == 3 |
80
|
|
|
|
81
|
|
|
# unused config updates raise |
82
|
|
|
with pytest.raises(KeyError): |
83
|
|
|
ex.run(config_updates={'d': 3}) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_fails_on_nested_unused_config_updates(ex): |
87
|
|
|
@ex.config |
88
|
|
|
def cfg(): |
89
|
|
|
a = {'b': 1} |
90
|
|
|
d = {'e': 3} |
91
|
|
|
|
92
|
|
|
@ex.main |
93
|
|
|
def foo(a): |
94
|
|
|
return a['b'] |
95
|
|
|
|
96
|
|
|
# normal config updates work |
97
|
|
|
assert ex.run(config_updates={'a': {'b': 2}}).result == 2 |
98
|
|
|
# not in config but parent is works |
99
|
|
|
assert ex.run(config_updates={'a': {'c': 5}}).result == 1 |
100
|
|
|
# unused but in config works |
101
|
|
|
assert ex.run(config_updates={'d': {'e': 7}}).result == 1 |
102
|
|
|
|
103
|
|
|
# unused nested config updates raise |
104
|
|
|
with pytest.raises(KeyError): |
105
|
|
|
ex.run(config_updates={'d': {'f': 3}}) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def test_considers_captured_functions_for_fail_on_unused_config(ex): |
109
|
|
|
@ex.config |
110
|
|
|
def cfg(): |
111
|
|
|
a = 1 |
112
|
|
|
|
113
|
|
|
@ex.capture |
114
|
|
|
def transmogrify(a, b=0): |
115
|
|
|
return a + b |
116
|
|
|
|
117
|
|
|
@ex.main |
118
|
|
|
def foo(): |
119
|
|
|
return transmogrify() |
120
|
|
|
|
121
|
|
|
assert ex.run(config_updates={'a': 7}).result == 7 |
122
|
|
|
assert ex.run(config_updates={'b': 3}).result == 4 |
123
|
|
|
|
124
|
|
|
with pytest.raises(KeyError): |
125
|
|
|
ex.run(config_updates={'c': 3}) |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def test_considers_prefix_for_fail_on_unused_config(ex): |
129
|
|
|
@ex.config |
130
|
|
|
def cfg(): |
131
|
|
|
a = {'b': 1} |
132
|
|
|
|
133
|
|
|
@ex.capture(prefix='a') |
134
|
|
|
def transmogrify(b): |
135
|
|
|
return b |
136
|
|
|
|
137
|
|
|
@ex.main |
138
|
|
|
def foo(): |
139
|
|
|
return transmogrify() |
140
|
|
|
|
141
|
|
|
assert ex.run(config_updates={'a': {'b': 3}}).result == 3 |
142
|
|
|
|
143
|
|
|
with pytest.raises(KeyError): |
144
|
|
|
ex.run(config_updates={'b': 5}) |
145
|
|
|
|
146
|
|
|
with pytest.raises(KeyError): |
147
|
|
|
ex.run(config_updates={'a': {'c': 5}}) |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
def test_non_existing_prefix_is_treatet_as_empty_dict(ex): |
151
|
|
|
@ex.capture(prefix='nonexisting') |
152
|
|
|
def transmogrify(b=10): |
153
|
|
|
return b |
154
|
|
|
|
155
|
|
|
@ex.main |
156
|
|
|
def foo(): |
157
|
|
|
return transmogrify() |
158
|
|
|
|
159
|
|
|
assert ex.run().result == 10 |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def test_using_a_named_config(ex): |
163
|
|
|
@ex.config |
164
|
|
|
def cfg(): |
165
|
|
|
a = 1 |
166
|
|
|
|
167
|
|
|
@ex.named_config |
168
|
|
|
def ncfg(): |
169
|
|
|
a = 10 |
170
|
|
|
|
171
|
|
|
@ex.main |
172
|
|
|
def run(a): |
173
|
|
|
return a |
174
|
|
|
|
175
|
|
|
assert ex.run().result == 1 |
176
|
|
|
assert ex.run(named_configs=['ncfg']).result == 10 |
177
|
|
|
|
178
|
|
|
|
179
|
|
View Code Duplication |
def test_empty_dict_named_config(ex): |
|
|
|
|
180
|
|
|
@ex.named_config |
181
|
|
|
def ncfg(): |
182
|
|
|
empty_dict = {} |
183
|
|
|
nested_empty_dict = {'k1': {'k2': {}}} |
184
|
|
|
|
185
|
|
|
@ex.automain |
186
|
|
|
def main(empty_dict=1, nested_empty_dict=2): |
187
|
|
|
return empty_dict, nested_empty_dict |
188
|
|
|
|
189
|
|
|
assert ex.run().result == (1, 2) |
190
|
|
|
assert ex.run(named_configs=['ncfg']).result == ({}, {'k1': {'k2': {}}}) |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
def test_captured_out_filter(ex, capsys): |
194
|
|
|
@ex.main |
195
|
|
|
def run_print_mock_progress(): |
196
|
|
|
sys.stdout.write('progress 0') |
197
|
|
|
sys.stdout.flush() |
198
|
|
|
for i in range(10): |
199
|
|
|
sys.stdout.write('\b') |
200
|
|
|
sys.stdout.write("{}".format(i)) |
201
|
|
|
sys.stdout.flush() |
202
|
|
|
|
203
|
|
|
ex.captured_out_filter = apply_backspaces_and_linefeeds |
204
|
|
|
# disable logging and set capture mode to python |
205
|
|
|
options = {'--loglevel': 'CRITICAL', '--capture': 'sys'} |
206
|
|
|
with capsys.disabled(): |
207
|
|
|
assert ex.run(options=options).captured_out == 'progress 9' |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
def test_adding_option_hooks(ex): |
211
|
|
|
@ex.option_hook |
212
|
|
|
def hook(options): |
213
|
|
|
pass |
214
|
|
|
|
215
|
|
|
@ex.option_hook |
216
|
|
|
def hook2(options): |
217
|
|
|
pass |
218
|
|
|
|
219
|
|
|
assert hook in ex.option_hooks |
220
|
|
|
assert hook2 in ex.option_hooks |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
def test_option_hooks_without_options_arg_raises(ex): |
224
|
|
|
with pytest.raises(KeyError): |
225
|
|
|
@ex.option_hook |
226
|
|
|
def invalid_hook(wrong_arg_name): |
227
|
|
|
pass |
228
|
|
|
|