-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_DI.py
253 lines (201 loc) · 9.11 KB
/
test_DI.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
from PythonDI import DIContainer, UnregisteredAction, UnregisteredType
import pytest
from pydantic import BaseModel
def test_register():
di = DIContainer()
di.register(HelloResponse)
def test_locate():
di = DIContainer()
di.register(HelloResponse)
response = di.locate(HelloResponse, [])
assert response is not None # HelloResponse located
assert response.body == "" # body populated with default value for str
assert response.goodbye is not None # GoodbyeResponse not registered. default value used
assert response.goodbye.test is None # TestResponse not located because GoodbyeResponse was default. test never populated
response.body = "test"
response2 = di.locate(HelloResponse, [])
assert response != response2
assert response.body != response2.body
def test_locate_unregistered_none():
di = DIContainer(UnregisteredAction.NONE)
di.register(HelloResponse)
response = di.locate(HelloResponse, [])
assert response is not None # HelloResponse located
assert response.body is None # str not registered. value is None
assert response.goodbye is None # GoodbyeResponse not registered. value is None
def test_locate_unregistered_exception():
di = DIContainer(UnregisteredAction.EXCEPTION)
di.register(HelloResponse)
with pytest.raises(UnregisteredType) as ex:
_ = di.locate(HelloResponse, [])
assert ex.value.args[0] == f"{str} not registered with DIContainer."
def test_locate_unregistered_register():
di = DIContainer(UnregisteredAction.REGISTER)
di.register(HelloResponse)
_ = di.locate(HelloResponse, [])
response = di.locate(HelloResponse, [])
assert response is not None # HelloResponse located
assert response.body == "" # body populated with default value for str
assert response.goodbye is not None # GoodbyeResponse located
assert response.goodbye.test is not None # TestResponse located
assert response.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.goodbye.test.body == "" # TestResponse.body populated with default value for str
def test_locate_dependencies():
di = DIContainer()
di.register(OtherResponse)
di.register(GoodbyeResponse)
di.register(HelloResponse)
response = di.locate(HelloResponse, [])
assert response is not None # HelloResponse located
assert response.body == "" # body populated with default value for str
assert response.goodbye is not None # GoodbyeResponse located
assert response.goodbye.test is not None # TestResponse located
assert response.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.goodbye.test.body == "" # TestResponse.body populated with default value for str
def test_locate_pydantic_dependencies():
di = DIContainer()
di.register(PydanticOtherResponse)
di.register(PydanticGoodbyeResponse)
di.register(PydanticHelloResponse)
response = di.locate(PydanticHelloResponse, [])
assert response is not None # HelloResponse located
assert response.body == "" # body populated with default value for str
assert response.goodbye is not None # GoodbyeResponse located
assert response.goodbye.test is not None # TestResponse located
assert response.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.goodbye.test.body == "" # TestResponse.body populated with default value for str
def test_locate_dependencies_mixed():
di = DIContainer()
di.register(OtherResponse)
di.register(GoodbyeResponse)
di.register(HelloResponse)
di.register(PydanticOtherResponse)
di.register(PydanticGoodbyeResponse)
di.register(PydanticHelloResponse)
di.register(MixedResponse)
response: MixedResponse = di.locate(MixedResponse)
assert response is not None # HelloResponse located
assert response.hello is not None
assert response.hello.body == "" # body populated with default value for str
assert response.hello.goodbye is not None # GoodbyeResponse located
assert response.hello.goodbye.test is not None # TestResponse located
assert response.hello.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.hello.goodbye.test.body == "" # TestResponse.body populated with default value for str
assert response.py_hello is not None
assert response.py_hello is not None
assert response.py_hello.body == "" # body populated with default value for str
assert response.py_hello.goodbye is not None # GoodbyeResponse located
assert response.py_hello.goodbye.test is not None # TestResponse located
assert response.py_hello.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.py_hello.goodbye.test.body == "" # TestResponse.body populated with default value for str
def test_locate_with_param():
di = DIContainer()
di.register(HelloResponse)
response = di.locate(HelloResponse, ["Hello"])
assert response is not None # HelloResponse located
assert response.body == "Hello" # body param applies
def test_locate_dependencies_with_param():
di = DIContainer()
di.register(OtherResponse)
di.register(GoodbyeResponse)
di.register(HelloResponse)
response = di.locate(HelloResponse, ["Hello"])
assert response is not None # HelloResponse located
assert response.body == "Hello" # body populated with default value for str
assert response.goodbye is not None # GoodbyeResponse located
assert response.goodbye.test is not None # TestResponse located
assert response.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.goodbye.test.body == "" # TestResponse.body populated with default value for str
def test_locate_with_param_pydantic():
di = DIContainer()
di.register(PydanticHelloResponse)
response = di.locate(PydanticHelloResponse, ["Hello"])
assert response is not None # HelloResponse located
assert response.body == "Hello" # body param applies
def test_locate_pydantic_dependencies_with_param():
di = DIContainer()
di.register(PydanticOtherResponse)
di.register(PydanticGoodbyeResponse)
di.register(PydanticHelloResponse)
response = di.locate(PydanticHelloResponse, ["Hello"])
assert response is not None # HelloResponse located
assert response.body == "Hello" # body populated with default value for str
assert response.goodbye is not None # GoodbyeResponse located
assert response.goodbye.test is not None # TestResponse located
assert response.goodbye.test.test == 0 # TestResponse.test populated with default value for int
assert response.goodbye.test.body == "" # TestResponse.body populated with default value for str
def test_register_instance():
di = DIContainer()
di.register_instance(B, B())
def test_register_instance_none():
di = DIContainer()
di.register_instance(B)
def test_locate_instance():
di = DIContainer()
b1 = B()
di.register_instance(B, b1)
b2 = di.locate(B)
assert b1 == b2
def test_locate_instance_none():
di = DIContainer()
di.register_instance(B)
b1 = di.locate(B)
b2 = di.locate(B)
assert b1 == b2
def test_locate_all():
di = DIContainer()
di.register(A)
di.register(B)
di.register(C)
d = D()
di.register_instance(D, d)
# locate_all test
all_A = di.locate_all(A)
assert len(all_A) == 4 # Includes A, B, C, and D
assert d in all_A
all_B = di.locate_all(B)
assert len(all_B) == 2 # Includes B and C
assert d not in all_B
def test_locate_dependencies_required_defaults():
di = DIContainer()
di.register(RequiredHelloResponse)
with pytest.raises(TypeError):
_ = di.locate(RequiredHelloResponse, [])
class OtherResponse:
def __init__(self, test: int, body: str):
self.test = test
self.body = body
class GoodbyeResponse:
def __init__(self, test: OtherResponse = None):
self.test = test
class HelloResponse:
def __init__(self, body: str, goodbye: GoodbyeResponse):
self.body = body
self.goodbye = goodbye
class PydanticOtherResponse(BaseModel):
test: int = 0
body: str = ""
class PydanticGoodbyeResponse(BaseModel):
test: PydanticOtherResponse = None
class PydanticHelloResponse(BaseModel):
body: str = ""
goodbye: PydanticGoodbyeResponse = None
class MixedResponse:
def __init__(self, hello: HelloResponse = None, py_hello: PydanticHelloResponse = None):
self.hello = hello
self.py_hello = py_hello
class RequiredGoodbyeResponse:
def __init__(self, test: OtherResponse):
self.test = test
class RequiredHelloResponse:
def __init__(self, body: str, goodbye: RequiredGoodbyeResponse):
self.body = body
self.goodbye = goodbye
class A:
pass
class B(A):
pass
class C(B):
pass
class D(A):
pass