2022-07-19 23:35:41 +02:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2022-07-23 22:34:03 +02:00
|
|
|
from freezegun import freeze_time
|
2022-07-27 21:10:43 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
2022-07-19 23:35:41 +02:00
|
|
|
|
|
|
|
from database.crud import birthdays as crud
|
2022-07-23 22:34:03 +02:00
|
|
|
from database.crud import users
|
2022-07-25 21:20:09 +02:00
|
|
|
from database.schemas.relational import User
|
2022-07-19 23:35:41 +02:00
|
|
|
|
|
|
|
|
2022-07-27 21:10:43 +02:00
|
|
|
async def test_add_birthday_not_present(postgres: AsyncSession, user: User):
|
2022-07-19 23:35:41 +02:00
|
|
|
"""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)
|
2022-07-19 23:35:41 +02:00
|
|
|
assert user.birthday is not None
|
2022-07-23 20:59:02 +02:00
|
|
|
assert user.birthday.birthday == bd_date
|
2022-07-19 23:35:41 +02:00
|
|
|
|
|
|
|
|
2022-07-27 21:10:43 +02:00
|
|
|
async def test_add_birthday_overwrite(postgres: AsyncSession, user: User):
|
2022-07-19 23:35:41 +02:00
|
|
|
"""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)
|
2022-07-19 23:35:41 +02:00
|
|
|
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-19 23:35:41 +02:00
|
|
|
|
|
|
|
|
2022-07-27 21:10:43 +02:00
|
|
|
async def test_get_birthday_exists(postgres: AsyncSession, user: User):
|
2022-07-19 23:35:41 +02:00
|
|
|
"""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-19 23:35:41 +02:00
|
|
|
|
2022-07-25 19:12:27 +02:00
|
|
|
bd = await crud.get_birthday_for_user(postgres, user.user_id)
|
2022-07-19 23:35:41 +02:00
|
|
|
assert bd is not None
|
2022-07-23 20:59:02 +02:00
|
|
|
assert bd.birthday == bd_date
|
2022-07-19 23:35:41 +02:00
|
|
|
|
|
|
|
|
2022-07-27 21:10:43 +02:00
|
|
|
async def test_get_birthday_not_exists(postgres: AsyncSession, user: User):
|
2022-07-19 23:35:41 +02:00
|
|
|
"""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)
|
2022-07-19 23:35:41 +02:00
|
|
|
assert bd is None
|
2022-07-23 22:34:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
@freeze_time("2022/07/23")
|
2022-07-27 21:10:43 +02:00
|
|
|
async def test_get_birthdays_on_day(postgres: AsyncSession, user: User):
|
2022-07-23 22:34:03 +02:00
|
|
|
"""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-23 22:34:03 +02:00
|
|
|
|
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())
|
2022-07-23 22:34:03 +02:00
|
|
|
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):
|
2022-07-23 22:34:03 +02:00
|
|
|
"""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())
|
2022-07-23 22:34:03 +02:00
|
|
|
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-23 22:34:03 +02:00
|
|
|
|
2022-07-25 19:12:27 +02:00
|
|
|
birthdays = await crud.get_birthdays_on_day(postgres, datetime.today())
|
2022-07-23 22:34:03 +02:00
|
|
|
assert len(birthdays) == 0
|