-
Notifications
You must be signed in to change notification settings - Fork 148
/
test_config.py
227 lines (175 loc) · 6.82 KB
/
test_config.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
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
from __future__ import annotations
import os
from enum import Enum
import pytest
from anndata._config import (
SettingsManager,
check_and_get_bool,
check_and_get_environ_var,
validate_bool,
)
option = "test_var"
default_val = False
description = "My doc string!"
option_2 = "test_var_2"
default_val_2 = False
description_2 = "My doc string 2!"
option_3 = "test_var_3"
default_val_3 = [1, 2]
description_3 = "My doc string 3!"
type_3 = list[int]
def validate_int_list(val) -> bool:
if not isinstance(val, list) or not [isinstance(type(e), int) for e in val]:
raise TypeError(f"{repr(val)} is not a valid int list")
return True
settings = SettingsManager()
settings.register(option, default_val, description, validate_bool)
settings.register(option_2, default_val_2, description_2, validate_bool)
settings.register(
option_3,
default_val_3,
description_3,
validate_int_list,
type_3,
)
def test_register_option_default():
assert getattr(settings, option) == default_val
assert description in settings.describe(option)
def test_register_with_env(monkeypatch):
with monkeypatch.context() as mp:
option_env = "test_var_env"
default_val_env = False
description_env = "My doc string env!"
option_env_var = "ANNDATA_" + option_env.upper()
mp.setenv(option_env_var, "1")
settings.register(
option_env,
default_val_env,
description_env,
validate_bool,
get_from_env=check_and_get_bool,
)
assert settings.test_var_env
def test_register_with_env_enum(monkeypatch):
with monkeypatch.context() as mp:
option_env = "test_var_env"
default_val_env = False
description_env = "My doc string env!"
option_env_var = "ANNDATA_" + option_env.upper()
mp.setenv(option_env_var, "b")
class TestEnum(Enum):
a = False
b = True
def check_and_get_bool_enum(option, default_value):
return check_and_get_environ_var(
"ANNDATA_" + option.upper(), "a", cast=TestEnum
).value
settings.register(
option_env,
default_val_env,
description_env,
validate_bool,
get_from_env=check_and_get_bool_enum,
)
assert settings.test_var_env
def test_register_bad_option():
with pytest.raises(TypeError, match="'foo' is not a valid int list"):
settings.register(
"test_var_4",
"foo", # should be a list of ints
description_3,
validate_int_list,
type_3,
)
def test_set_option():
setattr(settings, option, not default_val)
assert getattr(settings, option) == (not default_val)
settings.reset(option)
assert getattr(settings, option) == default_val
def test_dir():
assert {option, option_2, option_3} <= set(dir(settings))
assert dir(settings) == sorted(dir(settings))
def test_reset_multiple():
setattr(settings, option, not default_val)
setattr(settings, option_2, not default_val_2)
settings.reset([option, option_2])
assert getattr(settings, option) == default_val
assert getattr(settings, option_2) == default_val_2
def test_get_unregistered_option():
with pytest.raises(AttributeError):
setattr(settings, option + "_different", default_val)
def test_override():
with settings.override(**{option: not default_val}):
assert getattr(settings, option) == (not default_val)
assert getattr(settings, option) == default_val
def test_override_multiple():
with settings.override(**{option: not default_val, option_2: not default_val_2}):
assert getattr(settings, option) == (not default_val)
assert getattr(settings, option_2) == (not default_val_2)
assert getattr(settings, option) == default_val
assert getattr(settings, option_2) == default_val_2
def test_deprecation():
warning = "This is a deprecation warning!"
version = "0.1.0"
settings.deprecate(option, version, warning)
described_option = settings.describe(option, print_description=False)
# first line is message, second two from deprecation
default_deprecation_message = f"{option} will be removed in {version}.*"
assert described_option.endswith(default_deprecation_message)
described_option = (
described_option.rstrip().removesuffix(default_deprecation_message).rstrip()
)
assert described_option.endswith(warning)
with pytest.warns(
DeprecationWarning,
match="'test_var' will be removed in 0.1.0. This is a deprecation warning!",
):
assert getattr(settings, option) == default_val
def test_deprecation_no_message():
version = "0.1.0"
settings.deprecate(option, version)
described_option = settings.describe(option, print_description=False)
# first line is message, second from deprecation version
assert described_option.endswith(f"{option} will be removed in {version}.*")
def test_option_typing():
assert settings._registered_options[option_3].type == type_3
assert str(type_3) in settings.describe(option_3, print_description=False)
def test_check_and_get_environ_var(monkeypatch):
with monkeypatch.context() as mp:
option_env_var = "ANNDATA_OPTION"
assert hash("foo") == check_and_get_environ_var(
option_env_var, "foo", ["foo", "bar"], lambda x: hash(x)
)
mp.setenv(option_env_var, "bar")
assert hash("bar") == check_and_get_environ_var(
option_env_var, "foo", ["foo", "bar"], lambda x: hash(x)
)
mp.setenv(option_env_var, "Not foo or bar")
with pytest.warns(
match=f'Value "{os.environ[option_env_var]}" is not in allowed'
):
check_and_get_environ_var(
option_env_var, "foo", ["foo", "bar"], lambda x: hash(x)
)
assert hash("Not foo or bar") == check_and_get_environ_var(
option_env_var, "foo", cast=lambda x: hash(x)
)
def test_check_and_get_bool(monkeypatch):
with monkeypatch.context() as mp:
option_env_var = "ANNDATA_" + option.upper()
assert not check_and_get_bool(option, default_val)
mp.setenv(option_env_var, "1")
assert check_and_get_bool(option, default_val)
mp.setenv(option_env_var, "Not 0 or 1")
with pytest.warns(
match=f'Value "{os.environ[option_env_var]}" is not in allowed'
):
check_and_get_bool(option, default_val)
def test_check_and_get_bool_enum(monkeypatch):
with monkeypatch.context() as mp:
option_env_var = "ANNDATA_" + option.upper()
mp.setenv(option_env_var, "b")
class TestEnum(Enum):
a = False
b = True
assert check_and_get_environ_var(option_env_var, "a", cast=TestEnum).value