-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_5_make.py
More file actions
83 lines (58 loc) · 1.49 KB
/
test_5_make.py
File metadata and controls
83 lines (58 loc) · 1.49 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
72
73
74
75
76
77
78
79
80
81
82
83
import pytest
from ghoshell_container import Container
class _Foo:
pass
class _Bar(_Foo):
pass
class _Baz:
def __init__(self, foo: _Foo) -> None:
self.foo = foo
class _Zoo:
def __init__(self, foo: _Foo, a: int) -> None:
self.foo = foo
self.a = a
class _Cat:
def __init__(self, foo: _Foo, zoo: _Zoo) -> None:
self.foo = foo
self.zoo = zoo
def test_make():
c = Container()
assert isinstance(c.make(_Foo), _Foo)
def test_make_with_kwargs():
c = Container()
zoo = c.make(_Zoo, a=2)
assert isinstance(zoo, _Zoo)
assert zoo.a == 2
def test_make_exception_without_kwargs():
c = Container()
with pytest.raises(RuntimeError):
zoo = c.make(_Zoo)
def test_make_with_bind():
c = Container()
c.bind(_Foo, _Bar)
assert isinstance(c.make(_Foo), _Bar)
def test_make_with_di():
c = Container()
c.bind(_Foo, _Bar)
assert isinstance(c.make(_Baz), _Baz)
assert isinstance(c.make(_Baz).foo, _Bar)
def test_recursive_make():
import threading
c = Container()
c.bind(_Foo, _Bar)
c.bind(_Zoo, kwargs=dict(a=1))
def assert_zoo():
zoo = c.make(_Zoo)
assert isinstance(zoo, _Zoo)
assert zoo.a == 1
assert isinstance(zoo.foo, _Bar)
# single time
assert_zoo()
ths = []
# concurrent making
for i in range(10):
t = threading.Thread(target=assert_zoo)
t.start()
ths.append(t)
for t in ths:
t.join()