Coverage for tests/test_utils/test_with_pytest.py: 97%
176 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-11 03:57 +0330
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-11 03:57 +0330
1import pytest
3from lazy_settings.conf import settings
4from lazy_settings.test.utils import override_settings, modify_settings
6from . import global_settings
8pytestmark = pytest.mark.anyio
10settings.clear()
11settings.register(global_settings)
14@pytest.mark.order(1)
15@override_settings(A_SETTING="something new")
16def test_override_settings_function_decorator():
17 assert settings.A_SETTING == "something new"
20@pytest.mark.order(2)
21def test_override_settings_function_decorator_cleanup():
22 assert settings.A_SETTING == "something"
25@pytest.mark.order(3)
26@override_settings(A_SETTING="something new")
27async def test_override_settings_async_function_decorator():
28 assert settings.A_SETTING == "something new"
31@pytest.mark.order(4)
32async def test_override_settings_async_function_decorator_cleanup():
33 assert settings.A_SETTING == "something"
36def test_override_settings_context_manager_in_function():
37 with override_settings(A_SETTING="context!!"):
38 assert settings.A_SETTING == "context!!"
40 assert settings.A_SETTING == "something"
43async def test_override_settings_context_manager_in_async_function():
44 with override_settings(A_SETTING="context!!"):
45 assert settings.A_SETTING == "context!!"
47 assert settings.A_SETTING == "something"
50@pytest.mark.order(5)
51@modify_settings(LIST_BASED_SETTING={"append": "things", "prepend": "first things"})
52def test_modify_settings_fucntion_decorator():
53 assert settings.LIST_BASED_SETTING == ["first things", "one", "two", "things"]
56@pytest.mark.order(6)
57def test_modify_settings_function_decorator_cleanup():
58 assert settings.LIST_BASED_SETTING == ["one", "two"]
61@pytest.mark.order(7)
62@modify_settings(LIST_BASED_SETTING={"append": "things", "prepend": "first things"})
63async def test_modify_settings_async_fucntion_decorator():
64 assert settings.LIST_BASED_SETTING == ["first things", "one", "two", "things"]
67@pytest.mark.order(8)
68async def test_modify_settings_async_function_decorator_cleanup():
69 assert settings.LIST_BASED_SETTING == ["one", "two"]
72def test_modify_settings_context_manager_in_function():
73 with modify_settings(
74 LIST_BASED_SETTING={"append": "three", "prepend": "zero"},
75 ):
76 assert settings.LIST_BASED_SETTING == ["zero", "one", "two", "three"]
77 assert settings.LIST_BASED_SETTING == ["one", "two"]
80async def test_modify_settings_context_manager_in_async_function():
81 with modify_settings(
82 LIST_BASED_SETTING={"append": "three", "prepend": "zero"},
83 ):
84 assert settings.LIST_BASED_SETTING == ["zero", "one", "two", "three"]
85 assert settings.LIST_BASED_SETTING == ["one", "two"]
88@override_settings(A_SETTING="new value", NEW_SETTING="this wasn't in the file")
89class TestOverridentSettings:
90 def test_overriden_settings(self):
91 assert settings.A_SETTING == "new value"
92 assert settings.NEW_SETTING == "this wasn't in the file"
94 @override_settings(A_SETTING="another one")
95 def test_override_after_override(self):
96 assert settings.A_SETTING == "another one"
98 def test_override_with_context_manager(self):
99 assert settings.A_SETTING == "new value"
100 with override_settings(A_SETTING="context!"):
101 assert settings.A_SETTING == "context!"
103 assert settings.A_SETTING == "new value"
105 def test_class_decorator_is_accessed(self, mocker):
106 spy_decorator = mocker.spy(override_settings, "decorate_class")
108 @override_settings(A_SETTING="a")
109 class TestClass:
110 def test_something(self):
111 assert 2 == 2
113 spy_decorator.assert_called_once()
114 assert spy_decorator.spy_return == TestClass
116 def test_callable_decorator_accessed(self, mocker):
117 spy_decorator = mocker.spy(override_settings, "decorate_callable")
119 @override_settings(A_SETTING="a")
120 def test_callable():
121 assert 2 == 2
123 spy_decorator.assert_called_once()
124 assert spy_decorator.spy_return == test_callable
126 def test_enable_and_disable_accessed(self, mocker):
127 spy_enable = mocker.spy(override_settings, "enable")
128 spy_disable = mocker.spy(override_settings, "disable")
130 # TODO: test enable and disable when used as a decorator
131 with override_settings(A_SETTING="a"):
132 pass
134 spy_enable.assert_called_once()
135 spy_disable.assert_called_once()
138@override_settings(A_SETTING="new value", NEW_SETTING="this wasn't in the file")
139class TestOverridentSettingsAsync:
140 @override_settings(A_SETTING="another one")
141 async def test_override_after_override(self):
142 assert settings.A_SETTING == "another one"
144 async def test_override_with_context_manager(self):
145 assert settings.A_SETTING == "new value"
146 with override_settings(A_SETTING="context!"):
147 assert settings.A_SETTING == "context!"
149 assert settings.A_SETTING == "new value"
151 async def test_callable_decorator_accessed(self, mocker):
152 spy_decorator = mocker.spy(override_settings, "decorate_callable")
154 @override_settings(A_SETTING="a")
155 async def test_callable():
156 assert 2 == 2
158 spy_decorator.assert_called_once()
159 assert spy_decorator.spy_return == test_callable
161 async def test_enable_and_disable_accessed(self, mocker):
162 spy_enable = mocker.spy(override_settings, "enable")
163 spy_disable = mocker.spy(override_settings, "disable")
165 # TODO: test enable and disable when used as a decorator
166 with override_settings(A_SETTING="a"):
167 pass
169 spy_enable.assert_called_once()
170 spy_disable.assert_called_once()
173@modify_settings(LIST_BASED_SETTING={"append": "three", "prepend": "zero"})
174class TestModifySettings:
175 def test_modified_settings(self):
176 assert len(settings.LIST_BASED_SETTING) == 4
177 assert settings.LIST_BASED_SETTING[0] == "zero"
178 assert settings.LIST_BASED_SETTING[-1] == "three"
180 @modify_settings(LIST_BASED_SETTING={"remove": "two"})
181 def test_modify_after_modify(self):
182 assert len(settings.LIST_BASED_SETTING) == 3
183 assert "two" not in settings.LIST_BASED_SETTING
185 def test_modify_with_context_manager(self):
186 assert len(settings.LIST_BASED_SETTING) == 4
187 with modify_settings(
188 LIST_BASED_SETTING={"append": "four", "prepend": "less than zero"},
189 ):
190 assert len(settings.LIST_BASED_SETTING) == 6
191 assert settings.LIST_BASED_SETTING[0] == "less than zero"
192 assert settings.LIST_BASED_SETTING[-1] == "four"
194 assert len(settings.LIST_BASED_SETTING) == 4
196 def test_class_decorator_is_accessed(self, mocker):
197 spy_decorator = mocker.spy(modify_settings, "decorate_class")
199 @modify_settings(LIST_BASED_SETTING={"append": "four"})
200 class TestClass:
201 def test_something(self):
202 assert 2 == 2
204 spy_decorator.assert_called_once()
205 assert spy_decorator.spy_return == TestClass
207 def test_callable_decorator_accessed(self, mocker):
208 spy_decorator = mocker.spy(modify_settings, "decorate_callable")
210 @modify_settings(LIST_BASED_SETTING={"append": "four"})
211 def test_callable():
212 assert 2 == 2
214 spy_decorator.assert_called_once()
215 assert spy_decorator.spy_return == test_callable
217 def test_enable_and_disable_accessed(self, mocker):
218 spy_enable = mocker.spy(modify_settings, "enable")
219 spy_disable = mocker.spy(modify_settings, "disable")
221 # TODO: test enable and disable when used as a decorator
222 with modify_settings(LIST_BASED_SETTING={"append": "four"}):
223 pass
225 spy_enable.assert_called_once()
226 spy_disable.assert_called_once()
229@modify_settings(LIST_BASED_SETTING={"append": "three", "prepend": "zero"})
230class TestModifySettingsAsync:
231 @modify_settings(LIST_BASED_SETTING={"remove": "two"})
232 async def test_modify_after_modify(self):
233 assert len(settings.LIST_BASED_SETTING) == 3
234 assert "two" not in settings.LIST_BASED_SETTING
236 async def test_modify_with_context_manager(self):
237 assert len(settings.LIST_BASED_SETTING) == 4
238 with modify_settings(
239 LIST_BASED_SETTING={"append": "four", "prepend": "less than zero"},
240 ):
241 assert len(settings.LIST_BASED_SETTING) == 6
242 assert settings.LIST_BASED_SETTING[0] == "less than zero"
243 assert settings.LIST_BASED_SETTING[-1] == "four"
245 assert len(settings.LIST_BASED_SETTING) == 4
247 async def test_callable_decorator_accessed(self, mocker):
248 spy_decorator = mocker.spy(modify_settings, "decorate_callable")
250 @modify_settings(LIST_BASED_SETTING={"append": "four"})
251 async def test_callable():
252 assert 2 == 2
254 spy_decorator.assert_called_once()
255 assert spy_decorator.spy_return == test_callable
257 async def test_enable_and_disable_accessed(self, mocker):
258 spy_enable = mocker.spy(modify_settings, "enable")
259 spy_disable = mocker.spy(modify_settings, "disable")
261 # TODO: test enable and disable when used as a decorator
262 with modify_settings(LIST_BASED_SETTING={"append": "four"}):
263 pass
265 spy_enable.assert_called_once()
266 spy_disable.assert_called_once()