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

1import pytest 

2 

3from lazy_settings.conf import settings 

4from lazy_settings.test.utils import override_settings, modify_settings 

5 

6from . import global_settings 

7 

8pytestmark = pytest.mark.anyio 

9 

10settings.clear() 

11settings.register(global_settings) 

12 

13 

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" 

18 

19 

20@pytest.mark.order(2) 

21def test_override_settings_function_decorator_cleanup(): 

22 assert settings.A_SETTING == "something" 

23 

24 

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" 

29 

30 

31@pytest.mark.order(4) 

32async def test_override_settings_async_function_decorator_cleanup(): 

33 assert settings.A_SETTING == "something" 

34 

35 

36def test_override_settings_context_manager_in_function(): 

37 with override_settings(A_SETTING="context!!"): 

38 assert settings.A_SETTING == "context!!" 

39 

40 assert settings.A_SETTING == "something" 

41 

42 

43async def test_override_settings_context_manager_in_async_function(): 

44 with override_settings(A_SETTING="context!!"): 

45 assert settings.A_SETTING == "context!!" 

46 

47 assert settings.A_SETTING == "something" 

48 

49 

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"] 

54 

55 

56@pytest.mark.order(6) 

57def test_modify_settings_function_decorator_cleanup(): 

58 assert settings.LIST_BASED_SETTING == ["one", "two"] 

59 

60 

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"] 

65 

66 

67@pytest.mark.order(8) 

68async def test_modify_settings_async_function_decorator_cleanup(): 

69 assert settings.LIST_BASED_SETTING == ["one", "two"] 

70 

71 

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"] 

78 

79 

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"] 

86 

87 

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" 

93 

94 @override_settings(A_SETTING="another one") 

95 def test_override_after_override(self): 

96 assert settings.A_SETTING == "another one" 

97 

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!" 

102 

103 assert settings.A_SETTING == "new value" 

104 

105 def test_class_decorator_is_accessed(self, mocker): 

106 spy_decorator = mocker.spy(override_settings, "decorate_class") 

107 

108 @override_settings(A_SETTING="a") 

109 class TestClass: 

110 def test_something(self): 

111 assert 2 == 2 

112 

113 spy_decorator.assert_called_once() 

114 assert spy_decorator.spy_return == TestClass 

115 

116 def test_callable_decorator_accessed(self, mocker): 

117 spy_decorator = mocker.spy(override_settings, "decorate_callable") 

118 

119 @override_settings(A_SETTING="a") 

120 def test_callable(): 

121 assert 2 == 2 

122 

123 spy_decorator.assert_called_once() 

124 assert spy_decorator.spy_return == test_callable 

125 

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") 

129 

130 # TODO: test enable and disable when used as a decorator 

131 with override_settings(A_SETTING="a"): 

132 pass 

133 

134 spy_enable.assert_called_once() 

135 spy_disable.assert_called_once() 

136 

137 

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" 

143 

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!" 

148 

149 assert settings.A_SETTING == "new value" 

150 

151 async def test_callable_decorator_accessed(self, mocker): 

152 spy_decorator = mocker.spy(override_settings, "decorate_callable") 

153 

154 @override_settings(A_SETTING="a") 

155 async def test_callable(): 

156 assert 2 == 2 

157 

158 spy_decorator.assert_called_once() 

159 assert spy_decorator.spy_return == test_callable 

160 

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") 

164 

165 # TODO: test enable and disable when used as a decorator 

166 with override_settings(A_SETTING="a"): 

167 pass 

168 

169 spy_enable.assert_called_once() 

170 spy_disable.assert_called_once() 

171 

172 

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" 

179 

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 

184 

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" 

193 

194 assert len(settings.LIST_BASED_SETTING) == 4 

195 

196 def test_class_decorator_is_accessed(self, mocker): 

197 spy_decorator = mocker.spy(modify_settings, "decorate_class") 

198 

199 @modify_settings(LIST_BASED_SETTING={"append": "four"}) 

200 class TestClass: 

201 def test_something(self): 

202 assert 2 == 2 

203 

204 spy_decorator.assert_called_once() 

205 assert spy_decorator.spy_return == TestClass 

206 

207 def test_callable_decorator_accessed(self, mocker): 

208 spy_decorator = mocker.spy(modify_settings, "decorate_callable") 

209 

210 @modify_settings(LIST_BASED_SETTING={"append": "four"}) 

211 def test_callable(): 

212 assert 2 == 2 

213 

214 spy_decorator.assert_called_once() 

215 assert spy_decorator.spy_return == test_callable 

216 

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") 

220 

221 # TODO: test enable and disable when used as a decorator 

222 with modify_settings(LIST_BASED_SETTING={"append": "four"}): 

223 pass 

224 

225 spy_enable.assert_called_once() 

226 spy_disable.assert_called_once() 

227 

228 

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 

235 

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" 

244 

245 assert len(settings.LIST_BASED_SETTING) == 4 

246 

247 async def test_callable_decorator_accessed(self, mocker): 

248 spy_decorator = mocker.spy(modify_settings, "decorate_callable") 

249 

250 @modify_settings(LIST_BASED_SETTING={"append": "four"}) 

251 async def test_callable(): 

252 assert 2 == 2 

253 

254 spy_decorator.assert_called_once() 

255 assert spy_decorator.spy_return == test_callable 

256 

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") 

260 

261 # TODO: test enable and disable when used as a decorator 

262 with modify_settings(LIST_BASED_SETTING={"append": "four"}): 

263 pass 

264 

265 spy_enable.assert_called_once() 

266 spy_disable.assert_called_once()