1
|
|
|
from lagom import Container, Singleton |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class InitialDep: |
5
|
|
|
pass |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class SomeMockForTesting(InitialDep): |
9
|
|
|
pass |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class SomeOtherMockForTesting(InitialDep): |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_container_can_be_cloned_and_maintains_separate_deps(container: Container): |
17
|
|
|
new_container = container.clone() |
18
|
|
|
new_container.define(InitialDep, lambda: SomeMockForTesting()) |
19
|
|
|
|
20
|
|
|
assert isinstance(new_container[InitialDep], SomeMockForTesting) |
21
|
|
|
assert isinstance(container[InitialDep], InitialDep) |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_a_cloned_container_can_have_deps_overwritten(container: Container): |
25
|
|
|
container.define(InitialDep, lambda: SomeMockForTesting()) |
26
|
|
|
new_container = container.clone() |
27
|
|
|
new_container.define(InitialDep, lambda: SomeOtherMockForTesting()) |
28
|
|
|
|
29
|
|
|
assert isinstance(new_container[InitialDep], SomeOtherMockForTesting) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def test_a_clone_shares_the_parents_singleton_instances(container: Container): |
33
|
|
|
container.define(InitialDep, Singleton(InitialDep)) |
34
|
|
|
new_container = container.clone() |
35
|
|
|
|
36
|
|
|
assert id(container[InitialDep]) == id(new_container[InitialDep]) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def test_overwriting_a_singleton_creates_a_new_one(container: Container): |
40
|
|
|
container.define(InitialDep, Singleton(InitialDep)) |
41
|
|
|
new_container = container.clone() |
42
|
|
|
new_container.define(InitialDep, Singleton(InitialDep)) |
43
|
|
|
|
44
|
|
|
assert id(container[InitialDep]) != id(new_container[InitialDep]) |
45
|
|
|
|