-
Notifications
You must be signed in to change notification settings - Fork 288
/
test_wiring_string_ids_py36.py
435 lines (337 loc) · 14.7 KB
/
test_wiring_string_ids_py36.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
from decimal import Decimal
import unittest
from dependency_injector.wiring import (
wire,
Provide,
Provider,
Closing,
)
from dependency_injector import containers, providers, errors
# Runtime import to avoid syntax errors in samples on Python < 3.5
import os
_TOP_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
'../',
)),
)
_SAMPLES_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
'../samples/',
)),
)
import sys
sys.path.append(_TOP_DIR)
sys.path.append(_SAMPLES_DIR)
from asyncutils import AsyncTestCase
from wiringstringidssamples import module, package
from wiringstringidssamples.service import Service
from wiringstringidssamples.container import Container, SubContainer
class WiringTest(unittest.TestCase):
container: Container
def setUp(self) -> None:
self.container = Container(config={'a': {'b': {'c': 10}}})
self.container.wire(
modules=[module],
packages=[package],
)
self.addCleanup(self.container.unwire)
def test_package_lookup(self):
from wiringstringidssamples.package import test_package_function
service = test_package_function()
self.assertIsInstance(service, Service)
def test_package_subpackage_lookup(self):
from wiringstringidssamples.package.subpackage import test_package_function
service = test_package_function()
self.assertIsInstance(service, Service)
def test_package_submodule_lookup(self):
from wiringstringidssamples.package.subpackage.submodule import test_function
service = test_function()
self.assertIsInstance(service, Service)
def test_module_attributes_wiring(self):
self.assertIsInstance(module.service, Service)
self.assertIsInstance(module.service_provider(), Service)
self.assertIsInstance(module.undefined, Provide)
def test_class_wiring(self):
test_class_object = module.TestClass()
self.assertIsInstance(test_class_object.service, Service)
def test_class_wiring_context_arg(self):
test_service = self.container.service()
test_class_object = module.TestClass(service=test_service)
self.assertIs(test_class_object.service, test_service)
def test_class_method_wiring(self):
test_class_object = module.TestClass()
service = test_class_object.method()
self.assertIsInstance(service, Service)
def test_class_classmethod_wiring(self):
service = module.TestClass.class_method()
self.assertIsInstance(service, Service)
def test_instance_classmethod_wiring(self):
instance = module.TestClass()
service = instance.class_method()
self.assertIsInstance(service, Service)
def test_class_staticmethod_wiring(self):
service = module.TestClass.static_method()
self.assertIsInstance(service, Service)
def test_instance_staticmethod_wiring(self):
instance = module.TestClass()
service = instance.static_method()
self.assertIsInstance(service, Service)
def test_class_attribute_wiring(self):
self.assertIsInstance(module.TestClass.service, Service)
self.assertIsInstance(module.TestClass.service_provider(), Service)
self.assertIsInstance(module.TestClass.undefined, Provide)
def test_function_wiring(self):
service = module.test_function()
self.assertIsInstance(service, Service)
def test_function_wiring_context_arg(self):
test_service = self.container.service()
service = module.test_function(service=test_service)
self.assertIs(service, test_service)
def test_function_wiring_provider(self):
service = module.test_function_provider()
self.assertIsInstance(service, Service)
def test_function_wiring_provider_context_arg(self):
test_service = self.container.service()
service = module.test_function_provider(service_provider=lambda: test_service)
self.assertIs(service, test_service)
def test_configuration_option(self):
(
value_int,
value_float,
value_str,
value_decimal,
value_required,
value_required_int,
value_required_float,
value_required_str,
value_required_decimal,
) = module.test_config_value()
self.assertEqual(value_int, 10)
self.assertEqual(value_float, 10.0)
self.assertEqual(value_str, '10')
self.assertEqual(value_decimal, Decimal(10))
self.assertEqual(value_required, 10)
self.assertEqual(value_required_int, 10)
self.assertEqual(value_required_float, 10.0)
self.assertEqual(value_required_str, '10')
self.assertEqual(value_required_decimal, Decimal(10))
def test_configuration_option_required_undefined(self):
self.container.config.reset_override()
with self.assertRaisesRegex(errors.Error, 'Undefined configuration option "config.a.b.c"'):
module.test_config_value_required_undefined()
def test_provide_provider(self):
service = module.test_provide_provider()
self.assertIsInstance(service, Service)
def test_provided_instance(self):
class TestService:
foo = {
'bar': lambda: 10,
}
with self.container.service.override(TestService()):
some_value = module.test_provided_instance()
self.assertEqual(some_value, 10)
def test_subcontainer(self):
some_value = module.test_subcontainer_provider()
self.assertEqual(some_value, 1)
def test_config_invariant(self):
config = {
'option': {
'a': 1,
'b': 2,
},
'switch': 'a',
}
self.container.config.from_dict(config)
value_default = module.test_config_invariant()
self.assertEqual(value_default, 1)
with self.container.config.switch.override('a'):
value_a = module.test_config_invariant()
self.assertEqual(value_a, 1)
with self.container.config.switch.override('b'):
value_b = module.test_config_invariant()
self.assertEqual(value_b, 2)
def test_wire_with_class_error(self):
with self.assertRaises(Exception):
wire(
container=Container,
modules=[module],
)
def test_unwire_function(self):
self.container.unwire()
self.assertIsInstance(module.test_function(), Provide)
def test_unwire_class(self):
self.container.unwire()
test_class_object = module.TestClass()
self.assertIsInstance(test_class_object.service, Provide)
def test_unwire_class_method(self):
self.container.unwire()
test_class_object = module.TestClass()
self.assertIsInstance(test_class_object.method(), Provide)
def test_unwire_package_function(self):
self.container.unwire()
from wiringstringidssamples.package.subpackage.submodule import test_function
self.assertIsInstance(test_function(), Provide)
def test_unwire_package_function_by_reference(self):
from wiringstringidssamples.package.subpackage import submodule
self.container.unwire()
self.assertIsInstance(submodule.test_function(), Provide)
def test_unwire_module_attributes(self):
self.container.unwire()
self.assertIsInstance(module.service, Provide)
self.assertIsInstance(module.service_provider, Provider)
self.assertIsInstance(module.undefined, Provide)
def test_unwire_class_attributes(self):
self.container.unwire()
self.assertIsInstance(module.TestClass.service, Provide)
self.assertIsInstance(module.TestClass.service_provider, Provider)
self.assertIsInstance(module.TestClass.undefined, Provide)
def test_wire_multiple_containers(self):
sub_container = SubContainer()
sub_container.wire(
modules=[module],
packages=[package],
)
self.addCleanup(sub_container.unwire)
service, some_value = module.test_provide_from_different_containers()
self.assertIsInstance(service, Service)
self.assertEqual(some_value, 1)
def test_closing_resource(self):
from wiringstringidssamples import resourceclosing
resourceclosing.Service.reset_counter()
container = resourceclosing.Container()
container.wire(modules=[resourceclosing])
self.addCleanup(container.unwire)
result_1 = resourceclosing.test_function()
self.assertIsInstance(result_1, resourceclosing.Service)
self.assertEqual(result_1.init_counter, 1)
self.assertEqual(result_1.shutdown_counter, 1)
result_2 = resourceclosing.test_function()
self.assertIsInstance(result_2, resourceclosing.Service)
self.assertEqual(result_2.init_counter, 2)
self.assertEqual(result_2.shutdown_counter, 2)
self.assertIsNot(result_1, result_2)
def test_closing_resource_context(self):
from wiringstringidssamples import resourceclosing
resourceclosing.Service.reset_counter()
service = resourceclosing.Service()
container = resourceclosing.Container()
container.wire(modules=[resourceclosing])
self.addCleanup(container.unwire)
result_1 = resourceclosing.test_function(service=service)
self.assertIs(result_1, service)
self.assertEqual(result_1.init_counter, 0)
self.assertEqual(result_1.shutdown_counter, 0)
result_2 = resourceclosing.test_function(service=service)
self.assertIs(result_2, service)
self.assertEqual(result_2.init_counter, 0)
self.assertEqual(result_2.shutdown_counter, 0)
def test_class_decorator(self):
service = module.test_class_decorator()
self.assertIsInstance(service, Service)
def test_container(self):
service = module.test_container()
self.assertIsInstance(service, Service)
class WiringAndFastAPITest(unittest.TestCase):
container: Container
def test_bypass_marker_injection(self):
container = Container()
container.wire(modules=[module])
self.addCleanup(container.unwire)
service = module.test_function(service=Provide[Container.service])
self.assertIsInstance(service, Service)
def test_closing_resource_bypass_marker_injection(self):
from wiringstringidssamples import resourceclosing
resourceclosing.Service.reset_counter()
container = resourceclosing.Container()
container.wire(modules=[resourceclosing])
self.addCleanup(container.unwire)
result_1 = resourceclosing.test_function(
service=Closing[Provide[resourceclosing.Container.service]],
)
self.assertIsInstance(result_1, resourceclosing.Service)
self.assertEqual(result_1.init_counter, 1)
self.assertEqual(result_1.shutdown_counter, 1)
result_2 = resourceclosing.test_function(
service=Closing[Provide[resourceclosing.Container.service]],
)
self.assertIsInstance(result_2, resourceclosing.Service)
self.assertEqual(result_2.init_counter, 2)
self.assertEqual(result_2.shutdown_counter, 2)
self.assertIsNot(result_1, result_2)
class WireDynamicContainerTest(unittest.TestCase):
def test_wire(self):
sub = containers.DynamicContainer()
sub.int_object = providers.Object(1)
container = containers.DynamicContainer()
container.config = providers.Configuration()
container.service = providers.Factory(Service)
container.sub = sub
container.wire(
modules=[module],
packages=[package],
)
self.addCleanup(container.unwire)
service = module.test_function()
self.assertIsInstance(service, Service)
class WiringAsyncInjectionsTest(AsyncTestCase):
def test_async_injections(self):
from wiringstringidssamples import asyncinjections
container = asyncinjections.Container()
container.wire(modules=[asyncinjections])
self.addCleanup(container.unwire)
asyncinjections.resource1.reset_counters()
asyncinjections.resource2.reset_counters()
resource1, resource2 = self._run(asyncinjections.async_injection())
self.assertIs(resource1, asyncinjections.resource1)
self.assertEqual(asyncinjections.resource1.init_counter, 1)
self.assertEqual(asyncinjections.resource1.shutdown_counter, 0)
self.assertIs(resource2, asyncinjections.resource2)
self.assertEqual(asyncinjections.resource2.init_counter, 1)
self.assertEqual(asyncinjections.resource2.shutdown_counter, 0)
def test_async_injections_with_closing(self):
from wiringstringidssamples import asyncinjections
container = asyncinjections.Container()
container.wire(modules=[asyncinjections])
self.addCleanup(container.unwire)
asyncinjections.resource1.reset_counters()
asyncinjections.resource2.reset_counters()
resource1, resource2 = self._run(asyncinjections.async_injection_with_closing())
self.assertIs(resource1, asyncinjections.resource1)
self.assertEqual(asyncinjections.resource1.init_counter, 1)
self.assertEqual(asyncinjections.resource1.shutdown_counter, 1)
self.assertIs(resource2, asyncinjections.resource2)
self.assertEqual(asyncinjections.resource2.init_counter, 1)
self.assertEqual(asyncinjections.resource2.shutdown_counter, 1)
resource1, resource2 = self._run(asyncinjections.async_injection_with_closing())
self.assertIs(resource1, asyncinjections.resource1)
self.assertEqual(asyncinjections.resource1.init_counter, 2)
self.assertEqual(asyncinjections.resource1.shutdown_counter, 2)
self.assertIs(resource2, asyncinjections.resource2)
self.assertEqual(asyncinjections.resource2.init_counter, 2)
self.assertEqual(asyncinjections.resource2.shutdown_counter, 2)
# class AutoLoaderTest(unittest.TestCase):
#
# container: Container
#
# def setUp(self) -> None:
# self.container = Container(config={'a': {'b': {'c': 10}}})
# importlib.reload(module)
#
# def tearDown(self) -> None:
# with contextlib.suppress(ValueError):
# unregister_loader_containers(self.container)
#
# self.container.unwire()
#
# @classmethod
# def tearDownClass(cls) -> None:
# importlib.reload(module)
#
# def test_register_container(self):
# register_loader_containers(self.container)
# importlib.reload(module)
#
# service = module.test_function()
# self.assertIsInstance(service, Service)