didier/tests/test_database/test_crud/test_users.py

26 lines
827 B
Python
Raw Normal View History

2022-06-30 21:33:37 +02:00
from sqlalchemy import select
2022-07-27 21:10:43 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
2022-06-30 21:33:37 +02:00
from database.crud import users as crud
2022-08-29 20:24:42 +02:00
from database.schemas import User
2022-06-30 21:33:37 +02:00
2022-07-27 21:10:43 +02:00
async def test_get_or_add_non_existing(postgres: AsyncSession):
2022-06-30 21:33:37 +02:00
"""Test get_or_add for a user that doesn't exist"""
2022-08-29 20:49:29 +02:00
await crud.get_or_add_user(postgres, 1)
2022-06-30 21:33:37 +02:00
statement = select(User)
2022-07-25 19:12:27 +02:00
res = (await postgres.execute(statement)).scalars().all()
2022-06-30 21:33:37 +02:00
assert len(res) == 1
assert res[0].bank is not None
assert res[0].nightly_data is not None
2022-07-27 21:10:43 +02:00
async def test_get_or_add_existing(postgres: AsyncSession):
2022-06-30 21:33:37 +02:00
"""Test get_or_add for a user that does exist"""
2022-08-29 20:49:29 +02:00
user = await crud.get_or_add_user(postgres, 1)
2022-06-30 21:33:37 +02:00
bank = user.bank
2022-08-29 20:49:29 +02:00
assert await crud.get_or_add_user(postgres, 1) == user
assert (await crud.get_or_add_user(postgres, 1)).bank == bank