|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# coding=utf-8 |
|
3
|
|
|
"""global docstring""" |
|
4
|
|
|
from __future__ import division, print_function, unicode_literals |
|
5
|
|
|
|
|
6
|
|
|
import os |
|
7
|
|
|
import pytest |
|
8
|
|
|
import tempfile |
|
9
|
|
|
|
|
10
|
|
|
from sacred.config import ConfigScope, ConfigDict |
|
11
|
|
|
from sacred.dependencies import Source, PackageDependency |
|
12
|
|
|
from sacred.ingredient import Ingredient |
|
13
|
|
|
from sacred.utils import CircularDependencyError |
|
14
|
|
|
from sacred.serializer import json |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
@pytest.fixture |
|
18
|
|
|
def ing(): |
|
19
|
|
|
return Ingredient('tickle') |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def test_create_ingredient(ing): |
|
23
|
|
|
assert ing.path == 'tickle' |
|
24
|
|
|
assert ing.doc == __doc__ |
|
25
|
|
|
assert Source.create(__file__) in ing.sources |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def test_capture_function(ing): |
|
29
|
|
|
@ing.capture |
|
30
|
|
|
def foo(something): |
|
31
|
|
|
pass |
|
32
|
|
|
assert foo in ing.captured_functions |
|
33
|
|
|
assert foo.prefix is None |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def test_capture_function_with_prefix(ing): |
|
37
|
|
|
@ing.capture(prefix='bar') |
|
38
|
|
|
def foo(something): |
|
39
|
|
|
pass |
|
40
|
|
|
assert foo in ing.captured_functions |
|
41
|
|
|
assert foo.prefix == 'bar' |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def test_capture_function_twice(ing): |
|
45
|
|
|
@ing.capture |
|
46
|
|
|
def foo(something): |
|
47
|
|
|
pass |
|
48
|
|
|
|
|
49
|
|
|
assert ing.captured_functions == [foo] |
|
50
|
|
|
ing.capture(foo) |
|
51
|
|
|
assert ing.captured_functions == [foo] |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_add_pre_run_hook(ing): |
|
55
|
|
|
@ing.pre_run_hook |
|
56
|
|
|
def foo(something): |
|
57
|
|
|
pass |
|
58
|
|
|
assert foo in ing.pre_run_hooks |
|
59
|
|
|
assert foo in ing.captured_functions |
|
60
|
|
|
assert foo.prefix is None |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def test_add_pre_run_hook_with_prefix(ing): |
|
64
|
|
|
@ing.pre_run_hook(prefix='bar') |
|
65
|
|
|
def foo(something): |
|
66
|
|
|
pass |
|
67
|
|
|
assert foo in ing.pre_run_hooks |
|
68
|
|
|
assert foo in ing.captured_functions |
|
69
|
|
|
assert foo.prefix == 'bar' |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_add_post_run_hook(ing): |
|
73
|
|
|
@ing.post_run_hook |
|
74
|
|
|
def foo(something): |
|
75
|
|
|
pass |
|
76
|
|
|
assert foo in ing.post_run_hooks |
|
77
|
|
|
assert foo in ing.captured_functions |
|
78
|
|
|
assert foo.prefix is None |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
def test_add_post_run_hook_with_prefix(ing): |
|
82
|
|
|
@ing.post_run_hook(prefix='bar') |
|
83
|
|
|
def foo(something): |
|
84
|
|
|
pass |
|
85
|
|
|
assert foo in ing.post_run_hooks |
|
86
|
|
|
assert foo in ing.captured_functions |
|
87
|
|
|
assert foo.prefix == 'bar' |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def test_add_command(ing): |
|
91
|
|
|
@ing.command |
|
92
|
|
|
def foo(a, b): |
|
93
|
|
|
pass |
|
94
|
|
|
|
|
95
|
|
|
assert 'foo' in ing.commands |
|
96
|
|
|
assert ing.commands['foo'] == foo |
|
97
|
|
|
assert foo.prefix is None |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
def test_add_command_with_prefix(ing): |
|
101
|
|
|
@ing.command(prefix='bar') |
|
102
|
|
|
def foo(a, b): |
|
103
|
|
|
pass |
|
104
|
|
|
|
|
105
|
|
|
assert 'foo' in ing.commands |
|
106
|
|
|
assert ing.commands['foo'] == foo |
|
107
|
|
|
assert foo.prefix == 'bar' |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
def test_add_unobserved_command(ing): |
|
111
|
|
|
@ing.command(unobserved=True) |
|
112
|
|
|
def foo(a, b): |
|
113
|
|
|
pass |
|
114
|
|
|
|
|
115
|
|
|
assert 'foo' in ing.commands |
|
116
|
|
|
assert ing.commands['foo'] == foo |
|
117
|
|
|
assert foo.unobserved is True |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
def test_add_config_hook(ing): |
|
121
|
|
|
def foo(config, command_name, logger): |
|
122
|
|
|
pass |
|
123
|
|
|
ch = ing.config_hook(foo) |
|
124
|
|
|
assert ch == foo |
|
125
|
|
|
assert foo in ing.config_hooks |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
def test_add_config(ing): |
|
129
|
|
|
@ing.config |
|
130
|
|
|
def cfg(): |
|
131
|
|
|
pass |
|
132
|
|
|
|
|
133
|
|
|
assert isinstance(cfg, ConfigScope) |
|
134
|
|
|
assert cfg in ing.configurations |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
def test_add_named_config(ing): |
|
138
|
|
|
@ing.named_config |
|
139
|
|
|
def foo(): |
|
140
|
|
|
pass |
|
141
|
|
|
assert isinstance(foo, ConfigScope) |
|
142
|
|
|
assert 'foo' in ing.named_configs |
|
143
|
|
|
assert ing.named_configs['foo'] == foo |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
def test_add_config_hook_with_invalid_signature_raises(ing): |
|
147
|
|
|
with pytest.raises(ValueError): |
|
148
|
|
|
@ing.config_hook |
|
149
|
|
|
def foo(wrong, signature): |
|
150
|
|
|
pass |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
def test_add_config_dict(ing): |
|
154
|
|
|
ing.add_config({'foo': 12, 'bar': 4}) |
|
155
|
|
|
assert len(ing.configurations) == 1 |
|
156
|
|
|
assert isinstance(ing.configurations[0], ConfigDict) |
|
157
|
|
|
assert ing.configurations[0]() == {'foo': 12, 'bar': 4} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
def test_add_config_kwargs(ing): |
|
161
|
|
|
ing.add_config(foo=18, bar=3) |
|
162
|
|
|
assert len(ing.configurations) == 1 |
|
163
|
|
|
assert isinstance(ing.configurations[0], ConfigDict) |
|
164
|
|
|
assert ing.configurations[0]() == {'foo': 18, 'bar': 3} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
def test_add_config_kwargs_and_dict_raises(ing): |
|
168
|
|
|
with pytest.raises(ValueError): |
|
169
|
|
|
ing.add_config({'foo': 12}, bar=3) |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
def test_add_config_empty_raises(ing): |
|
173
|
|
|
with pytest.raises(ValueError): |
|
174
|
|
|
ing.add_config() |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
def test_add_config_non_dict_raises(ing): |
|
178
|
|
|
with pytest.raises(TypeError): |
|
179
|
|
|
ing.add_config(12) |
|
180
|
|
|
|
|
181
|
|
|
with pytest.raises(TypeError): |
|
182
|
|
|
ing.add_config(True) |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
def test_add_config_file(ing): |
|
186
|
|
|
handle, f_name = tempfile.mkstemp(suffix='.json') |
|
187
|
|
|
f = os.fdopen(handle, "w") |
|
188
|
|
|
f.write(json.encode({'foo': 15, 'bar': 7})) |
|
189
|
|
|
f.close() |
|
190
|
|
|
ing.add_config(f_name) |
|
191
|
|
|
|
|
192
|
|
|
assert len(ing.configurations) == 1 |
|
193
|
|
|
assert isinstance(ing.configurations[0], ConfigDict) |
|
194
|
|
|
assert ing.configurations[0]() == {'foo': 15, 'bar': 7} |
|
195
|
|
|
os.remove(f_name) |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
def test_add_config_file_nonexisting_raises(ing): |
|
199
|
|
|
with pytest.raises(IOError): |
|
200
|
|
|
ing.add_config("nonexistens.json") |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
def test_add_source_file(ing): |
|
204
|
|
|
handle, f_name = tempfile.mkstemp(suffix='.py') |
|
205
|
|
|
f = os.fdopen(handle, "w") |
|
206
|
|
|
f.write("print('Hello World')") |
|
207
|
|
|
f.close() |
|
208
|
|
|
ing.add_source_file(f_name) |
|
209
|
|
|
assert Source.create(f_name) in ing.sources |
|
210
|
|
|
os.remove(f_name) |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
def test_add_source_file_nonexisting_raises(ing): |
|
214
|
|
|
with pytest.raises(ValueError): |
|
215
|
|
|
ing.add_source_file('nonexisting.py') |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
def test_add_package_dependency(ing): |
|
219
|
|
|
ing.add_package_dependency('django', '1.8.2') |
|
220
|
|
|
assert PackageDependency('django', '1.8.2') in ing.dependencies |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
def test_add_package_dependency_invalid_version_raises(ing): |
|
224
|
|
|
with pytest.raises(ValueError): |
|
225
|
|
|
ing.add_package_dependency('django', 'foobar') |
|
226
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
def test_get_experiment_info(ing): |
|
229
|
|
|
info = ing.get_experiment_info() |
|
230
|
|
|
assert info['name'] == 'tickle' |
|
231
|
|
|
assert 'dependencies' in info |
|
232
|
|
|
assert 'sources' in info |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
def test_get_experiment_info_circular_dependency_raises(ing): |
|
236
|
|
|
ing2 = Ingredient('other', ingredients=[ing]) |
|
237
|
|
|
ing.ingredients = [ing2] |
|
238
|
|
|
with pytest.raises(CircularDependencyError): |
|
239
|
|
|
ing.get_experiment_info() |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
def test_gather_commands(ing): |
|
243
|
|
|
ing2 = Ingredient('other', ingredients=[ing]) |
|
244
|
|
|
|
|
245
|
|
|
@ing.command |
|
246
|
|
|
def foo(): |
|
247
|
|
|
pass |
|
248
|
|
|
|
|
249
|
|
|
@ing2.command |
|
250
|
|
|
def bar(): |
|
251
|
|
|
pass |
|
252
|
|
|
|
|
253
|
|
|
commands = list(ing2.gather_commands()) |
|
254
|
|
|
assert ('other.bar', bar) in commands |
|
255
|
|
|
assert ('tickle.foo', foo) in commands |
|
256
|
|
|
|