From 66997b7556530f3fa7f6138133075a780138a2d4 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Sat, 23 Jul 2022 20:59:02 +0200 Subject: [PATCH] Fix broken migration --- alembic/versions/346b408c362a_create_tasks.py | 1 + database/crud/currency.py | 6 +++--- requirements-dev.txt | 1 + tests/conftest.py | 2 +- tests/test_database/test_crud/test_birthdays.py | 6 +++--- tests/test_database/test_crud/test_currency.py | 8 ++++++++ 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/alembic/versions/346b408c362a_create_tasks.py b/alembic/versions/346b408c362a_create_tasks.py index 25f1530..f6efeeb 100644 --- a/alembic/versions/346b408c362a_create_tasks.py +++ b/alembic/versions/346b408c362a_create_tasks.py @@ -32,4 +32,5 @@ def upgrade() -> None: def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.drop_table("tasks") + sa.Enum("BIRTHDAYS", "UFORA_ANNOUNCEMENTS", name="tasktype").drop(op.get_bind()) # ### end Alembic commands ### diff --git a/database/crud/currency.py b/database/crud/currency.py index dea303e..1bb2d11 100644 --- a/database/crud/currency.py +++ b/database/crud/currency.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import date from typing import Union from sqlalchemy.ext.asyncio import AsyncSession @@ -69,9 +69,9 @@ async def claim_nightly(session: AsyncSession, user_id: int): """Claim daily Dinks""" nightly_data = await get_nightly_data(session, user_id) - now = datetime.now() + now = date.today() - if nightly_data.last_nightly is not None and nightly_data.last_nightly == now.date(): + if nightly_data.last_nightly is not None and nightly_data.last_nightly == now: raise exceptions.DoubleNightly bank = await get_bank(session, user_id) diff --git a/requirements-dev.txt b/requirements-dev.txt index d82fde3..64b8467 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,5 +1,6 @@ black==22.3.0 coverage[toml]==6.4.1 +freezegun==1.2.1 mypy==0.961 pre-commit==2.20.0 pytest==7.1.2 diff --git a/tests/conftest.py b/tests/conftest.py index 8530de5..2e425ef 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,7 +23,7 @@ def event_loop() -> Generator: @pytest.fixture(scope="session") async def tables(): - """Initialize a database before the tests, and then tear it down again + """Fixture to initialize a database before the tests, and then tear it down again Checks that the migrations were successful by asserting that we are currently on the latest migration diff --git a/tests/test_database/test_crud/test_birthdays.py b/tests/test_database/test_crud/test_birthdays.py index 96b924c..ba90a3a 100644 --- a/tests/test_database/test_crud/test_birthdays.py +++ b/tests/test_database/test_crud/test_birthdays.py @@ -14,7 +14,7 @@ async def test_add_birthday_not_present(database_session: AsyncSession, user: Us await crud.add_birthday(database_session, user.user_id, bd_date) await database_session.refresh(user) assert user.birthday is not None - assert user.birthday.birthday.date() == bd_date + assert user.birthday.birthday == bd_date async def test_add_birthday_overwrite(database_session: AsyncSession, user: User): @@ -27,7 +27,7 @@ async def test_add_birthday_overwrite(database_session: AsyncSession, user: User new_bd_date = bd_date + timedelta(weeks=1) await crud.add_birthday(database_session, user.user_id, new_bd_date) await database_session.refresh(user) - assert user.birthday.birthday.date() == new_bd_date + assert user.birthday.birthday == new_bd_date async def test_get_birthday_exists(database_session: AsyncSession, user: User): @@ -38,7 +38,7 @@ async def test_get_birthday_exists(database_session: AsyncSession, user: User): bd = await crud.get_birthday_for_user(database_session, user.user_id) assert bd is not None - assert bd.birthday.date() == bd_date + assert bd.birthday == bd_date async def test_get_birthday_not_exists(database_session: AsyncSession, user: User): diff --git a/tests/test_database/test_crud/test_currency.py b/tests/test_database/test_crud/test_currency.py index 1f0a163..a2eeec8 100644 --- a/tests/test_database/test_crud/test_currency.py +++ b/tests/test_database/test_crud/test_currency.py @@ -1,4 +1,7 @@ +import datetime + import pytest +from freezegun import freeze_time from sqlalchemy.ext.asyncio import AsyncSession from database.crud import currency as crud @@ -14,13 +17,18 @@ async def test_add_dinks(database_session: AsyncSession, bank: Bank): assert bank.dinks == 10 +@freeze_time("2022/07/23") async def test_claim_nightly_available(database_session: AsyncSession, bank: Bank): """Test claiming nightlies when it hasn't been done yet""" await crud.claim_nightly(database_session, bank.user_id) await database_session.refresh(bank) assert bank.dinks == crud.NIGHTLY_AMOUNT + nightly_data = await crud.get_nightly_data(database_session, bank.user_id) + assert nightly_data.last_nightly == datetime.date(year=2022, month=7, day=23) + +@freeze_time("2022/07/23") async def test_claim_nightly_unavailable(database_session: AsyncSession, bank: Bank): """Test claiming nightlies twice in a day""" await crud.claim_nightly(database_session, bank.user_id)