mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Tests for birthday commands, overwrite existing birthdays
This commit is contained in:
parent
f49f32d2e9
commit
adcf94c66e
9 changed files with 103 additions and 19 deletions
|
|
@ -12,6 +12,10 @@ from didier import Didier
|
|||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def event_loop() -> Generator:
|
||||
"""Fixture to change the event loop
|
||||
|
||||
This fixes a lot of headaches during async tests
|
||||
"""
|
||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||
yield loop
|
||||
loop.close()
|
||||
|
|
@ -33,6 +37,7 @@ async def tables():
|
|||
@pytest.fixture
|
||||
async def database_session(tables) -> AsyncGenerator[AsyncSession, None]:
|
||||
"""Fixture to create a session for every test
|
||||
|
||||
Rollbacks the transaction afterwards so that the future tests start with a clean database
|
||||
"""
|
||||
connection = await engine.connect()
|
||||
|
|
@ -52,6 +57,7 @@ async def database_session(tables) -> AsyncGenerator[AsyncSession, None]:
|
|||
@pytest.fixture
|
||||
def mock_client() -> Didier:
|
||||
"""Fixture to get a mock Didier instance
|
||||
|
||||
The mock uses 0 as the id
|
||||
"""
|
||||
mock_client = MagicMock()
|
||||
|
|
|
|||
|
|
@ -3,7 +3,33 @@ import datetime
|
|||
import pytest
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.models import UforaAnnouncement, UforaCourse, UforaCourseAlias
|
||||
from database.crud import users
|
||||
from database.models import Bank, UforaAnnouncement, UforaCourse, UforaCourseAlias, User
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def test_user_id() -> int:
|
||||
"""User id used when creating the debug user
|
||||
|
||||
Fixture is useful when comparing, fetching data, ...
|
||||
"""
|
||||
return 1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def user(database_session: AsyncSession, test_user_id) -> User:
|
||||
"""Fixture to create a user"""
|
||||
_user = await users.get_or_add(database_session, test_user_id)
|
||||
await database_session.refresh(_user)
|
||||
return _user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def bank(database_session: AsyncSession, user: User) -> Bank:
|
||||
"""Fixture to fetch the test user's bank"""
|
||||
_bank = user.bank
|
||||
await database_session.refresh(_bank)
|
||||
return _bank
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
|
|||
47
tests/test_database/test_crud/test_birthdays.py
Normal file
47
tests/test_database/test_crud/test_birthdays.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
from datetime import datetime, timedelta
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import birthdays as crud
|
||||
from database.models import User
|
||||
|
||||
|
||||
async def test_add_birthday_not_present(database_session: AsyncSession, user: User):
|
||||
"""Test setting a user's birthday when it doesn't exist yet"""
|
||||
assert user.birthday is None
|
||||
|
||||
bd_date = datetime.today().date()
|
||||
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
|
||||
|
||||
|
||||
async def test_add_birthday_overwrite(database_session: AsyncSession, user: User):
|
||||
"""Test that setting a user's birthday when it already exists overwrites it"""
|
||||
bd_date = datetime.today().date()
|
||||
await crud.add_birthday(database_session, user.user_id, bd_date)
|
||||
await database_session.refresh(user)
|
||||
assert user.birthday is not None
|
||||
|
||||
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
|
||||
|
||||
|
||||
async def test_get_birthday_exists(database_session: AsyncSession, user: User):
|
||||
"""Test getting a user's birthday when it exists"""
|
||||
bd_date = datetime.today().date()
|
||||
await crud.add_birthday(database_session, user.user_id, bd_date)
|
||||
await database_session.refresh(user)
|
||||
|
||||
bd = await crud.get_birthday_for_user(database_session, user.user_id)
|
||||
assert bd is not None
|
||||
assert bd.birthday.date() == bd_date
|
||||
|
||||
|
||||
async def test_get_birthday_not_exists(database_session: AsyncSession, user: User):
|
||||
"""Test getting a user's birthday when it doesn't exist"""
|
||||
bd = await crud.get_birthday_for_user(database_session, user.user_id)
|
||||
assert bd is None
|
||||
|
|
@ -6,16 +6,6 @@ from database.exceptions import currency as exceptions
|
|||
from database.models import Bank
|
||||
|
||||
|
||||
DEBUG_USER_ID = 1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def bank(database_session: AsyncSession) -> Bank:
|
||||
_bank = await crud.get_bank(database_session, DEBUG_USER_ID)
|
||||
await database_session.refresh(_bank)
|
||||
return _bank
|
||||
|
||||
|
||||
async def test_add_dinks(database_session: AsyncSession, bank: Bank):
|
||||
"""Test adding dinks to an account"""
|
||||
assert bank.dinks == 0
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import datetime
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import ufora_announcements as crud
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ def test_abbreviated_no_number():
|
|||
|
||||
def test_abbreviated_float_floors():
|
||||
"""Test abbreviated_number for a float that is longer than the unit
|
||||
|
||||
Example:
|
||||
5.3k is 5300, but 5.3001k is 5300.1
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue