Total Complexity | 9 |
Total Lines | 30 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import pytest |
||
2 | |||
3 | from lagom import Container |
||
4 | from lagom.exceptions import InvalidDependencyDefinition |
||
5 | |||
6 | |||
7 | class MyBasicDep: |
||
8 | basic_value = 5 |
||
9 | |||
10 | |||
11 | class ComplexDep: |
||
12 | def __init__(self, some_dep): |
||
13 | self.value = some_dep.basic_value |
||
14 | |||
15 | |||
16 | def test_lambda_arity_zero_works(container: Container): |
||
17 | container.define(MyBasicDep, lambda: MyBasicDep()) |
||
18 | assert type(container[MyBasicDep]) == MyBasicDep |
||
19 | |||
20 | |||
21 | def test_lambda_arity_one_is_passed_the_container(container: Container): |
||
22 | container.define(MyBasicDep, lambda: MyBasicDep()) |
||
23 | container.define(ComplexDep, lambda c: ComplexDep(c[MyBasicDep])) |
||
24 | assert container[ComplexDep].value == 5 |
||
25 | |||
26 | |||
27 | def test_lambda_arity_two_results_in_an_error(container: Container): |
||
28 | with pytest.raises(InvalidDependencyDefinition): |
||
29 | container.define(MyBasicDep, lambda _x, _y: MyBasicDep) # type: ignore |
||
30 |