-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_9_inherit_container.py
More file actions
71 lines (48 loc) · 1.76 KB
/
test_9_inherit_container.py
File metadata and controls
71 lines (48 loc) · 1.76 KB
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
from ghoshell_container import Container, Provider, IoCContainer, INSTANCE
class _Foo:
def __init__(self):
self.foo = 1
class _FooSingletonButInheritanceProvider(Provider[_Foo]):
def singleton(self) -> bool:
return True
def inheritable(self) -> bool:
return True
def factory(self, con: IoCContainer) -> INSTANCE:
return _Foo()
def test_container_without_bind():
parent = Container(name="parent")
assert parent.parent is None
child = Container(parent, name="child")
assert child.parent is parent
parent.set(_Foo, _Foo())
assert parent.bound(_Foo)
assert child.bound(_Foo)
assert child.force_fetch(_Foo) is parent.force_fetch(_Foo)
assert not child.bound(_Foo, recursively=False)
def test_inherit_bind():
parent = Container(name="parent")
# if singleton is false, rebound at child
parent.bind(_Foo, singleton=False)
child = Container(parent, name="child")
# child inherit the provider
assert child.bound(_Foo, recursively=False)
def test_inheritable_provider():
parent = Container(name="parent")
parent.register(_FooSingletonButInheritanceProvider())
child = Container(parent, name="child")
# child also bind
assert child.bound(_Foo, recursively=False)
# child instance is not the same as parent
assert child.force_fetch(_Foo) is not parent.force_fetch(_Foo)
def test_child_shutdown_when_parent_shutdown():
parent = Container(name="parent")
child = Container(parent, name="child")
child.bind(_Foo, singleton=True)
def shutdown(_foo: _Foo) -> None:
_foo.foo = 0
child.add_shutdown(shutdown)
foo = child.force_fetch(_Foo)
assert foo.foo == 1
# and child shutdown
parent.shutdown()
assert foo.foo == 0