-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_singleton.py
More file actions
200 lines (146 loc) · 4.57 KB
/
test_singleton.py
File metadata and controls
200 lines (146 loc) · 4.57 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
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
from dataclasses import dataclass
import pytest
from pydantic import BaseModel
from injection import aget_instance, get_instance, singleton
class TestSingleton:
def test_singleton_with_success(self):
@singleton
class SomeInjectable: ...
instance_1 = get_instance(SomeInjectable)
instance_2 = get_instance(SomeInjectable)
assert instance_1 is instance_2
def test_singleton_with_recipe(self):
class SomeClass: ...
@singleton
def recipe() -> SomeClass:
return SomeClass()
instance_1 = get_instance(SomeClass)
instance_2 = get_instance(SomeClass)
assert instance_1 is instance_2
async def test_singleton_with_async_recipe(self):
class SomeClass: ...
@singleton
async def recipe() -> SomeClass:
return SomeClass()
with pytest.raises(RuntimeError):
get_instance(SomeClass)
instance_1 = await aget_instance(SomeClass)
instance_2 = get_instance(SomeClass)
assert instance_1 is instance_2
def test_singleton_with_recipe_and_union(self):
class A: ...
class B(A): ...
@singleton
def recipe() -> A | B:
return B()
a = get_instance(A)
b = get_instance(B)
assert a is b
assert isinstance(a, B)
def test_singleton_with_recipe_and_no_return_type(self):
class SomeClass: ...
@singleton
def recipe():
return SomeClass() # pragma: no cover
assert get_instance(SomeClass) is NotImplemented
def test_singleton_with_on(self):
class A: ...
@singleton(on=A)
class B(A): ...
a = get_instance(A)
assert isinstance(a, B)
def test_singleton_with_on_and_several_classes(self):
class A: ...
class B(A): ...
@singleton(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_singleton_with_inject(self):
@singleton
class A: ...
@singleton
class B:
def __init__(self, __a: A):
self.a = __a
a = get_instance(A)
b = get_instance(B)
assert isinstance(a, A)
assert isinstance(b, B)
assert isinstance(b.a, A)
assert a is b.a
def test_singleton_with_dataclass_and_inject(self):
@singleton
class A: ...
@singleton
@dataclass(frozen=True, slots=True)
class B:
a: A
a = get_instance(A)
b = get_instance(B)
assert isinstance(a, A)
assert isinstance(b, B)
assert isinstance(b.a, A)
assert a is b.a
def test_singleton_with_pydantic_model_and_inject(self):
@singleton
class A(BaseModel): ...
@singleton
class B(BaseModel):
a: A
a = get_instance(A)
b = get_instance(B)
assert isinstance(a, A)
assert isinstance(b, B)
assert isinstance(b.a, A)
assert a is b.a
def test_singleton_with_recipe_and_inject(self):
@singleton
class A: ...
class B: ...
@singleton
def recipe(__a: A) -> B:
assert isinstance(__a, A)
assert __a is a
return B()
a = get_instance(A)
b = get_instance(B)
assert isinstance(a, A)
assert isinstance(b, B)
def test_singleton_with_injectable_already_exist_raise_runtime_error(self):
class A: ...
@singleton(on=A)
class B(A): ...
with pytest.raises(RuntimeError):
@singleton(on=A)
class C(A): ...
def test_singleton_with_override(self):
@singleton
class A: ...
@singleton(on=A, mode="override")
class B(A): ...
a = get_instance(A)
assert isinstance(a, B)
def test_singleton_with_multiple_override(self):
@singleton
class A: ...
@singleton(on=A, mode="override")
class B(A): ...
@singleton(on=A, mode="override")
class C(B): ...
a = get_instance(A)
assert isinstance(a, C)
async def test_singleton_with_circular_dependency_raise_recursion_error(self):
class A: ...
@singleton
@dataclass
class B:
a: A
@singleton
async def a_factory(_b: B) -> A:
return A() # pragma: no cover
with pytest.raises(RecursionError):
await aget_instance(A)