-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_constant.py
More file actions
97 lines (68 loc) · 2.29 KB
/
test_constant.py
File metadata and controls
97 lines (68 loc) · 2.29 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pytest
from injection import aget_instance, constant, get_instance
class TestConstant:
def test_constant_with_success(self):
@constant
class SomeInjectable: ...
instance_1 = get_instance(SomeInjectable)
instance_2 = get_instance(SomeInjectable)
assert instance_1 is instance_2 is not None
def test_constant_with_recipe(self):
class SomeClass: ...
@constant
def recipe() -> SomeClass:
return SomeClass()
instance_1 = get_instance(SomeClass)
instance_2 = get_instance(SomeClass)
assert instance_1 is instance_2
assert isinstance(instance_1, SomeClass)
async def test_constant_with_async_recipe(self):
class SomeClass: ...
@constant
async def recipe() -> SomeClass:
return SomeClass()
with pytest.raises(RuntimeError):
get_instance(SomeClass)
instance_1 = await aget_instance(SomeClass)
instance_2 = await aget_instance(SomeClass)
assert instance_1 is instance_2
assert isinstance(instance_1, SomeClass)
def test_constant_with_on(self):
class A: ...
@constant(on=A)
class B(A): ...
a = get_instance(A)
assert isinstance(a, B)
def test_constant_with_on_and_several_classes(self):
class A: ...
class B(A): ...
@constant(on=(A, B))
class C(B): ...
a = get_instance(A)
b = get_instance(B)
assert isinstance(a, C)
assert isinstance(b, C)
assert a is b
def test_constant_with_injectable_already_exist_raise_runtime_error(self):
class A: ...
@constant(on=A)
class B(A): ...
with pytest.raises(RuntimeError):
@constant(on=A)
class C(A): ...
def test_constant_with_override(self):
@constant
class A: ...
@constant(on=A, mode="override")
class B(A): ...
a = get_instance(A)
assert isinstance(a, B)
def test_constant_with_multiple_override(self):
@constant
class A: ...
@constant(on=A, mode="override")
class B(A): ...
@constant(on=A, mode="override")
class C(B): ...
a = get_instance(A)
assert isinstance(a, C)