-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_executor.py
92 lines (70 loc) 路 2.92 KB
/
test_executor.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
86
87
88
89
90
91
92
import asyncio
from datetime import datetime
from typing import List, NoReturn, Optional
import async_timeout
import pytest
from glQiwiApi import QiwiWallet
from glQiwiApi.core.event_fetching.dispatcher import QiwiDispatcher
from glQiwiApi.core.event_fetching.executor import (
HandlerContext,
ExecutorEvent,
start_non_blocking_qiwi_api_polling,
)
from glQiwiApi.qiwi.clients.wallet.types import History, Source, Transaction, TransactionType
from glQiwiApi.yoo_money.methods.operation_history import MAX_HISTORY_LIMIT
class TestExecutorEvent:
async def test_fire(self):
context = HandlerContext({'api_key': 'fake_api_key'})
async def init_event(ctx: HandlerContext) -> NoReturn:
assert ctx == context
raise RuntimeError()
event = ExecutorEvent(context, init_handlers=[init_event])
event += init_event
event -= init_event
with pytest.raises(RuntimeError):
await event.fire()
async def test_fire_sync_handlers(self):
context = HandlerContext({'api_key': 'fake_api_key'})
event = ExecutorEvent(context)
def on_event(ctx: HandlerContext) -> NoReturn:
assert ctx == context
raise RuntimeError()
event += on_event
with pytest.raises(RuntimeError):
await event.fire()
class WalletStub(QiwiWallet):
def __init__(self, fake_transaction: Transaction, api_access_token: str = ''):
super().__init__(api_access_token)
self._fake_transaction = fake_transaction
async def history(
self,
rows: int = MAX_HISTORY_LIMIT,
transaction_type: TransactionType = TransactionType.ALL,
sources: Optional[List[Source]] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
next_txn_date: Optional[datetime] = None,
next_txn_id: Optional[int] = None,
) -> History:
return History(data=[self._fake_transaction])
async def test_start_non_blocking_qiwi_api_polling(transaction: Transaction) -> None:
c = HandlerContext({'api_key': 'my_api_key'})
wallet = WalletStub(transaction)
dp = QiwiDispatcher()
handled_transaction_event = asyncio.Event()
handle_on_startup = asyncio.Event()
@dp.transaction_handler()
async def handle_transaction(txn: Transaction, ctx: HandlerContext):
assert ctx['api_key'] == 'my_api_key'
assert ctx.wallet == wallet
handled_transaction_event.set()
async def on_startup(ctx: HandlerContext):
assert ctx['api_key'] == 'my_api_key'
assert ctx.wallet == wallet
handle_on_startup.set()
async with async_timeout.timeout(5):
await start_non_blocking_qiwi_api_polling(wallet, dp, context=c, on_startup=on_startup)
await handle_on_startup.wait()
await handled_transaction_event.wait()
assert handled_transaction_event.is_set()
assert handle_on_startup.is_set()