1 | from typing import Optional |
||
2 | |||
3 | import pytest |
||
4 | |||
5 | from lagom import Container, Singleton, ExplicitContainer |
||
6 | from lagom.exceptions import InvalidDependencyDefinition, DependencyNotDefined |
||
7 | |||
8 | |||
9 | class SomethingToBuild: |
||
10 | pass |
||
11 | |||
12 | |||
13 | class SomethingBiggerToBuild: |
||
14 | def __init__(self, inner: SomethingToBuild): |
||
15 | pass |
||
16 | |||
17 | |||
18 | def test_dependencies_must_be_explicitly_defined(explicit_container: Container): |
||
19 | with pytest.raises(DependencyNotDefined) as e_info: |
||
20 | explicit_container.resolve(SomethingToBuild) |
||
21 | |||
22 | assert ( |
||
23 | str(e_info.value) |
||
24 | == "SomethingToBuild has not been defined. In an explict container all dependencies must be defined" |
||
25 | ) |
||
26 | |||
27 | |||
28 | def test_the_error_suppression_flag_is_honoured(explicit_container: Container): |
||
29 | assert explicit_container.resolve(SomethingToBuild, suppress_error=True) is None |
||
30 | |||
31 | |||
32 | def test_an_can_build_with_basic_lambdas(explicit_container: Container): |
||
33 | explicit_container[SomethingToBuild] = lambda: SomethingToBuild() |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
34 | |||
35 | built_object = explicit_container.resolve(SomethingToBuild) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
36 | assert isinstance(built_object, SomethingToBuild) |
||
37 | |||
38 | |||
39 | def test_aliases_arent_valid_as_they_require_reflection(explicit_container: Container): |
||
40 | with pytest.raises(InvalidDependencyDefinition) as e_info: |
||
41 | explicit_container[SomethingBiggerToBuild] = SomethingBiggerToBuild |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
42 | |||
43 | assert str(e_info.value) == "Aliases are not valid in an explicit container" |
||
44 | |||
45 | |||
46 | def test_singletons_with_aliases_arent_valid_as_they_require_reflection( |
||
47 | explicit_container: Container, |
||
48 | ): |
||
49 | with pytest.raises(InvalidDependencyDefinition) as e_info: |
||
50 | explicit_container[SomethingBiggerToBuild] = Singleton(SomethingBiggerToBuild) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
51 | |||
52 | assert ( |
||
53 | str(e_info.value) |
||
54 | == "Aliases are not valid inside singletons in an explicit container" |
||
55 | ) |
||
56 | |||
57 | |||
58 | def test_the_inner_dependencies_dont_have_to_be_defined_in_the_container( |
||
59 | explicit_container: Container, |
||
60 | ): |
||
61 | explicit_container[SomethingBiggerToBuild] = lambda: SomethingBiggerToBuild( |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
62 | SomethingToBuild() |
||
63 | ) |
||
64 | |||
65 | built_object = explicit_container.resolve(SomethingBiggerToBuild) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
66 | assert isinstance(built_object, SomethingBiggerToBuild) |
||
67 | |||
68 | # We still can't build the inner object directly |
||
69 | assert explicit_container.resolve(SomethingToBuild, suppress_error=True) is None |
||
70 | |||
71 | |||
72 | def test_optional_deps_can_be_injected_if_they_can_be_built( |
||
73 | explicit_container: Container, |
||
74 | ): |
||
75 | explicit_container[SomethingToBuild] = lambda: SomethingToBuild() |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
76 | |||
77 | built_object = explicit_container.resolve(Optional[SomethingToBuild]) # type: ignore |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
78 | assert isinstance(built_object, SomethingToBuild) |
||
79 | |||
80 | |||
81 | def test_the_clone_of_an_explicit_container_is_also_an_explicit_container( |
||
82 | explicit_container: Container, |
||
83 | ): |
||
84 | clone = explicit_container.clone() |
||
85 | assert isinstance(clone, ExplicitContainer) |
||
86 |