/
burevol
/
GymAssistant
Обзор
Документация
Войти
/
burevol
/
GymAssistant
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
tests/test_db.py
69 строк
2 KB
Alexander Maximov
обновление автонумерации во время миграции
10 май 2025, 23:08
10 май 2025, 23:08
c84fa2c
Код
Авторство
О чём код?
import pytest from sqlalchemy import select from utils import Db from utils.db import User, Category @pytest.fixture async def db(): db = Db() user_exists = await db.user_exists(username="johndoe") if not user_exists: async with db.AsyncSessionLocal() as async_session: # noinspection SpellCheckingInspection fake_user = User(username="johndoe", hashed_password="$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW", disabled=False) async_session.add(fake_user) await async_session.commit() await async_session.refresh(fake_user) yield db await db.delete_user('johndoe') await db.async_engine.dispose() @pytest.fixture async def clean_db(): db = Db() yield db await db.async_engine.dispose() @pytest.fixture async def session(): db = Db() async with db.AsyncSessionLocal() as session: try: yield session finally: await session.rollback() await db.async_engine.dispose() def test_db(db): assert db assert isinstance(db.url, str) async def test_create_user(clean_db): await clean_db.create_first_user() async def test_get_user(db): assert await db.get_user("johndoe") async def test_repr(db): async with db.AsyncSessionLocal() as async_session: stmt = select(User).where(User.username == "johndoe") result = await async_session.scalar(stmt) assert str(result) == "<User johndoe>" async def test_category(session): category = Category(name='Category1') session.add(category) await session.flush() await session.refresh(category) assert isinstance(category.id, int) assert isinstance(category.name, str)