File: test_views.py

package info (click to toggle)
python-confuse 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: python: 3,065; makefile: 112; sh: 8
file content (278 lines) | stat: -rw-r--r-- 9,531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import annotations

import unittest
from typing import TYPE_CHECKING

import pytest

import confuse

from . import _root

if TYPE_CHECKING:
    from collections.abc import Iterator

    from confuse import Subview


class SingleSourceTest(unittest.TestCase):
    def test_dict_access(self):
        config = _root({"foo": "bar"})
        value = config["foo"].get()
        assert value == "bar"

    def test_list_access(self):
        config = _root({"foo": ["bar", "baz"]})
        value = config["foo"][1].get()
        assert value == "baz"

    def test_missing_key(self):
        config = _root({"foo": "bar"})
        with pytest.raises(confuse.NotFoundError):
            config["baz"].get()

    def test_missing_index(self):
        config = _root({"l": ["foo", "bar"]})
        with pytest.raises(confuse.NotFoundError):
            config["l"][5].get()

    def test_dict_iter(self):
        config = _root({"foo": "bar", "baz": "qux"})
        keys = [key for key in config]
        assert set(keys) == {"foo", "baz"}

    def test_list_iter(self):
        config = _root({"l": ["foo", "bar"]})
        # TODO(@snejus): we need to split Subview to SequenceView and MappingView in
        # order to have automatic resolution here
        _items: Iterator[Subview] = iter(config["l"])
        items = [subview.get() for subview in _items]
        assert items == ["foo", "bar"]

    def test_int_iter(self):
        config = _root({"n": 2})
        with pytest.raises(confuse.ConfigTypeError):
            [item for item in config["n"]]

    def test_dict_keys(self):
        config = _root({"foo": "bar", "baz": "qux"})
        keys = config.keys()
        assert set(keys) == {"foo", "baz"}

    def test_dict_values(self):
        config = _root({"foo": "bar", "baz": "qux"})
        values = [value.get() for value in config.values()]
        assert set(values) == {"bar", "qux"}

    def test_dict_items(self):
        config = _root({"foo": "bar", "baz": "qux"})
        items = [(key, value.get()) for (key, value) in config.items()]
        assert set(items) == {("foo", "bar"), ("baz", "qux")}

    def test_list_keys_error(self):
        config = _root({"l": ["foo", "bar"]})
        with pytest.raises(confuse.ConfigTypeError):
            config["l"].keys()

    def test_list_sequence(self):
        config = _root({"l": ["foo", "bar"]})
        items = [item.get() for item in config["l"].sequence()]
        assert items == ["foo", "bar"]

    def test_dict_sequence_error(self):
        config = _root({"foo": "bar", "baz": "qux"})
        with pytest.raises(confuse.ConfigTypeError):
            list(config.sequence())

    def test_dict_contents(self):
        config = _root({"foo": "bar", "baz": "qux"})
        contents = config.all_contents()
        assert set(contents) == {"foo", "baz"}

    def test_list_contents(self):
        config = _root({"l": ["foo", "bar"]})
        contents = config["l"].all_contents()
        assert list(contents) == ["foo", "bar"]

    def test_int_contents(self):
        config = _root({"n": 2})
        with pytest.raises(confuse.ConfigTypeError):
            list(config["n"].all_contents())


class ConverstionTest(unittest.TestCase):
    def test_str_conversion_from_str(self):
        config = _root({"foo": "bar"})
        value = str(config["foo"])
        assert value == "bar"

    def test_str_conversion_from_int(self):
        config = _root({"foo": 2})
        value = str(config["foo"])
        assert value == "2"

    def test_bool_conversion_from_bool(self):
        config = _root({"foo": True})
        value = bool(config["foo"])
        assert value

    def test_bool_conversion_from_int(self):
        config = _root({"foo": 0})
        value = bool(config["foo"])
        assert not value


class NameTest(unittest.TestCase):
    def test_root_name(self):
        config = _root()
        assert config.name == "root"

    def test_string_access_name(self):
        config = _root()
        name = config["foo"].name
        assert name == "foo"

    def test_int_access_name(self):
        config = _root()
        name = config[5].name
        assert name == "#5"

    def test_nested_access_name(self):
        config = _root()
        name = config[5]["foo"]["bar"][20].name
        assert name == "#5.foo.bar#20"


class MultipleSourceTest(unittest.TestCase):
    def test_dict_access_shadowed(self):
        config = _root({"foo": "bar"}, {"foo": "baz"})
        value = config["foo"].get()
        assert value == "bar"

    def test_dict_access_fall_through(self):
        config = _root({"qux": "bar"}, {"foo": "baz"})
        value = config["foo"].get()
        assert value == "baz"

    def test_dict_access_missing(self):
        config = _root({"qux": "bar"}, {"foo": "baz"})
        with pytest.raises(confuse.NotFoundError):
            config["fred"].get()

    def test_list_access_shadowed(self):
        config = _root({"l": ["a", "b"]}, {"l": ["c", "d", "e"]})
        value = config["l"][1].get()
        assert value == "b"

    def test_list_access_fall_through(self):
        config = _root({"l": ["a", "b"]}, {"l": ["c", "d", "e"]})
        value = config["l"][2].get()
        assert value == "e"

    def test_list_access_missing(self):
        config = _root({"l": ["a", "b"]}, {"l": ["c", "d", "e"]})
        with pytest.raises(confuse.NotFoundError):
            config["l"][3].get()

    def test_access_dict_replaced(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"qux": "fred"}})
        value = config["foo"].get()
        assert value == {"bar": "baz"}

    def test_dict_keys_merged(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"qux": "fred"}})
        keys = config["foo"].keys()
        assert set(keys) == {"bar", "qux"}

    def test_dict_keys_replaced(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"bar": "fred"}})
        keys = config["foo"].keys()
        assert list(keys) == ["bar"]

    def test_dict_values_merged(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"qux": "fred"}})
        values = [value.get() for value in config["foo"].values()]
        assert set(values) == {"baz", "fred"}

    def test_dict_values_replaced(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"bar": "fred"}})
        values = [value.get() for value in config["foo"].values()]
        assert list(values) == ["baz"]

    def test_dict_items_merged(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"qux": "fred"}})
        items = [(key, value.get()) for (key, value) in config["foo"].items()]
        assert set(items) == {("bar", "baz"), ("qux", "fred")}

    def test_dict_items_replaced(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"bar": "fred"}})
        items = [(key, value.get()) for (key, value) in config["foo"].items()]
        assert list(items) == [("bar", "baz")]

    def test_list_sequence_shadowed(self):
        config = _root({"l": ["a", "b"]}, {"l": ["c", "d", "e"]})
        items = [item.get() for item in config["l"].sequence()]
        assert items == ["a", "b"]

    def test_list_sequence_shadowed_by_dict(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": ["qux", "fred"]})
        with pytest.raises(confuse.ConfigTypeError):
            list(config["foo"].sequence())

    def test_dict_contents_concatenated(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"qux": "fred"}})
        contents = config["foo"].all_contents()
        assert set(contents) == {"bar", "qux"}

    def test_dict_contents_concatenated_not_replaced(self):
        config = _root({"foo": {"bar": "baz"}}, {"foo": {"bar": "fred"}})
        contents = config["foo"].all_contents()
        assert list(contents) == ["bar", "bar"]

    def test_list_contents_concatenated(self):
        config = _root({"foo": ["bar", "baz"]}, {"foo": ["qux", "fred"]})
        contents = config["foo"].all_contents()
        assert list(contents) == ["bar", "baz", "qux", "fred"]

    def test_int_contents_error(self):
        config = _root({"foo": ["bar", "baz"]}, {"foo": 5})
        with pytest.raises(confuse.ConfigTypeError):
            list(config["foo"].all_contents())

    def test_list_and_dict_contents_concatenated(self):
        config = _root({"foo": ["bar", "baz"]}, {"foo": {"qux": "fred"}})
        contents = config["foo"].all_contents()
        assert list(contents) == ["bar", "baz", "qux"]

    def test_add_source(self):
        config = _root({"foo": "bar"})
        config.add({"baz": "qux"})
        assert config["foo"].get() == "bar"
        assert config["baz"].get() == "qux"


class SetTest(unittest.TestCase):
    def test_set_missing_top_level_key(self):
        config = _root({})
        config["foo"] = "bar"
        assert config["foo"].get() == "bar"

    def test_override_top_level_key(self):
        config = _root({"foo": "bar"})
        config["foo"] = "baz"
        assert config["foo"].get() == "baz"

    def test_set_second_level_key(self):
        config = _root({})
        config["foo"]["bar"] = "baz"
        assert config["foo"]["bar"].get() == "baz"

    def test_override_second_level_key(self):
        config = _root({"foo": {"bar": "qux"}})
        config["foo"]["bar"] = "baz"
        assert config["foo"]["bar"].get() == "baz"

    def test_override_list_index(self):
        config = _root({"foo": ["a", "b", "c"]})
        config["foo"][1] = "bar"
        assert config["foo"][1].get() == "bar"