tests.test_magic_partial_functions_scoped_singletons   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 11

7 Functions

Rating   Name   Duplication   Size   Complexity  
A test_invocation_level_singletons_are_not_shared_across_calls() 0 11 1
A test_by_default_all_resources_are_reconstructed() 0 7 1
A test_that_shared_types_are_lazy_loaded() 0 9 1
A test_invocation_level_singletons_can_be_defined() 0 9 1
A test_invocation_level_singletons_dont_affect_the_base_container() 0 14 1
A test_partial_application_returns_something_that_is_considered_a_function() 0 8 1
A test_invocation_level_singletons_respect_container_singletons() 0 14 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SomeCache.value() 0 2 1
A SomeCache.__init__() 0 3 1
A MyDepTwo.__init__() 0 2 1
A MyDepOne.__init__() 0 2 1
1
import inspect
2
from random import random
3
from typing import Any, ClassVar
4
5
from lagom import Container, magic_bind_to_container, Singleton
6
7
8
class SomeCache:
9
    loaded: ClassVar[bool] = False
10
11
    def __init__(self):
12
        SomeCache.loaded = True
13
        self._init_random = random()
14
15
    def value(self):
16
        return self._init_random
17
18
19
class MyDepOne:
20
    value: Any
21
22
    def __init__(self, cache: SomeCache):
23
        self.value = cache.value()
24
25
26
class MyDepTwo:
27
    other_value: Any
28
29
    def __init__(self, cache: SomeCache):
30
        self.other_value = cache.value()
31
32
33
def test_by_default_all_resources_are_reconstructed(container: Container):
34
    @magic_bind_to_container(container)
35
    def example_function(dep_one: MyDepOne, dep_two: MyDepTwo):
36
        return {"a": dep_one.value, "b": dep_two.other_value}
37
38
    result = example_function()
39
    assert result["a"] != result["b"]
40
41
42
def test_invocation_level_singletons_can_be_defined(container: Container):
43
    @magic_bind_to_container(container, shared=[SomeCache])
44
    def example_function_with_invocation_level_sharing(
45
        dep_one: MyDepOne, dep_two: MyDepTwo
46
    ):
47
        return {"a": dep_one.value, "b": dep_two.other_value}
48
49
    result = example_function_with_invocation_level_sharing()
50
    assert result["a"] == result["b"]
51
52
53
def test_invocation_level_singletons_dont_affect_the_base_container(
54
    container: Container,
55
):
56
    @magic_bind_to_container(container, shared=[SomeCache])
57
    def example_function_with_invocation_level_sharing(
58
        cache_one: SomeCache, cache_two: SomeCache
59
    ):
60
        # This is what we expect with invocation level singleton
61
        assert cache_one == cache_two
62
63
    example_function_with_invocation_level_sharing()
64
65
    # Check that each invocation gets a new one - this is the normal behaviour
66
    assert container[SomeCache] != container[SomeCache]
67
68
69
def test_invocation_level_singletons_are_not_shared_across_calls(container: Container):
70
    @magic_bind_to_container(container, shared=[SomeCache])
71
    def example_function_with_invocation_level_sharing(
72
        dep_one: MyDepOne, dep_two: MyDepTwo
73
    ):
74
        return {"a": dep_one.value, "b": dep_two.other_value}
75
76
    call_one = example_function_with_invocation_level_sharing()
77
    call_two = example_function_with_invocation_level_sharing()
78
79
    assert call_one["a"] != call_two["a"]
80
81
82
def test_that_shared_types_are_lazy_loaded(container: Container):
83
    SomeCache.loaded = False
84
85
    @magic_bind_to_container(container, shared=[SomeCache])
86
    def example_function_that_defines_but_doesnt_use_sharing():
87
        return "ok"
88
89
    example_function_that_defines_but_doesnt_use_sharing()
90
    assert not SomeCache.loaded
91
92
93
def test_partial_application_returns_something_that_is_considered_a_function(
94
    container: Container,
95
):
96
    @magic_bind_to_container(container, shared=[SomeCache])
97
    def example_function_with_shared():
98
        return "ok"
99
100
    inspect.isfunction(example_function_with_shared)
101
102
103
def test_invocation_level_singletons_respect_container_singletons(container: Container):
104
    container[SomeCache] = Singleton(SomeCache)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeCache does not seem to be defined.
Loading history...
105
106
    @magic_bind_to_container(container, shared=[SomeCache])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SomeCache does not seem to be defined.
Loading history...
107
    def example_function_with_invocation_level_sharing(
108
        dep_one: MyDepOne, dep_two: MyDepTwo
109
    ):
110
        return {"a": dep_one.value, "b": dep_two.other_value}
111
112
    result_one = example_function_with_invocation_level_sharing()
113
    result_two = example_function_with_invocation_level_sharing()
114
115
    assert result_one["a"] == result_one["b"]
116
    assert result_one["a"] == result_two["a"]
117