Completed
Push — master ( 50b00b...d515a4 )
by Klaus
11s
created

tests/test_config/test_config_dict.py (1 issue)

1
#!/usr/bin/env python
2
# coding=utf-8
3
from __future__ import division, print_function, unicode_literals
4
5
import pytest
6
import sacred.optional as opt
7
from sacred.config import ConfigDict
8
from sacred.config.custom_containers import DogmaticDict, DogmaticList
9
10
11
@pytest.fixture
12
def conf_dict():
13
    cfg = ConfigDict({
14
        "a": 1,
15
        "b": 2.0,
16
        "c": True,
17
        "d": 'string',
18
        "e": [1, 2, 3],
19
        "f": {'a': 'b', 'c': 'd'},
20
    })
21
    return cfg
22
23
24
def test_config_dict_returns_dict(conf_dict):
25
    assert isinstance(conf_dict(), dict)
26
27
28
def test_config_dict_result_contains_keys(conf_dict):
29
    cfg = conf_dict()
30
    assert set(cfg.keys()) == {'a', 'b', 'c', 'd', 'e', 'f'}
31
    assert cfg['a'] == 1
32
    assert cfg['b'] == 2.0
33
    assert cfg['c']
34
    assert cfg['d'] == 'string'
35
    assert cfg['e'] == [1, 2, 3]
36
    assert cfg['f'] == {'a': 'b', 'c': 'd'}
37
38
39
def test_fixing_values(conf_dict):
40
    assert conf_dict({'a': 100})['a'] == 100
41
42
43
@pytest.mark.parametrize("key", ["$f", "contains.dot", "py/tuple", "json://1"])
44
def test_config_dict_raises_on_invalid_keys(key):
45
    with pytest.raises(KeyError):
46
        ConfigDict({key: True})
47
48
49
@pytest.mark.parametrize("value", [lambda x:x, pytest, test_fixing_values])
50
def test_config_dict_accepts_special_types(value):
51
    assert ConfigDict({"special": value})()['special'] == value
52
53
54
def test_fixing_nested_dicts(conf_dict):
55
    cfg = conf_dict({'f': {'c': 't'}})
56
    assert cfg['f']['a'] == 'b'
57
    assert cfg['f']['c'] == 't'
58
59
60
def test_adding_values(conf_dict):
61
    cfg = conf_dict({'g': 23, 'h': {'i': 10}})
62
    assert cfg['g'] == 23
63
    assert cfg['h'] == {'i': 10}
64
    assert cfg.added == {'g', 'h', 'h.i'}
65
66
67
def test_typechange(conf_dict):
68
    cfg = conf_dict({'a': 'bar', 'b': 'foo', 'c': 1})
69
    assert cfg.typechanged == {'a': (int, type('bar')),
70
                               'b': (float, type('foo')),
71
                               'c': (bool, int)}
72
73
74
def test_nested_typechange(conf_dict):
75
    cfg = conf_dict({'f': {'a': 10}})
76
    assert cfg.typechanged == {'f.a': (type('a'), int)}
77
78
79
def is_dogmatic(a):
80
    if isinstance(a, (DogmaticDict, DogmaticList)):
81
        return True
82
    elif isinstance(a, dict):
83
        return any(is_dogmatic(v) for v in a.values())
84
    elif isinstance(a, (list, tuple)):
85
        return any(is_dogmatic(v) for v in a)
86
87
88
def test_result_of_conf_dict_is_not_dogmatic(conf_dict):
89
    cfg = conf_dict({'e': [1, 1, 1]})
90
    assert not is_dogmatic(cfg)
91
92
93
@pytest.mark.skipif(not opt.has_numpy, reason="requires numpy")
94
def test_conf_scope_handles_numpy_bools():
95
    cfg = ConfigDict({
96
        "a": opt.np.bool_(1)
97
    })
98
    assert 'a' in cfg()
99
    assert cfg()['a']
100
101
102
def test_conf_scope_contains_presets():
103
    conf_dict = ConfigDict({
104
        "answer": 42
105
    })
106
    cfg = conf_dict(preset={'a': 21, 'unrelated': True})
107
    assert set(cfg.keys()) == {'a', 'answer', 'unrelated'}
108
    assert cfg['a'] == 21
109
    assert cfg['answer'] == 42
110
    assert cfg['unrelated'] is True
111
112
113
def test_conf_scope_does_not_contain_fallback():
114
    config_dict = ConfigDict({
115
        "answer": 42
116
    })
117
118
    cfg = config_dict(fallback={'a': 21, 'b': 10})
119
120
    assert set(cfg.keys()) == {'answer'}
121
122
123 View Code Duplication
def test_fixed_subentry_of_preset():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
124
    config_dict = ConfigDict({})
125
126
    cfg = config_dict(preset={'d': {'a': 1, 'b': 2}}, fixed={'d': {'a': 10}})
127
128
    assert set(cfg.keys()) == {'d'}
129
    assert set(cfg['d'].keys()) == {'a', 'b'}
130
    assert cfg['d']['a'] == 10
131
    assert cfg['d']['b'] == 2
132