-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_di.py
85 lines (55 loc) · 1.87 KB
/
test_di.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import arc
import hikari
import pytest
import tanjun
import miru
bot = hikari.GatewayBot("...", banner=None)
client = miru.Client(bot)
class Dummy:
def __init__(self, val: int):
self.val = val
client.set_type_dependency(Dummy, Dummy(0))
@client.inject_dependencies
def dummy_callback(dummy: Dummy = miru.inject()) -> int:
dummy.val += 1
return dummy.val
@client.inject_dependencies
async def async_dummy_callback(dummy: Dummy = miru.inject()) -> int:
dummy.val += 1
return dummy.val
@client.inject_dependencies
def injects_client(client: miru.Client = miru.inject()) -> miru.Client:
return client
injector = miru.Injector()
injector.set_type_dependency(Dummy, Dummy(0))
manually_injected_client = miru.Client(bot, injector=injector)
@manually_injected_client.inject_dependencies
def manual_dummy_callback(dummy: Dummy = miru.inject()) -> int:
dummy.val += 1
return dummy.val
tanjun_c = tanjun.Client.from_gateway_bot(bot)
tanjun_c.set_type_dependency(Dummy, Dummy(0))
tanjun_miru_c = miru.Client.from_tanjun(tanjun_c)
@tanjun_miru_c.inject_dependencies
def tanjun_miru_c_callback(dummy: Dummy = miru.inject()) -> int:
dummy.val += 1
return dummy.val
arc_c = arc.GatewayClient(bot)
arc_c.set_type_dependency(Dummy, Dummy(0))
arc_miru_c = miru.Client.from_arc(arc_c)
@arc_miru_c.inject_dependencies
def arc_miru_c_callback(dummy: Dummy = miru.inject()) -> int:
dummy.val += 1
return dummy.val
@pytest.mark.asyncio
async def test_inject() -> None:
assert dummy_callback() == 1
assert await async_dummy_callback() == 2
assert dummy_callback() == 3
assert injects_client() is client
assert manual_dummy_callback() == 1
assert manual_dummy_callback() == 2
assert tanjun_miru_c_callback() == 1
assert tanjun_miru_c_callback() == 2
assert arc_miru_c_callback() == 1
assert arc_miru_c_callback() == 2