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
|
|
@ -4,14 +4,26 @@ from typing import Optional
|
|||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import users
|
||||
from database.models import Birthday
|
||||
|
||||
__all__ = ["add_birthday", "get_birthday_for_user"]
|
||||
|
||||
|
||||
async def add_birthday(session: AsyncSession, user_id: int, birthday: date):
|
||||
"""Add a user's birthday into the database"""
|
||||
bd = Birthday(user_id=user_id, birthday=birthday)
|
||||
"""Add a user's birthday into the database
|
||||
|
||||
If already present, overwrites the existing one
|
||||
"""
|
||||
user = await users.get_or_add(session, user_id)
|
||||
|
||||
if user.birthday is not None:
|
||||
bd = user.birthday
|
||||
await session.refresh(bd)
|
||||
bd.birthday = birthday
|
||||
else:
|
||||
bd = Birthday(user_id=user_id, birthday=birthday)
|
||||
|
||||
session.add(bd)
|
||||
await session.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ class User(Base):
|
|||
bank: Bank = relationship(
|
||||
"Bank", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
birthday: Birthday = relationship(
|
||||
birthday: Optional[Birthday] = relationship(
|
||||
"Birthday", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
nightly_data: NightlyData = relationship(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue