link_shortener/app/tests/conftest.py
2024-04-05 16:23:27 +05:00

41 lines
1,011 B
Python

import json
import pytest
from sqlalchemy import insert
from httpx import AsyncClient
from app.config import settings
from app.database import Base, async_session_maker, engine
from app.models import URLs
from app.main import app as fastapi_app
@pytest.fixture(autouse=True, scope='module')
async def prepare_database():
assert settings.MODE == "TEST"
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
def open_mock_json(model: str):
with open(f"app/tests/mock_{model}.json", 'r', encoding='utf8') as file:
return json.load(file)
urls = open_mock_json("urls")
async with async_session_maker() as session:
add_users = insert(URLs).values(urls)
await session.execute(add_users)
await session.commit()
@pytest.fixture(scope="function")
async def ac():
async with AsyncClient(app=fastapi_app, base_url="http://test") as ac:
yield ac