|
1
|
|
|
import inspect |
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
from lagom import Container, magic_bind_to_container, injectable |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class Something: |
|
9
|
|
|
pass |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def test_partial_application_async_functions_pass_iscoroutinefunction( |
|
13
|
|
|
container: Container, |
|
14
|
|
|
): |
|
15
|
|
|
@magic_bind_to_container(container) |
|
16
|
|
|
async def example_async_function(message: str) -> str: |
|
17
|
|
|
return message |
|
18
|
|
|
|
|
19
|
|
|
assert inspect.iscoroutinefunction(example_async_function) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def test_partial_application_async_functions_with_shared_pass_iscoroutinefunction( |
|
23
|
|
|
container: Container, |
|
24
|
|
|
): |
|
25
|
|
|
@magic_bind_to_container(container, shared=[Something]) |
|
26
|
|
|
async def example_async_function(message: str) -> str: |
|
27
|
|
|
return message |
|
28
|
|
|
|
|
29
|
|
|
assert inspect.iscoroutinefunction(example_async_function) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
@pytest.mark.asyncio |
|
33
|
|
|
async def test_calling_async_partials_works_as_expected(container: Container): |
|
34
|
|
|
@magic_bind_to_container(container) |
|
35
|
|
|
async def example_async_function(message: str) -> str: |
|
36
|
|
|
return message |
|
37
|
|
|
|
|
38
|
|
|
assert await example_async_function("test") == "test" |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
@pytest.mark.asyncio |
|
42
|
|
|
async def test_calling_async_partials_on_a_object_works_as_expected( |
|
43
|
|
|
container: Container, |
|
44
|
|
|
): |
|
45
|
|
|
class Thingy: |
|
46
|
|
|
async def example_async_function( |
|
47
|
|
|
self, message: str, something: Something = injectable |
|
48
|
|
|
) -> str: |
|
49
|
|
|
assert isinstance(something, Something) |
|
50
|
|
|
return message |
|
51
|
|
|
|
|
52
|
|
|
thing = Thingy() |
|
53
|
|
|
bound_thing = container.partial(thing.example_async_function) |
|
54
|
|
|
assert await bound_thing("test") == "test" |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
@pytest.mark.asyncio |
|
58
|
|
|
async def test_calling_async_partials_works_as_expected_with_shared_too( |
|
59
|
|
|
container: Container, |
|
60
|
|
|
): |
|
61
|
|
|
@magic_bind_to_container(container, shared=[Something]) |
|
62
|
|
|
async def example_async_function(message: str) -> str: |
|
63
|
|
|
return message |
|
64
|
|
|
|
|
65
|
|
|
assert await example_async_function("test") == "test" |
|
66
|
|
|
|