didier/tests/test_database/conftest.py

67 lines
1.8 KiB
Python
Raw Normal View History

import datetime
import pytest
2022-07-27 21:10:43 +02:00
from sqlalchemy.ext.asyncio import AsyncSession
from database.crud import users
2022-08-29 20:24:42 +02:00
from database.schemas import (
2022-07-25 21:20:09 +02:00
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
2022-07-27 21:10:43 +02:00
async def user(postgres: AsyncSession, test_user_id: int) -> User:
"""Fixture to create a user"""
2022-08-29 20:49:29 +02:00
_user = await users.get_or_add_user(postgres, test_user_id)
2022-07-25 19:12:27 +02:00
await postgres.refresh(_user)
return _user
@pytest.fixture
2022-07-27 21:10:43 +02:00
async def bank(postgres: AsyncSession, user: User) -> Bank:
"""Fixture to fetch the test user's bank"""
_bank = user.bank
2022-07-25 19:12:27 +02:00
await postgres.refresh(_bank)
return _bank
@pytest.fixture
2022-07-27 21:10:43 +02:00
async def ufora_course(postgres: AsyncSession) -> UforaCourse:
"""Fixture to create a course"""
course = UforaCourse(name="test", code="code", year=1, log_announcements=True)
2022-07-25 19:12:27 +02:00
postgres.add(course)
await postgres.commit()
return course
@pytest.fixture
2022-07-27 21:10:43 +02:00
async def ufora_course_with_alias(postgres: AsyncSession, ufora_course: UforaCourse) -> UforaCourse:
"""Fixture to create a course with an alias"""
alias = UforaCourseAlias(course_id=ufora_course.course_id, alias="alias")
2022-07-25 19:12:27 +02:00
postgres.add(alias)
await postgres.commit()
await postgres.refresh(ufora_course)
return ufora_course
@pytest.fixture
2022-07-27 21:10:43 +02:00
async def ufora_announcement(postgres: AsyncSession, ufora_course: UforaCourse) -> UforaAnnouncement:
"""Fixture to create an announcement"""
announcement = UforaAnnouncement(course_id=ufora_course.course_id, publication_date=datetime.datetime.now())
2022-07-25 19:12:27 +02:00
postgres.add(announcement)
await postgres.commit()
return announcement