| Total Complexity | 8 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from abc import ABC |
||
| 2 | from typing import List |
||
| 3 | |||
| 4 | import pytest |
||
| 5 | |||
| 6 | from lagom import Construction, Container, Alias |
||
| 7 | |||
| 8 | |||
| 9 | class MySimpleAbc(ABC): |
||
| 10 | stuff: str = "empty" |
||
| 11 | |||
| 12 | def bloop(self): |
||
| 13 | return "beep" |
||
| 14 | |||
| 15 | |||
| 16 | class MySimpleDep(MySimpleAbc): |
||
| 17 | stuff: str |
||
| 18 | |||
| 19 | def __init__(self, stuff): |
||
| 20 | self.stuff = stuff |
||
| 21 | |||
| 22 | |||
| 23 | class MyMoreComplicatedDep: |
||
| 24 | complicated_stuff: str |
||
| 25 | |||
| 26 | def __init__(self, dep: MySimpleAbc): |
||
| 27 | self.stuff = dep.stuff |
||
| 28 | |||
| 29 | |||
| 30 | class AnotherAbc(ABC): |
||
| 31 | stuff: str = "empty" |
||
| 32 | |||
| 33 | |||
| 34 | class AnotherConcrete(AnotherAbc): |
||
| 35 | stuff = "full" |
||
| 36 | |||
| 37 | |||
| 38 | @pytest.fixture |
||
| 39 | def container_with_abc(container: Container): |
||
| 40 | container.define(MySimpleAbc, Construction(lambda: MySimpleDep("hooray"))) |
||
| 41 | return container |
||
| 42 | |||
| 43 | |||
| 44 | def test_registered_concrete_class_is_loaded(container_with_abc: Container): |
||
| 45 | resolved = container_with_abc.resolve(MySimpleAbc) |
||
| 46 | assert resolved.stuff == "hooray" |
||
| 47 | |||
| 48 | |||
| 49 | def test_registered_concrete_class_is_used_for_other_objects( |
||
| 50 | container_with_abc: Container |
||
| 51 | ): |
||
| 52 | resolved = container_with_abc.resolve(MyMoreComplicatedDep) |
||
| 53 | assert resolved.stuff == "hooray" |
||
| 54 | |||
| 55 | |||
| 56 | def test_alias_can_be_defined(container_with_abc: Container): |
||
| 57 | container_with_abc.define(AnotherAbc, Alias(AnotherConcrete)) |
||
| 58 | resolved = container_with_abc.resolve(AnotherAbc) |
||
| 59 | assert resolved.stuff == "full" |
||
| 60 |