58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import asyncio
|
|
import json
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
from sqlalchemy import insert
|
|
|
|
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
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope='session')
|
|
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') 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')
|
|
# i['date_of_birth'] = i['date_of_birth'].isoformat()
|
|
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="session")
|
|
def event_loop(request):
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
yield loop
|
|
loop.close()
|