-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathtest__option.py
111 lines (76 loc) · 3.03 KB
/
test__option.py
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
import os
from unittest import mock
import pytest
from idom._option import DeprecatedOption, Option
def test_option_repr():
opt = Option("A_FAKE_OPTION", "some-value")
assert opt.name == "A_FAKE_OPTION"
assert repr(opt) == "Option(A_FAKE_OPTION='some-value')"
@mock.patch.dict(os.environ, {"A_FAKE_OPTION": "value-from-environ"})
def test_option_from_os_environ():
opt = Option("A_FAKE_OPTION", "default-value")
assert opt.current == "value-from-environ"
def test_option_from_default():
opt = Option("A_FAKE_OPTION", "default-value")
assert opt.current == "default-value"
assert opt.current is opt.default
@mock.patch.dict(os.environ, {"A_FAKE_OPTION": "1"})
def test_option_validator():
opt = Option("A_FAKE_OPTION", False, validator=lambda x: bool(int(x)))
assert opt.current is True
opt.current = "0"
assert opt.current is False
with pytest.raises(ValueError, match="invalid literal for int"):
opt.current = "not-an-int"
def test_immutable_option():
opt = Option("A_FAKE_OPTION", "default-value", mutable=False)
assert not opt.mutable
with pytest.raises(TypeError, match="cannot be modified after initial load"):
opt.current = "a-new-value"
with pytest.raises(TypeError, match="cannot be modified after initial load"):
opt.unset()
def test_option_reset():
opt = Option("A_FAKE_OPTION", "default-value")
opt.current = "a-new-value"
opt.unset()
assert opt.current is opt.default
assert not opt.is_set()
@mock.patch.dict(os.environ, {"A_FAKE_OPTION": "value-from-environ"})
def test_option_reload():
opt = Option("A_FAKE_OPTION", "default-value")
opt.current = "some-other-value"
opt.reload()
assert opt.current == "value-from-environ"
def test_option_set():
opt = Option("A_FAKE_OPTION", "default-value")
assert not opt.is_set()
opt.current = "a-new-value"
assert opt.is_set()
def test_option_set_default():
opt = Option("A_FAKE_OPTION", "default-value")
assert not opt.is_set()
assert opt.set_default("new-value") == "new-value"
assert opt.is_set()
def test_cannot_subscribe_immutable_option():
opt = Option("A_FAKE_OPTION", "default", mutable=False)
with pytest.raises(TypeError, match="Immutable options cannot be subscribed to"):
opt.subscribe(lambda value: None)
def test_option_subscribe():
opt = Option("A_FAKE_OPTION", "default")
calls = []
opt.subscribe(calls.append)
assert calls == ["default"]
opt.current = "default"
# value did not change, so no trigger
assert calls == ["default"]
opt.current = "new-1"
opt.current = "new-2"
assert calls == ["default", "new-1", "new-2"]
opt.unset()
assert calls == ["default", "new-1", "new-2", "default"]
def test_deprecated_option():
opt = DeprecatedOption("is deprecated!", "A_FAKE_OPTION", None)
with pytest.warns(DeprecationWarning, match="is deprecated!"):
opt.current
with pytest.warns(DeprecationWarning, match="is deprecated!"):
opt.current = "something"