|
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.custom_containers import DogmaticDict |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def test_isinstance_of_dict(): |
|
10
|
|
|
assert isinstance(DogmaticDict(), dict) |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def test_dict_interface_initialized_empty(): |
|
14
|
|
|
d = DogmaticDict() |
|
15
|
|
|
assert d == {} |
|
16
|
|
|
assert set(d.keys()) == set() |
|
17
|
|
|
assert set(d.values()) == set() |
|
18
|
|
|
assert set(d.items()) == set() |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def test_dict_interface_set_item(): |
|
22
|
|
|
d = DogmaticDict() |
|
23
|
|
|
d['a'] = 12 |
|
24
|
|
|
d['b'] = 'foo' |
|
25
|
|
|
assert 'a' in d |
|
26
|
|
|
assert 'b' in d |
|
27
|
|
|
|
|
28
|
|
|
assert d['a'] == 12 |
|
29
|
|
|
assert d['b'] == 'foo' |
|
30
|
|
|
|
|
31
|
|
|
assert set(d.keys()) == {'a', 'b'} |
|
32
|
|
|
assert set(d.values()) == {12, 'foo'} |
|
33
|
|
|
assert set(d.items()) == {('a', 12), ('b', 'foo')} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def test_dict_interface_del_item(): |
|
37
|
|
|
d = DogmaticDict() |
|
38
|
|
|
d['a'] = 12 |
|
39
|
|
|
del d['a'] |
|
40
|
|
|
assert 'a' not in d |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def test_dict_interface_update_with_dict(): |
|
44
|
|
|
d = DogmaticDict() |
|
45
|
|
|
d['a'] = 12 |
|
46
|
|
|
d['b'] = 'foo' |
|
47
|
|
|
|
|
48
|
|
|
d.update({'a': 1, 'c': 2}) |
|
49
|
|
|
assert d['a'] == 1 |
|
50
|
|
|
assert d['b'] == 'foo' |
|
51
|
|
|
assert d['c'] == 2 |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_dict_interface_update_with_kwargs(): |
|
55
|
|
|
d = DogmaticDict() |
|
56
|
|
|
d['a'] = 12 |
|
57
|
|
|
d['b'] = 'foo' |
|
58
|
|
|
d.update(a=2, b=3) |
|
59
|
|
|
assert d['a'] == 2 |
|
60
|
|
|
assert d['b'] == 3 |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def test_dict_interface_update_with_list_of_items(): |
|
64
|
|
|
d = DogmaticDict() |
|
65
|
|
|
d['a'] = 12 |
|
66
|
|
|
d['b'] = 'foo' |
|
67
|
|
|
d.update([('b', 9), ('c', 7)]) |
|
68
|
|
|
assert d['a'] == 12 |
|
69
|
|
|
assert d['b'] == 9 |
|
70
|
|
|
assert d['c'] == 7 |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def test_fixed_value_not_initialized(): |
|
74
|
|
|
d = DogmaticDict({'a': 7}) |
|
75
|
|
|
assert 'a' not in d |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def test_fixed_value_fixed(): |
|
79
|
|
|
d = DogmaticDict({'a': 7}) |
|
80
|
|
|
d['a'] = 8 |
|
81
|
|
|
assert d['a'] == 7 |
|
82
|
|
|
|
|
83
|
|
|
del d['a'] |
|
84
|
|
|
assert 'a' in d |
|
85
|
|
|
assert d['a'] == 7 |
|
86
|
|
|
|
|
87
|
|
|
d.update([('a', 9), ('b', 12)]) |
|
88
|
|
|
assert d['a'] == 7 |
|
89
|
|
|
|
|
90
|
|
|
d.update({'a': 9, 'b': 12}) |
|
91
|
|
|
assert d['a'] == 7 |
|
92
|
|
|
|
|
93
|
|
|
d.update(a=10, b=13) |
|
94
|
|
|
assert d['a'] == 7 |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
def test_revelation(): |
|
98
|
|
|
d = DogmaticDict({'a': 7, 'b': 12}) |
|
99
|
|
|
d['b'] = 23 |
|
100
|
|
|
assert 'a' not in d |
|
101
|
|
|
m = d.revelation() |
|
102
|
|
|
assert set(m) == {'a'} |
|
103
|
|
|
assert 'a' in d |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
def test_fallback(): |
|
107
|
|
|
d = DogmaticDict(fallback={'a': 23}) |
|
108
|
|
|
assert 'a' in d |
|
109
|
|
|
assert d['a'] == 23 |
|
110
|
|
|
assert d.get('a') == 23 |
|
111
|
|
|
|
|
112
|
|
|
d = DogmaticDict() |
|
113
|
|
|
d.fallback = {'a': 23} |
|
114
|
|
|
assert 'a' in d |
|
115
|
|
|
assert d['a'] == 23 |
|
116
|
|
|
assert d.get('a') == 23 |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
def test_fallback_not_iterated(): |
|
120
|
|
|
d = DogmaticDict(fallback={'a': 23}) |
|
121
|
|
|
d['b'] = 1234 |
|
122
|
|
|
assert list(d.keys()) == ['b'] |
|
123
|
|
|
assert list(d.values()) == [1234] |
|
124
|
|
|
assert list(d.items()) == [('b', 1234)] |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def test_overwrite_fallback(): |
|
128
|
|
|
d = DogmaticDict(fallback={'a': 23}) |
|
129
|
|
|
d['a'] = 0 |
|
130
|
|
|
assert d['a'] == 0 |
|
131
|
|
|
assert list(d.keys()) == ['a'] |
|
132
|
|
|
assert list(d.values()) == [0] |
|
133
|
|
|
assert list(d.items()) == [('a', 0)] |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
def test_fixed_has_precedence_over_fallback(): |
|
137
|
|
|
d = DogmaticDict(fixed={'a': 0}, fallback={'a': 23}) |
|
138
|
|
|
assert d['a'] == 0 |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
def test_nested_fixed_merges_with_fallback(): |
|
142
|
|
|
d = DogmaticDict(fixed={'foo': {'bar': 20}}, |
|
143
|
|
|
fallback={'foo': {'bar': 10, 'c': 5}}) |
|
144
|
|
|
assert d['foo']['bar'] == 20 |
|
145
|
|
|
assert d['foo']['c'] == 5 |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
def test_nested_fixed_with_fallback_madness(): |
|
149
|
|
|
d = DogmaticDict(fixed={'foo': {'bar': 20}}, |
|
150
|
|
|
fallback={'foo': {'bar': 10, 'c': 5}}) |
|
151
|
|
|
d['foo'] = {'bar': 30, 'a': 1} |
|
152
|
|
|
assert d['foo']['bar'] == 20 |
|
153
|
|
|
assert d['foo']['a'] == 1 |
|
154
|
|
|
assert d['foo']['c'] == 5 |
|
155
|
|
|
|