-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_definition.py
196 lines (128 loc) · 4.69 KB
/
test_definition.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
import re
import pytest
from env_config import Environment, values
from env_config.constants import Undefined
from env_config.decorators import classproperty
from env_config.errors import MissingEnvValueError
from tests.helpers import set_dotenv, set_environ
@set_dotenv("Test", FOO="bar")
def test_environment__load_dotenv():
class Test(Environment):
pass
assert Test.dotenv == {"FOO": "bar"}
@set_dotenv("Test", FOO="bar")
def test_environment__dont_load_dotenv_if_env_not_match():
class Prod(Environment):
pass
assert Prod.dotenv == Undefined
def test_environment__subclassed():
with set_dotenv("Common", FOO="1"):
class Common(Environment):
FOO = values.StringValue()
with set_dotenv("Test", FOO="2"):
class Test(Common):
pass # Field not redefined, but different env-value
assert Common.dotenv == {"FOO": "1"}
assert Test.dotenv == {"FOO": "2"}
assert Common.FOO == "1"
assert Test.FOO == "2"
@set_dotenv("Test", FOO="bar")
def test_environment__set_globals():
class Test(Environment):
FOO = values.StringValue()
assert Test.FOO == "bar"
assert globals()["FOO"] == "bar"
@set_dotenv("Test", FOO="bar")
def test_environment__set_globals__classproperty():
class Test(Environment):
FOO = values.StringValue()
@classproperty
def BAR(cls):
return f"{cls.FOO.upper()}"
assert Test.FOO == "bar"
assert Test.BAR == "BAR"
assert globals()["FOO"] == "bar"
assert globals()["BAR"] == "BAR"
@set_dotenv("Test", FIZZ="buzz")
def test_environment__no_data():
msg = "Value 'FOO' in environment 'Test' not defined in the .env file and value does not have a default"
with pytest.raises(MissingEnvValueError, match=re.escape(msg)):
class Test(Environment):
FOO = values.StringValue()
@set_dotenv("Test", FOO="bar")
def test_environment__no_dotenv():
msg = "Value 'FOO' in environment 'Test' needs a default value since environment does not define a `dotenv_path`"
with pytest.raises(MissingEnvValueError, match=re.escape(msg)):
class Test(Environment, dotenv_path=None):
FOO = values.StringValue()
@set_environ("Test", FOO="bar")
def test_environment__use_environ():
class Test(Environment, use_environ=True):
FOO = values.StringValue()
assert Test.FOO == "bar"
@set_dotenv("Test", FOO="bar")
def test_environment__overrides_from():
class Overrides:
FOO = "foo"
class Test(Environment, overrides_from=Overrides):
FOO = values.StringValue()
assert Test.FOO == "foo"
@set_dotenv("Test")
def test_environment__overrides_from__value_descriptor():
class Overrides:
FOO = values.StringValue(default="foo")
class Test(Environment, overrides_from=Overrides):
FOO = values.StringValue()
assert Test.FOO == "foo"
@set_dotenv("Test", FOO="foo")
def test_environment__overrides_from__value_descriptor__no_default():
class Overrides:
FOO = values.StringValue()
class Test(Environment, overrides_from=Overrides):
FOO = values.StringValue(default="bar")
assert Test.FOO == "foo"
@set_dotenv("Test", FOO="bar")
def test_environment__overrides_from__pre_setup():
class Overrides:
FOO = "foo"
@classmethod
def pre_setup(cls):
cls.FOO = "baz"
class Test(Environment, overrides_from=Overrides):
FOO = values.StringValue()
assert Test.FOO == "baz"
@set_dotenv("Test", FOO="bar")
def test_environment__overrides_from__post_setup():
class Overrides:
FOO = "foo"
@classmethod
def post_setup(cls):
cls.FOO = "baz"
class Test(Environment, overrides_from=Overrides):
FOO = values.StringValue()
assert Test.FOO == "baz"
@set_dotenv("Test")
def test_environment__default():
class Test(Environment):
FOO = values.StringValue(default="fizzbuzz")
assert Test.FOO == "fizzbuzz"
@set_dotenv("Test")
def test_environment__default__null():
class Test(Environment):
FOO = values.StringValue(default=None)
assert Test.FOO is None
@set_dotenv("Test", FIZZ="buzz")
def test_environment__env_name():
class Test(Environment):
FOO = values.StringValue(env_name="FIZZ")
assert Test.FOO == "buzz"
@set_dotenv("Test", FOO="bar")
def test_environment__env_name__null():
with pytest.raises(MissingEnvValueError):
class Test(Environment):
FOO = values.StringValue(env_name=None)
@set_dotenv("Test", FOO="bar")
def test_environment__env_name__null__default():
class Test(Environment):
FOO = values.StringValue(default="foo", env_name=None)
assert Test.FOO == "foo"