add tests for project #1
@@ -1,24 +1,63 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import NullPool, insert
|
from sqlalchemy import NullPool, insert
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||||
|
|
||||||
|
from src.api.dependacies.db_dep import get_db
|
||||||
from src.core.auth_manager import AuthManager
|
from src.core.auth_manager import AuthManager
|
||||||
from src.core.database import Base
|
from src.core.database import Base
|
||||||
|
from src.main import app
|
||||||
from src.models import * # noqa: F403
|
from src.models import * # noqa: F403
|
||||||
|
from src.repository.tasks import TasksRepo
|
||||||
|
from src.repository.users import UsersRepo
|
||||||
|
|
||||||
engine_null_pool = create_async_engine('sqlite+aiosqlite:///tests/test_db.db', poolclass=NullPool)
|
engine_null_pool = create_async_engine(
|
||||||
|
"sqlite+aiosqlite:///tests/test_db.db", poolclass=NullPool
|
||||||
|
)
|
||||||
|
test_session_maker = async_sessionmaker(engine_null_pool, expire_on_commit=False)
|
||||||
|
|
||||||
|
|
||||||
|
class TestDBManager:
|
||||||
|
|
||||||
|
async def __aenter__(self) -> "TestDBManager":
|
||||||
|
self.session: AsyncSession = test_session_maker()
|
||||||
|
self.user = UsersRepo(self.session)
|
||||||
|
self.task = TasksRepo(self.session)
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
await self.session.rollback()
|
||||||
|
await self.session.close()
|
||||||
|
|
||||||
|
|
||||||
|
async def get_test_db():
|
||||||
|
async with TestDBManager() as db:
|
||||||
|
yield db
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="function")
|
||||||
|
async def db():
|
||||||
|
async for db in get_test_db():
|
||||||
|
yield db
|
||||||
|
|
||||||
|
|
||||||
|
app.dependency_overrides[get_db] = get_test_db
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session", autouse=True)
|
@pytest.fixture(scope="session", autouse=True)
|
||||||
async def setup_database():
|
async def setup_database():
|
||||||
hashed_pass = AuthManager.get_password_hash("admin")
|
hashed_pass = AuthManager.get_password_hash("admin")
|
||||||
user_admin = {"username": "admin", "hashed_password": hashed_pass, "is_superuser": True}
|
user_admin = {
|
||||||
|
"username": "admin",
|
||||||
|
"hashed_password": hashed_pass,
|
||||||
|
"is_superuser": True,
|
||||||
|
}
|
||||||
async with engine_null_pool.begin() as conn:
|
async with engine_null_pool.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.drop_all)
|
await conn.run_sync(Base.metadata.drop_all)
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
async with async_sessionmaker(engine_null_pool, expire_on_commit=False)() as conn:
|
async with test_session_maker() as conn:
|
||||||
result = await conn.execute(insert(UsersORM).values(user_admin).returning(UsersORM)) # noqa: F405
|
result = await conn.execute(
|
||||||
|
insert(UsersORM).values(user_admin).returning(UsersORM)) # noqa: F405
|
||||||
await conn.commit()
|
await conn.commit()
|
||||||
admin = result.scalar_one()
|
admin = result.scalar_one()
|
||||||
assert admin.is_superuser is True
|
assert admin.is_superuser is True
|
||||||
|
|||||||
Reference in New Issue
Block a user