Skip to content

Commit a3727aa

Browse files
committed
feat: Add testing specifying the resource type creating scoping resources.
1 parent 6f4d23c commit a3727aa

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

tests/unit/containers/instance/test_async_resources_py36.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,121 @@ class Container(containers.DeclarativeContainer):
145145
await container.shutdown_resources()
146146
assert initialized_resources == ["r1", "r2", "r3", "r1", "r2", "r3"]
147147
assert shutdown_resources == ["r3", "r2", "r1", "r3", "r2", "r1"]
148+
149+
150+
@mark.asyncio
151+
async def test_init_and_shutdown_scoped_resources():
152+
initialized_resources = []
153+
shutdown_resources = []
154+
155+
def _sync_resource(name, **_):
156+
initialized_resources.append(name)
157+
yield name
158+
shutdown_resources.append(name)
159+
160+
async def _async_resource(name, **_):
161+
initialized_resources.append(name)
162+
yield name
163+
shutdown_resources.append(name)
164+
165+
166+
class ResourceA(providers.Resource):
167+
pass
168+
169+
170+
class ResourceB(providers.Resource):
171+
pass
172+
173+
174+
class Container(containers.DeclarativeContainer):
175+
resource_a = ResourceA(
176+
_sync_resource,
177+
name="ra1",
178+
)
179+
resource_b1 = ResourceB(
180+
_sync_resource,
181+
name="rb1",
182+
r1=resource_a,
183+
)
184+
resource_b2 = ResourceB(
185+
_async_resource,
186+
name="rb2",
187+
r2=resource_b1,
188+
)
189+
190+
container = Container()
191+
192+
container.init_resources(resource_type=ResourceA)
193+
assert initialized_resources == ["ra1"]
194+
assert shutdown_resources == []
195+
196+
container.shutdown_resources(resource_type=ResourceA)
197+
assert initialized_resources == ["ra1"]
198+
assert shutdown_resources == ["ra1"]
199+
200+
await container.init_resources(resource_type=ResourceB)
201+
assert initialized_resources == ["ra1", "ra1", "rb1", "rb2"]
202+
assert shutdown_resources == ["ra1"]
203+
204+
await container.shutdown_resources(resource_type=ResourceB)
205+
assert initialized_resources == ["ra1", "ra1", "rb1", "rb2"]
206+
assert shutdown_resources == ["ra1", "rb2", "rb1"]
207+
208+
209+
@mark.asyncio
210+
async def test_init_and_shutdown_all_scoped_resources_using_default_value():
211+
initialized_resources = []
212+
shutdown_resources = []
213+
214+
def _sync_resource(name, **_):
215+
initialized_resources.append(name)
216+
yield name
217+
shutdown_resources.append(name)
218+
219+
async def _async_resource(name, **_):
220+
initialized_resources.append(name)
221+
yield name
222+
shutdown_resources.append(name)
223+
224+
225+
class ResourceA(providers.Resource):
226+
pass
227+
228+
229+
class ResourceB(providers.Resource):
230+
pass
231+
232+
233+
class Container(containers.DeclarativeContainer):
234+
resource_a = ResourceA(
235+
_sync_resource,
236+
name="r1",
237+
)
238+
resource_b1 = ResourceB(
239+
_sync_resource,
240+
name="r2",
241+
r1=resource_a,
242+
)
243+
resource_b2 = ResourceB(
244+
_async_resource,
245+
name="r3",
246+
r2=resource_b1,
247+
)
248+
249+
container = Container()
250+
251+
await container.init_resources()
252+
assert initialized_resources == ["r1", "r2", "r3"]
253+
assert shutdown_resources == []
254+
255+
await container.shutdown_resources()
256+
assert initialized_resources == ["r1", "r2", "r3"]
257+
assert shutdown_resources == ["r3", "r2", "r1"]
258+
259+
await container.init_resources()
260+
assert initialized_resources == ["r1", "r2", "r3", "r1", "r2", "r3"]
261+
assert shutdown_resources == ["r3", "r2", "r1"]
262+
263+
await container.shutdown_resources()
264+
assert initialized_resources == ["r1", "r2", "r3", "r1", "r2", "r3"]
265+
assert shutdown_resources == ["r3", "r2", "r1", "r3", "r2", "r1"]

0 commit comments

Comments
 (0)