Coverage for tests/test_utils/test_with_xunit.py: 96%

153 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-11 03:57 +0330

1from unittest import TestCase, IsolatedAsyncioTestCase 

2 

3import pytest 

4 

5from lazy_settings.conf import settings 

6from lazy_settings.test.utils import override_settings, modify_settings 

7 

8from . import global_settings 

9 

10settings.clear() 

11settings.register(global_settings) 

12mock = None 

13 

14 

15@override_settings(A_SETTING="new value", NEW_SETTING="this wasn't in the file") 

16class OverridentSettingsUnitTest(TestCase): 

17 @pytest.fixture(autouse=True) 

18 def make_mocker_available(self, mocker): 

19 global mock 

20 mock = mocker 

21 yield mock 

22 mock = None 

23 

24 def test_overriden_settings(self): 

25 self.assertEqual(settings.A_SETTING, "new value") 

26 self.assertEqual(settings.NEW_SETTING, "this wasn't in the file") 

27 

28 @override_settings(A_SETTING="another one") 

29 def test_override_after_override(self): 

30 self.assertEqual(settings.A_SETTING, "another one") 

31 

32 def test_override_with_context_manager(self): 

33 self.assertEqual(settings.A_SETTING, "new value") 

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

35 self.assertEqual(settings.A_SETTING, "context!") 

36 

37 self.assertEqual(settings.A_SETTING, "new value") 

38 

39 def test_class_decorator_is_accessed(self): 

40 spy_decorator = mock.spy(override_settings, "decorate_class") 

41 

42 @override_settings(A_SETTING="a") 

43 class TestClass(TestCase): 

44 def test_something(self): 

45 self.assertEqual(2, 2) 

46 

47 spy_decorator.assert_called_once() 

48 self.assertEqual(spy_decorator.spy_return, TestClass) 

49 

50 def test_callable_decorator_accessed(self): 

51 spy_decorator = mock.spy(override_settings, "decorate_callable") 

52 

53 @override_settings(A_SETTING="a") 

54 def test_callable(): 

55 self.assertEqual(2, 2) 

56 

57 spy_decorator.assert_called_once() 

58 self.assertEqual(spy_decorator.spy_return, test_callable) 

59 

60 def test_enable_and_disable_accessed(self): 

61 spy_enable = mock.spy(override_settings, "enable") 

62 spy_disable = mock.spy(override_settings, "disable") 

63 

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

65 with override_settings(A_SETTING="a"): 

66 pass 

67 

68 spy_enable.assert_called_once() 

69 spy_disable.assert_called_once() 

70 

71 

72@override_settings(A_SETTING="new value", NEW_SETTING="this wasn't in the file") 

73class AsyncOverridentSettingsUnitTest(IsolatedAsyncioTestCase): 

74 @pytest.fixture(autouse=True) 

75 def make_mocker_available(self, mocker): 

76 global mock 

77 mock = mocker 

78 yield mock 

79 mock = None 

80 

81 @override_settings(A_SETTING="another one") 

82 async def test_override_after_override(self): 

83 self.assertEqual(settings.A_SETTING, "another one") 

84 

85 async def test_override_with_context_manager(self): 

86 self.assertEqual(settings.A_SETTING, "new value") 

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

88 self.assertEqual(settings.A_SETTING, "context!") 

89 

90 self.assertEqual(settings.A_SETTING, "new value") 

91 

92 async def test_callable_decorator_accessed(self): 

93 spy_decorator = mock.spy(override_settings, "decorate_callable") 

94 

95 @override_settings(A_SETTING="a") 

96 async def test_callable(): 

97 self.assertEqual(2, 2) 

98 

99 spy_decorator.assert_called_once() 

100 self.assertEqual(spy_decorator.spy_return, test_callable) 

101 

102 async def test_enable_and_disable_accessed(self): 

103 spy_enable = mock.spy(override_settings, "enable") 

104 spy_disable = mock.spy(override_settings, "disable") 

105 

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

107 with override_settings(A_SETTING="a"): 

108 pass 

109 

110 spy_enable.assert_called_once() 

111 spy_disable.assert_called_once() 

112 

113 

114@modify_settings(LIST_BASED_SETTING={"append": "three", "prepend": "zero"}) 

115class ModifySettingsUnitTest(TestCase): 

116 @pytest.fixture(autouse=True) 

117 def make_mocker_available(self, mocker): 

118 global mock 

119 mock = mocker 

120 yield mock 

121 mock = None 

122 

123 def test_modified_settings(self): 

124 self.assertEqual(len(settings.LIST_BASED_SETTING), 4) 

125 self.assertEqual(settings.LIST_BASED_SETTING[0], "zero") 

126 self.assertEqual(settings.LIST_BASED_SETTING[-1], "three") 

127 

128 @modify_settings(LIST_BASED_SETTING={"remove": "two"}) 

129 def test_modify_after_modify(self): 

130 self.assertEqual(len(settings.LIST_BASED_SETTING), 3) 

131 self.assertNotIn("two", settings.LIST_BASED_SETTING) 

132 

133 def test_modify_with_context_manager(self): 

134 self.assertEqual(len(settings.LIST_BASED_SETTING), 4) 

135 with modify_settings( 

136 LIST_BASED_SETTING={"append": "four", "prepend": "less than zero"}, 

137 ): 

138 self.assertEqual(len(settings.LIST_BASED_SETTING), 6) 

139 self.assertEqual(settings.LIST_BASED_SETTING[0], "less than zero") 

140 self.assertEqual(settings.LIST_BASED_SETTING[-1], "four") 

141 

142 self.assertEqual(len(settings.LIST_BASED_SETTING), 4) 

143 

144 def test_class_decorator_is_accessed(self): 

145 spy_decorator = mock.spy(modify_settings, "decorate_class") 

146 

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

148 class TestClass(TestCase): 

149 def test_something(self): 

150 self.assertEqual(2, 2) 

151 

152 spy_decorator.assert_called_once() 

153 self.assertEqual(spy_decorator.spy_return, TestClass) 

154 

155 def test_callable_decorator_accessed(self): 

156 spy_decorator = mock.spy(modify_settings, "decorate_callable") 

157 

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

159 def test_callable(): 

160 self.assertEqual(2, 2) 

161 

162 spy_decorator.assert_called_once() 

163 self.assertEqual(spy_decorator.spy_return, test_callable) 

164 

165 def test_enable_and_disable_accessed(self): 

166 spy_enable = mock.spy(modify_settings, "enable") 

167 spy_disable = mock.spy(modify_settings, "disable") 

168 

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

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

171 pass 

172 

173 spy_enable.assert_called_once() 

174 spy_disable.assert_called_once() 

175 

176 

177@modify_settings(LIST_BASED_SETTING={"append": "three", "prepend": "zero"}) 

178class AsyncModifySettingsUnitTest(IsolatedAsyncioTestCase): 

179 @pytest.fixture(autouse=True) 

180 def make_mocker_available(self, mocker): 

181 global mock 

182 mock = mocker 

183 yield mock 

184 mock = None 

185 

186 @modify_settings(LIST_BASED_SETTING={"remove": "two"}) 

187 async def test_modify_after_modify(self): 

188 self.assertEqual(len(settings.LIST_BASED_SETTING), 3) 

189 self.assertNotIn("two", settings.LIST_BASED_SETTING) 

190 

191 async def test_modify_with_context_manager(self): 

192 self.assertEqual(len(settings.LIST_BASED_SETTING), 4) 

193 with modify_settings( 

194 LIST_BASED_SETTING={"append": "four", "prepend": "less than zero"}, 

195 ): 

196 self.assertEqual(len(settings.LIST_BASED_SETTING), 6) 

197 self.assertEqual(settings.LIST_BASED_SETTING[0], "less than zero") 

198 self.assertEqual(settings.LIST_BASED_SETTING[-1], "four") 

199 

200 self.assertEqual(len(settings.LIST_BASED_SETTING), 4) 

201 

202 async def test_callable_decorator_accessed(self): 

203 spy_decorator = mock.spy(modify_settings, "decorate_callable") 

204 

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

206 async def test_callable(): 

207 self.assertEqual(2, 2) 

208 

209 spy_decorator.assert_called_once() 

210 self.assertEqual(spy_decorator.spy_return, test_callable) 

211 

212 async def test_enable_and_disable_accessed(self): 

213 spy_enable = mock.spy(modify_settings, "enable") 

214 spy_disable = mock.spy(modify_settings, "disable") 

215 

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

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

218 pass 

219 

220 spy_enable.assert_called_once() 

221 spy_disable.assert_called_once()