Completed
Push — master ( 2d5fd3...e2acfa )
by Klaus
01:03
created

tests.test_fails_on_unused_config_updates()   D

Complexity

Conditions 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 8
dl 0
loc 29
rs 4
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
12
13
@pytest.fixture
14
def ex():
15
    return Experiment('ator3000')
16
17
18
def test_main(ex):
19
    @ex.main
20
    def foo():
21
        pass
22
23
    assert 'foo' in ex.commands
24
    assert ex.commands['foo'] == foo
25
    assert ex.default_command == 'foo'
26
27
28
def test_automain_imported(ex):
29
    main_called = [False]
30
31
    with patch.object(sys, 'argv', ['test.py']):
32
33
        @ex.automain
34
        def foo():
35
            main_called[0] = True
36
37
        assert 'foo' in ex.commands
38
        assert ex.commands['foo'] == foo
39
        assert ex.default_command == 'foo'
40
        assert main_called[0] is False
41
42
43
def test_automain_script_runs_main(ex):
44
    global __name__
45
    oldname = __name__
46
    main_called = [False]
47
48
    try:
49
        __name__ = '__main__'
50
        with patch.object(sys, 'argv', ['test.py']):
51
            @ex.automain
52
            def foo():
53
                main_called[0] = True
54
55
            assert 'foo' in ex.commands
56
            assert ex.commands['foo'] == foo
57
            assert ex.default_command == 'foo'
58
            assert main_called[0] is True
59
    finally:
60
        __name__ = oldname
61
62
63
def test_fails_on_unused_config_updates(ex):
64
    @ex.config
65
    def cfg():
66
        b = 3
67
        f = {'oo': 1}
68
        g = {'a': 'l'}
69
70
    @ex.main
71
    def foo(f, a=10):
72
        assert f
73
        return a
74
75
    # normal config updates work
76
    assert ex.run(config_updates={'a': 3, 'b': 2}).result == 3
77
78
    # unused config updates raise
79
    with pytest.raises(KeyError):
80
        ex.run(config_updates={'c': 3})
81
82
    # unused but in config updates work
83
    ex.run(config_updates={'g': 3})
84
    ex.run(config_updates={'g': {'a': 'r'}})
85
86
    # nested unused config updates raise
87
    with pytest.raises(KeyError):
88
        ex.run(config_updates={'g': {'u': 'p'}})
89
90
    # nested unused but parent used updates work
91
    assert ex.run(config_updates={'f': {'uzz': 8}})
92