didier/tests/test_database/test_crud/test_birthdays.py

75 lines
2.8 KiB
Python
Raw Normal View History

from datetime import datetime, timedelta
from freezegun import freeze_time
2022-07-27 21:10:43 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
from database.crud import birthdays as crud
from database.crud import users
2022-07-25 21:20:09 +02:00
from database.schemas.relational import User
2022-07-27 21:10:43 +02:00
async def test_add_birthday_not_present(postgres: 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()
2022-07-25 19:12:27 +02:00
await crud.add_birthday(postgres, user.user_id, bd_date)
await postgres.refresh(user)
assert user.birthday is not None
2022-07-23 20:59:02 +02:00
assert user.birthday.birthday == bd_date
2022-07-27 21:10:43 +02:00
async def test_add_birthday_overwrite(postgres: AsyncSession, user: User):
"""Test that setting a user's birthday when it already exists overwrites it"""
bd_date = datetime.today().date()
2022-07-25 19:12:27 +02:00
await crud.add_birthday(postgres, user.user_id, bd_date)
await postgres.refresh(user)
assert user.birthday is not None
new_bd_date = bd_date + timedelta(weeks=1)
2022-07-25 19:12:27 +02:00
await crud.add_birthday(postgres, user.user_id, new_bd_date)
await postgres.refresh(user)
2022-07-23 20:59:02 +02:00
assert user.birthday.birthday == new_bd_date
2022-07-27 21:10:43 +02:00
async def test_get_birthday_exists(postgres: AsyncSession, user: User):
"""Test getting a user's birthday when it exists"""
bd_date = datetime.today().date()
2022-07-25 19:12:27 +02:00
await crud.add_birthday(postgres, user.user_id, bd_date)
await postgres.refresh(user)
2022-07-25 19:12:27 +02:00
bd = await crud.get_birthday_for_user(postgres, user.user_id)
assert bd is not None
2022-07-23 20:59:02 +02:00
assert bd.birthday == bd_date
2022-07-27 21:10:43 +02:00
async def test_get_birthday_not_exists(postgres: AsyncSession, user: User):
"""Test getting a user's birthday when it doesn't exist"""
2022-07-25 19:12:27 +02:00
bd = await crud.get_birthday_for_user(postgres, user.user_id)
assert bd is None
@freeze_time("2022/07/23")
2022-07-27 21:10:43 +02:00
async def test_get_birthdays_on_day(postgres: AsyncSession, user: User):
"""Test getting all birthdays on a given day"""
2022-07-25 19:12:27 +02:00
await crud.add_birthday(postgres, user.user_id, datetime.today().replace(year=2001))
2022-07-25 19:12:27 +02:00
user_2 = await users.get_or_add(postgres, user.user_id + 1)
await crud.add_birthday(postgres, user_2.user_id, datetime.today() + timedelta(weeks=1))
birthdays = await crud.get_birthdays_on_day(postgres, datetime.today())
assert len(birthdays) == 1
assert birthdays[0].user_id == user.user_id
@freeze_time("2022/07/23")
2022-07-27 21:10:43 +02:00
async def test_get_birthdays_none_present(postgres: AsyncSession):
"""Test getting all birthdays when there are none"""
2022-07-25 19:12:27 +02:00
birthdays = await crud.get_birthdays_on_day(postgres, datetime.today())
assert len(birthdays) == 0
# Add a random birthday that is not today
2022-07-25 19:12:27 +02:00
await crud.add_birthday(postgres, 1, datetime.today() + timedelta(days=1))
2022-07-25 19:12:27 +02:00
birthdays = await crud.get_birthdays_on_day(postgres, datetime.today())
assert len(birthdays) == 0