59 lines
2 KiB
Python
59 lines
2 KiB
Python
import asyncio
|
|
import json
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
from sqlalchemy import insert
|
|
from fastapi.testclient import TestClient
|
|
from httpx import AsyncClient
|
|
|
|
from app.config import settings
|
|
from app.database import Base, async_session_maker, engine
|
|
from app.users.models import Users, UsersVerificationCodes
|
|
from app.users.chat.models import Chats, UsersXChats, Messages
|
|
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)
|
|
|
|
users = open_mock_json("users")
|
|
users_verification_codes = open_mock_json("verification_codes")
|
|
chats = open_mock_json("chats")
|
|
users_x_chats = open_mock_json("x_chats")
|
|
messages = open_mock_json("messages")
|
|
|
|
new_users = []
|
|
for i in users:
|
|
i['date_of_birth'] = datetime.strptime(i['date_of_birth'], '%Y-%m-%d')
|
|
new_users.append(i)
|
|
|
|
async with async_session_maker() as session:
|
|
add_users = insert(Users).values(new_users)
|
|
add_users_verification_codes = insert(UsersVerificationCodes).values(users_verification_codes)
|
|
add_chats = insert(Chats).values(chats)
|
|
add_users_x_chats = insert(UsersXChats).values(users_x_chats)
|
|
add_messages = insert(Messages).values(messages)
|
|
|
|
await session.execute(add_users)
|
|
await session.execute(add_users_verification_codes)
|
|
await session.execute(add_chats)
|
|
await session.execute(add_users_x_chats)
|
|
await session.execute(add_messages)
|
|
|
|
await session.commit()
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def ac():
|
|
async with AsyncClient(app=fastapi_app, base_url="http://test") as ac:
|
|
yield ac
|