Passed
Push — master ( 05e6ef...1900c1 )
by Steve
03:19
created

test_context_partials()   A

Complexity

Conditions 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nop 1
dl 0
loc 24
rs 9.5
c 0
b 0
f 0
1
from typing import Iterator
2
3
import pytest
4
5
from lagom import (
6
    Singleton,
7
    Container,
8
    ContextContainer,
9
    magic_bind_to_container,
10
    ExplicitContainer,
11
    bind_to_container,
12
    injectable,
13
    context_dependency_definition,
14
)
15
from .core_domain import SomeOtherThingAsAsingleton, SomeService, AThingIMightNeed
16
17
18
@pytest.mark.benchmarking
19
def test_magic(benchmark):
20
    container = Container()
21
    container[SomeOtherThingAsAsingleton] = Singleton(SomeOtherThingAsAsingleton)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeOtherThingAsAsingleton does not seem to be defined.
Loading history...
22
23
    @magic_bind_to_container(container, shared=[SomeService])
24
    def do_work(thing: AThingIMightNeed):
25
        thing.do_it()
26
27
    def do_pretend_work():
28
        for _ in range(10):
29
            do_work()
30
        return True
31
32
    assert benchmark(do_pretend_work)
33
34
35
@pytest.mark.benchmarking
36
def test_plain(benchmark):
37
    container = Container()
38
    container[SomeOtherThingAsAsingleton] = Singleton(SomeOtherThingAsAsingleton)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeOtherThingAsAsingleton does not seem to be defined.
Loading history...
39
40
    @bind_to_container(container, shared=[SomeService])
41
    def do_work(thing: AThingIMightNeed = injectable):
42
        thing.do_it()
43
44
    def do_pretend_work():
45
        for _ in range(10):
46
            do_work()
47
        return True
48
49
    assert benchmark(do_pretend_work)
50
51
52
@pytest.mark.benchmarking
53
def test_optimised(benchmark):
54
    container = ExplicitContainer()
55
    container[SomeOtherThingAsAsingleton] = SomeOtherThingAsAsingleton()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeOtherThingAsAsingleton does not seem to be defined.
Loading history...
56
    container[SomeService] = lambda c: SomeService(c[SomeOtherThingAsAsingleton])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeService does not seem to be defined.
Loading history...
57
    container[AThingIMightNeed] = lambda c: AThingIMightNeed(c[SomeService])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable AThingIMightNeed does not seem to be defined.
Loading history...
58
59
    @bind_to_container(container, shared=[SomeService])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeService does not seem to be defined.
Loading history...
60
    def do_work(thing: AThingIMightNeed = injectable):
61
        thing.do_it()
62
63
    def do_pretend_work():
64
        for _ in range(10):
65
            do_work()
66
        return True
67
68
    assert benchmark(do_pretend_work)
69
70
71
@pytest.mark.benchmarking
72
def test_context_partials(benchmark):
73
    container = Container()
74
    container[SomeOtherThingAsAsingleton] = Singleton(SomeOtherThingAsAsingleton)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeOtherThingAsAsingleton does not seem to be defined.
Loading history...
75
76
    @context_dependency_definition(container)
77
    def _load_dep_then_clean(c) -> Iterator[SomeService]:
78
        try:
79
            yield SomeService(c[SomeOtherThingAsAsingleton])
80
        finally:
81
            pass
82
83
    context_container = ContextContainer(container, context_types=[SomeService])
84
85
    @bind_to_container(context_container, shared=[SomeService])
86
    def do_work(thing: AThingIMightNeed = injectable):
87
        thing.do_it()
88
89
    def do_pretend_work():
90
        for _ in range(10):
91
            do_work()
92
        return True
93
94
    assert benchmark(do_pretend_work)
95