tests.test_async_definitions_can_be_used   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
A MyComplexDep.asyc_loader() 0 3 1

5 Functions

Rating   Name   Duplication   Size   Complexity  
A test_simple_async_component_def() 0 7 1
A test_alternative_way_of_defining_an_async_dep() 0 5 1
A test_async_singleton_do_not_raise_runtime_error() 0 6 1
A test_defining_an_async_dep_provides_a_helpful_error_if_you_forget_awaitable() 0 8 2
A test_a_sync_and_async_version_can_be_defined() 0 9 2
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