Skip to content

Commit

Permalink
change tests logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GLEF1X committed Jul 14, 2021
1 parent ae99ae6 commit ddeae3d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 76 deletions.
2 changes: 1 addition & 1 deletion glQiwiApi/utils/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ async def _pool_process(self, get_updates_from: Optional[datetime]) -> None:
first_payment: Transaction = history_from_first_to_last[0]
self.last_handled_txn_id = first_payment.transaction_id - 1

await self._parse_history_and_process_events(history=history_from_first_to_last)
await self._parse_history_and_process_events(history_from_first_to_last)

async def _start_polling(self, **kwargs: Any) -> None:
"""
Expand Down
106 changes: 106 additions & 0 deletions tests/test_dispatcher/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import asyncio
import functools
from typing import Optional, Tuple

import pytest
from aiohttp import web
from aiohttp.test_utils import TestClient
from aiohttp.test_utils import TestServer, BaseTestServer

from glQiwiApi.core.web_hooks import server
from glQiwiApi.core.web_hooks.dispatcher import Dispatcher
from glQiwiApi.core.web_hooks.server import _check_ip # NOQA
from glQiwiApi.core.web_hooks.server import _check_ip # NOQA
from glQiwiApi.core.web_hooks.server import _check_ip # NOQA
from glQiwiApi.types import Notification, WebHook
from tests.types.dataset import NOTIFICATION_RAW_DATA, WEBHOOK_RAW_DATA

pytestmark = pytest.mark.asyncio


@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest.fixture(name="test_aiohttp")
async def aiohttp_client():
"""Factory to create a TestClient instance.
aiohttp_client(app, **kwargs)
aiohttp_client(server, **kwargs)
aiohttp_client(raw_server, **kwargs)
"""
clients = []

async def go(__param, *, server_kwargs=None, **kwargs): # type: ignore
if isinstance(__param, web.Application):
server_kwargs = server_kwargs or {}
server = TestServer(__param, **server_kwargs)
client = TestClient(server, **kwargs)
elif isinstance(__param, BaseTestServer):
client = TestClient(__param, **kwargs)
else:
raise ValueError("Unknown argument type: %r" % type(__param))

await client.start_server()
clients.append(client)
return client

yield go

async def finalize(): # type: ignore
while clients:
await clients.pop().close()

await finalize()


async def my_bill_handler(update: Notification, event: Optional[asyncio.Event] = None):
if not isinstance(event, asyncio.Event):
raise TypeError()
assert update == Notification.parse_raw(NOTIFICATION_RAW_DATA)
event.set()


async def my_transaction_handler(
update: WebHook, event: Optional[asyncio.Event] = None
):
if not isinstance(event, asyncio.Event):
raise TypeError()
assert update == WebHook.parse_raw(WEBHOOK_RAW_DATA)
event.set()


@pytest.fixture(name="bill_event", scope="module")
def bill_event_fixture():
yield asyncio.Event()


@pytest.fixture(name="txn_event", scope="module")
def transaction_event_fixture():
yield asyncio.Event()


@pytest.fixture(name="dp", scope="module")
async def dp_fixture():
dp = Dispatcher()
yield dp


@pytest.fixture(name="add_handlers", scope="module")
def add_handlers(dp: Dispatcher, txn_event: asyncio.Event, bill_event: asyncio.Event):
dp.register_bill_handler(functools.partial(my_bill_handler, event=bill_event))
dp.register_transaction_handler(functools.partial(my_transaction_handler, event=txn_event))
return txn_event, bill_event


@pytest.fixture(name="app", scope="module")
async def app_fixture(dp: Dispatcher, add_handlers: Tuple[asyncio.Event, asyncio.Event]):
txn_event, bill_event = add_handlers
app = web.Application()
app["bill_event"] = bill_event
app["txn_event"] = txn_event
return server.setup(dp, app=app)
3 changes: 2 additions & 1 deletion tests/test_dispatcher/test_filters/test_builtin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from glQiwiApi.core.builtin import TransactionFilter, BillFilter

from glQiwiApi import types
from glQiwiApi.core.builtin import TransactionFilter, BillFilter
from tests.types.dataset import NOTIFICATION_RAW_DATA, WEBHOOK_RAW_DATA, TXN_RAW_DATA


Expand Down
80 changes: 6 additions & 74 deletions tests/test_dispatcher/test_webhooks.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,27 @@
import asyncio
import functools
from typing import Awaitable, Callable, Optional
from typing import Awaitable, Callable

import pytest
from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer, BaseTestServer
from aiohttp.test_utils import TestClient
from pytest_mock import MockerFixture

from glQiwiApi.core.web_hooks import server
from glQiwiApi.core.web_hooks.config import (
DEFAULT_QIWI_BILLS_WEBHOOK_PATH,
DEFAULT_QIWI_WEBHOOK_PATH,
)
from glQiwiApi.core.web_hooks.dispatcher import Dispatcher
from glQiwiApi.core.web_hooks.server import _check_ip # NOQA
from glQiwiApi.types import Notification, WebHook
from tests.types.dataset import NOTIFICATION_RAW_DATA, WEBHOOK_RAW_DATA

pytestmark = pytest.mark.asyncio


async def my_bill_handler(update: Notification, event: Optional[asyncio.Event] = None):
if not isinstance(event, asyncio.Event):
raise TypeError()
assert update == Notification.parse_raw(NOTIFICATION_RAW_DATA)
event.set()


async def my_transaction_handler(
update: WebHook, event: Optional[asyncio.Event] = None
):
if not isinstance(event, asyncio.Event):
raise TypeError()
assert update == WebHook.parse_raw(WEBHOOK_RAW_DATA)
event.set()


@pytest.fixture()
async def aiohttp_client():
"""Factory to create a TestClient instance.
aiohttp_client(app, **kwargs)
aiohttp_client(server, **kwargs)
aiohttp_client(raw_server, **kwargs)
"""
clients = []

async def go(__param, *, server_kwargs=None, **kwargs): # type: ignore
if isinstance(__param, web.Application):
server_kwargs = server_kwargs or {}
server = TestServer(__param, **server_kwargs)
client = TestClient(server, **kwargs)
elif isinstance(__param, BaseTestServer):
client = TestClient(__param, **kwargs)
else:
raise ValueError("Unknown argument type: %r" % type(__param))

await client.start_server()
clients.append(client)
return client

yield go

async def finalize(): # type: ignore
while clients:
await clients.pop().close()

await finalize()


@pytest.fixture(name="app")
async def app_fixture():
dp = Dispatcher()
bill_event = asyncio.Event()
txn_event = asyncio.Event()
dp.register_bill_handler(functools.partial(my_bill_handler, event=bill_event))
dp.register_transaction_handler(
functools.partial(my_transaction_handler, event=txn_event)
)
app = web.Application()
app["bill_event"] = bill_event
app["txn_event"] = txn_event
return server.setup(dp, app=app)


async def test_bill_webhooks(
app: web.Application,
aiohttp_client: Callable[..., Awaitable[TestClient]],
mocker: MockerFixture,
app: web.Application,
test_aiohttp: Callable[..., Awaitable[TestClient]],
mocker: MockerFixture,
):
client = await aiohttp_client(app)
client = await test_aiohttp(app)
bill_event: asyncio.Event = app["bill_event"]
txn_event: asyncio.Event = app["txn_event"]
# skip ip validation
Expand Down

0 comments on commit ddeae3d

Please sign in to comment.