Skip to content

Commit

Permalink
Improved celery-task test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpro2022 committed Dec 10, 2023
1 parent fb9ea85 commit 4eae4e5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import Any, AsyncGenerator, Generator

import pytest
Expand All @@ -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
Expand Down
21 changes: 4 additions & 17 deletions tests/integration_tests/test_client.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
5 changes: 5 additions & 0 deletions tests/unit_tests/test_task.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime as dt
from datetime import timedelta
from typing import Any

from deepdiff import DeepDiff
Expand Down Expand Up @@ -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

0 comments on commit 4eae4e5

Please sign in to comment.