Completed
Push — master ( 6c3661...416f46 )
by Klaus
34s
created

tests/test_config/test_config_scope_chain.py (3 issues)

1
#!/usr/bin/env python
2
# coding=utf-8
3
from __future__ import division, print_function, unicode_literals
4
5
import pytest
6
from sacred.config import ConfigScope, chain_evaluate_config_scopes
7
8
9
def test_chained_config_scopes_contain_combined_keys():
10
    @ConfigScope
11
    def cfg1():
12
        a = 10
13
14
    @ConfigScope
15
    def cfg2():
16
        b = 20
17
18
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2])
19
    assert set(final_cfg.keys()) == {'a', 'b'}
20
    assert final_cfg['a'] == 10
21
    assert final_cfg['b'] == 20
22
23
24
def test_chained_config_scopes_can_access_previous_keys():
25
    @ConfigScope
26
    def cfg1():
27
        a = 10
28
29
    @ConfigScope
30
    def cfg2(a):
31
        b = 2 * a
32
33
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2])
34
    assert set(final_cfg.keys()) == {'a', 'b'}
35
    assert final_cfg['a'] == 10
36
37
38
def test_chained_config_scopes_can_modify_previous_keys():
39
    @ConfigScope
40
    def cfg1():
41
        a = 10
42
        b = 20
43
44
    @ConfigScope
45
    def cfg2(a):
46
        a *= 2
47
        b = 22
48
49
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2])
50
    assert set(final_cfg.keys()) == {'a', 'b'}
51
    assert final_cfg['a'] == 20
52
    assert final_cfg['b'] == 22
53
54
55
def test_chained_config_scopes_raise_for_undeclared_previous_keys():
56
    @ConfigScope
57
    def cfg1():
58
        a = 10
59
60
    @ConfigScope
61
    def cfg2():
62
        b = a * 2
63
64
    with pytest.raises(NameError):
65
        chain_evaluate_config_scopes([cfg1, cfg2])
66
67
68 View Code Duplication
def test_chained_config_scopes_cannot_modify_fixed():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
69
    @ConfigScope
70
    def cfg1():
71
        c = 10
72
        a = c * 2
73
74
    @ConfigScope
75
    def cfg2(c):
76
        b = 4 * c
77
        c *= 3
78
79
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2],
80
                                                      fixed={'c': 5})
81
    assert set(final_cfg.keys()) == {'a', 'b', 'c'}
82
    assert final_cfg['a'] == 10
83
    assert final_cfg['b'] == 20
84
    assert final_cfg['c'] == 5
85
86
87 View Code Duplication
def test_chained_config_scopes_can_access_preset():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
88
    @ConfigScope
89
    def cfg1(c):
90
        a = 10 + c
91
92
    @ConfigScope
93
    def cfg2(a, c):
94
        b = a * 2 + c
95
96
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2],
97
                                                      preset={'c': 32})
98
    assert set(final_cfg.keys()) == {'a', 'b', 'c'}
99
    assert final_cfg['a'] == 42
100
    assert final_cfg['b'] == 116
101
    assert final_cfg['c'] == 32
102
103
104
def test_chained_config_scopes_can_access_fallback():
105
    @ConfigScope
106
    def cfg1(c):
107
        a = 10 + c
108
109
    @ConfigScope
110
    def cfg2(a, c):
111
        b = a * 2 + c
112
113
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2],
114
                                                      fallback={'c': 32})
115
    assert set(final_cfg.keys()) == {'a', 'b'}
116
    assert final_cfg['a'] == 42
117
    assert final_cfg['b'] == 116
118
119
120 View Code Duplication
def test_chained_config_scopes_fix_subentries():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
121
    @ConfigScope
122
    def cfg1():
123
        d = {
124
            'a': 10,
125
            'b': 20
126
        }
127
128
    @ConfigScope
129
    def cfg2():
130
        pass
131
132
    final_cfg, summary = chain_evaluate_config_scopes([cfg1, cfg2],
133
                                                      fixed={'d': {'a': 0}})
134
    assert set(final_cfg['d'].keys()) == {'a', 'b'}
135
    assert final_cfg['d']['a'] == 0
136
    assert final_cfg['d']['b'] == 20
137
138
139
def test_empty_chain_contains_preset_and_fixed():
140
    final_cfg, summary = chain_evaluate_config_scopes([],
141
                                                      fixed={'a': 0},
142
                                                      preset={'a': 1, 'b': 2})
143
    assert set(final_cfg.keys()) == {'a', 'b'}
144
    assert final_cfg['a'] == 0
145
    assert final_cfg['b'] == 2
146