From 4eae4e57a70b9f225b9482d5976b619bc4d0f6c7 Mon Sep 17 00:00:00 2001 From: Alex Proskuriakov Date: Sun, 10 Dec 2023 23:03:58 +0300 Subject: [PATCH] Improved celery-task test --- app/client/utils.py | 2 +- tests/conftest.py | 2 ++ tests/integration_tests/test_client.py | 21 ++++----------------- tests/test_fixtures.py | 1 + tests/unit_tests/test_task.py | 5 +++++ tests/utils.py | 13 +++++++++++++ 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/client/utils.py b/app/client/utils.py index 1d34a11..9d6be68 100644 --- a/app/client/utils.py +++ b/app/client/utils.py @@ -15,7 +15,7 @@ async def wrapper(*args, **kwargs): data = [item async for item in func(*args, **kwargs)] [logger.info(item) for item in data] logger.info('Successfully loaded') - return data + return data[0] return wrapper diff --git a/tests/conftest.py b/tests/conftest.py index 9b50925..c62781b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +import asyncio from typing import Any, AsyncGenerator, Generator import pytest @@ -7,6 +8,7 @@ from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker, create_async_engine) +from app.client.deribit import CURRENCIES, get_data_from_tickers # noqa from app.core import settings # noqa from app.core import Base, current_user, get_aioredis, get_async_session from app.core.user import create_admin, create_user # noqa diff --git a/tests/integration_tests/test_client.py b/tests/integration_tests/test_client.py index 20e60bc..269ba67 100644 --- a/tests/integration_tests/test_client.py +++ b/tests/integration_tests/test_client.py @@ -1,23 +1,10 @@ -import asyncio -from datetime import datetime as dt -from datetime import timedelta - import pytest -from app.client.deribit import CURRENCIES, get_data_from_tickers +from tests.conftest import CURRENCIES, get_data_from_tickers +from tests.utils import check_currency @pytest.mark.asyncio async def test_get_data_from_tickers(event_loop): - assert event_loop is asyncio.get_running_loop() - # assert asyncio.iscoroutine(get_data_from_tickers) - seconds = 10 - data = await get_data_from_tickers() - for name, price, timestamp in data[0]: - assert name in CURRENCIES - assert isinstance(price, float) - assert isinstance(timestamp, int) - res = ((dt.now() - timedelta(seconds=seconds)).timestamp() < - timestamp < - (dt.now() + timedelta(seconds=seconds)).timestamp()) - assert res + for currency in await get_data_from_tickers(): + check_currency(*currency, CURRENCIES) diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index a48f113..bfa31d9 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -4,6 +4,7 @@ def test_event_loop_fixture(event_loop) -> None: + # assert event_loop is asyncio.get_running_loop() event_loop.run_until_complete(asyncio.sleep(0)) diff --git a/tests/unit_tests/test_task.py b/tests/unit_tests/test_task.py index d0dcf57..7d8df3d 100644 --- a/tests/unit_tests/test_task.py +++ b/tests/unit_tests/test_task.py @@ -1,8 +1,13 @@ from celery.local import PromiseProxy from app.celery_tasks.tasks import synchronize +from tests.conftest import CURRENCIES +from tests.utils import check_currency def test_task(): assert isinstance(synchronize, PromiseProxy), type(synchronize) assert synchronize.name == 'app.celery_tasks.tasks.synchronize' + currencies = synchronize() + for currency in currencies: + check_currency(*currency, CURRENCIES) diff --git a/tests/utils.py b/tests/utils.py index 171ed2d..b245165 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,3 +1,5 @@ +from datetime import datetime as dt +from datetime import timedelta from typing import Any from deepdiff import DeepDiff @@ -42,3 +44,14 @@ def check_exception_info(exc_info, expected_msg: str, expected_error_code: int | def check_exception_info_not_found(exc_info, msg_not_found: str) -> None: check_exception_info(exc_info, msg_not_found, status.HTTP_404_NOT_FOUND) + + +def check_currency(name: str, price: float, timestamp: int, currencies: tuple): + seconds = 10 + assert name in currencies + assert isinstance(price, float) + assert isinstance(timestamp, int) + res = ((dt.now() - timedelta(seconds=seconds)).timestamp() < + timestamp < + (dt.now() + timedelta(seconds=seconds)).timestamp()) + assert res