MyComplexDep.asyc_loader()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
from dataclasses import dataclass
2
from typing import Awaitable
3
4
import pytest
5
6
from lagom import Container, dependency_definition, Singleton
7
from lagom.exceptions import TypeOnlyAvailableAsAwaitable
8
9
10
@dataclass
11
class MyComplexDep:
12
    some_number: int
13
14
    @classmethod
15
    async def asyc_loader(cls):
16
        return cls(10)
17
18
19
@pytest.mark.asyncio
20
async def test_simple_async_component_def(container: Container):
21
    @dependency_definition(container)
22
    async def my_constructor() -> MyComplexDep:
23
        return MyComplexDep(some_number=5)
24
25
    assert (await container[Awaitable[MyComplexDep]]) == MyComplexDep(some_number=5)  # type: ignore
26
27
28
@pytest.mark.asyncio
29
async def test_alternative_way_of_defining_an_async_dep(container: Container):
30
    container[Awaitable[MyComplexDep]] = MyComplexDep.asyc_loader  # type: ignore
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable MyComplexDep does not seem to be defined.
Loading history...
31
32
    assert (await container[Awaitable[MyComplexDep]]) == MyComplexDep(some_number=10)  # type: ignore
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Awaitable does not seem to be defined.
Loading history...
33
34
35
@pytest.mark.asyncio
36
async def test_async_singleton_do_not_raise_runtime_error(container: Container):
37
    container[Awaitable[MyComplexDep]] = Singleton(MyComplexDep.asyc_loader)  # type: ignore[type-abstract]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable MyComplexDep does not seem to be defined.
Loading history...
38
39
    assert (await container[Awaitable[MyComplexDep]]) == MyComplexDep(some_number=10)  # type: ignore[type-abstract]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Awaitable does not seem to be defined.
Loading history...
40
    assert (await container[Awaitable[MyComplexDep]]) == MyComplexDep(some_number=10)  # type: ignore[type-abstract]
41
42
43
@pytest.mark.asyncio
44
async def test_defining_an_async_dep_provides_a_helpful_error_if_you_forget_awaitable(
45
    container: Container,
46
):
47
    container[Awaitable[MyComplexDep]] = MyComplexDep.asyc_loader  # type: ignore
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable MyComplexDep does not seem to be defined.
Loading history...
48
49
    with pytest.raises(TypeOnlyAvailableAsAwaitable):
50
        assert container[MyComplexDep]
51
52
53
@pytest.mark.asyncio
54
async def test_a_sync_and_async_version_can_be_defined(
55
    container: Container,
56
):
57
    container[MyComplexDep] = lambda: MyComplexDep(5)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable MyComplexDep does not seem to be defined.
Loading history...
58
    container[Awaitable[MyComplexDep]] = MyComplexDep.asyc_loader  # type: ignore
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable MyComplexDep does not seem to be defined.
Loading history...
59
60
    assert container[MyComplexDep]
61
    assert container[Awaitable[MyComplexDep]]  # type: ignore
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Awaitable does not seem to be defined.
Loading history...
62