Passed
Push — master ( f4eaa5...bb43d9 )
by Mitchell
58s
created

run()   A

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
try:
2
    from asyncio import run
3
except ImportError:
4
    import asyncio
5
6
    def run(main):
7
        loop = asyncio.get_event_loop()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable asyncio does not seem to be defined.
Loading history...
8
        return loop.run_until_complete(main)
9
10
from injectify.api import inject
11
from injectify.injectors import (
12
    HeadInjector,
13
    TailInjector,
14
    ReturnInjector,
15
    FieldInjector,
16
    NestedInjector,
17
)
18
19
20
def test_head_injector_correctly_injects_async_function():
21
    async def target(x):
22
        a = 10
23
        if x > a:
24
            a = x
25
        return a
26
27
    @inject(target=target, injector=HeadInjector())
28
    def handler():
29
        x = 11
30
31
    assert run(target(0)) == 11
32
    assert run(target(10)) == 11
33
    assert run(target(101)) == 11
34
35
36
def test_tail_injector_correctly_injects_async_function():
37
    async def target(x):
38
        if x > 100:
39
            return x
40
41
    @inject(target=target, injector=TailInjector())
42
    def handler():
43
        return -1
44
45
    assert run(target(13)) == -1
46
    assert run(target(101)) == 101
47
48
49
def test_return_injector_correctly_injects_async_function_all_returns():
50
    async def target(x):
51
        if x > 100:
52
            y = x * 2
53
            return y
54
        else:
55
            y = x + 2
56
            return y
57
58
    @inject(target=target, injector=ReturnInjector())
59
    def handler():
60
        return '{} :)'.format(y)
61
62
    assert run(target(13)) == '15 :)'
63
    assert run(target(101)) == '202 :)'
64
65
66 View Code Duplication
def test_return_injector_correctly_injects_async_function_ordinal_returns():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
    async def target(x):
68
        if x > 100:
69
            y = x * 2
70
            return y
71
        else:
72
            y = x + 2
73
            return y
74
75
    @inject(target=target, injector=ReturnInjector(ordinal=1))
76
    def handler():
77
        return '{} :)'.format(y)
78
79
    assert run(target(13)) == '15 :)'
80
    assert run(target(101)) == 202
81
82
83
def test_field_injector_correctly_injects_async_function_before_all_fields():
84
    async def target(x):
85
        if x > 100:
86
            y = x * 2
87
        else:
88
            y = x + 2
89
        return y
90
91
    @inject(target=target, injector=FieldInjector('y', insert='before'))
92
    def handler():
93
        x += 1
94
95
    assert run(target(13)) == 16
96
    assert run(target(101)) == 204
97
98
99
def test_field_injector_correctly_injects_async_function_after_all_fields():
100
    async def target(x):
101
        if x > 100:
102
            y = x * 2
103
        else:
104
            y = x + 2
105
        return y
106
107
    @inject(target=target, injector=FieldInjector('y', insert='after'))
108
    def handler():
109
        y -= 1
110
111
    assert run(target(13)) == 14
112
    assert run(target(101)) == 201
113
114
115 View Code Duplication
def test_field_injector_correctly_injects_async_function_before_ordinal_field():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
116
    async def target(x):
117
        if x > 100:
118
            y = x * 2
119
        else:
120
            y = x + 2
121
        return y
122
123
    @inject(
124
        target=target, injector=FieldInjector('y', ordinal=1, insert='before'),
125
    )
126
    def handler():
127
        x += 1
128
129
    assert run(target(13)) == 16
130
    assert run(target(101)) == 202
131
132
133
def test_field_injector_correctly_injects_async_function_after_ordinal_field():
134
    async def target(x):
135
        if x > 100:
136
            y = x * 2
137
        else:
138
            y = x + 2
139
        return y
140
141
    @inject(target=target, injector=FieldInjector('y', ordinal=0, insert='after'))
142
    def handler():
143
        y -= 1
144
145
    assert run(target(13)) == 15
146
    assert run(target(101)) == 201
147
148
149 View Code Duplication
def test_nested_injector_correctly_injects_async_function_sync_nested():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
150
    async def target(x):
151
        def nested(y):
152
            if y > 100:
153
                return y
154
155
        if x < 200:
156
            return nested(x)
157
158
    @inject(target=target, injector=NestedInjector('nested', TailInjector()))
159
    def handler():
160
        return -1
161
162
    assert run(target(13)) == -1
163
    assert run(target(101)) == 101
164
    assert run(target(200)) is None
165
166
167 View Code Duplication
def test_nested_injector_correctly_injects_async_function_async_nested():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
168
    async def target(x):
169
        async def nested(y):
170
            if y > 100:
171
                return y
172
173
        if x < 200:
174
            return await nested(x)
175
176
    @inject(target=target, injector=NestedInjector('nested', TailInjector()))
177
    def handler():
178
        return -1
179
180
    assert run(target(13)) == -1
181
    assert run(target(101)) == 101
182
    assert run(target(200)) is None
183
184
185 View Code Duplication
def test_nested_injector_correctly_injects_nested_sync_function_async_nested():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
186
    async def target(x):
187
        async def nested(y):
188
            if y > 100:
189
                return y
190
191
        if x < 200:
192
            return await nested(x)
193
194
    @inject(target=target, injector=NestedInjector('nested', TailInjector()))
195
    def handler():
196
        return -1
197
198
    assert run(target(13)) == -1
199
    assert run(target(101)) == 101
200
    assert run(target(200)) is None
201