-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
78 lines (63 loc) · 2.17 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Conftest for the application"""
import asyncio
import pytest
from uuid import uuid4
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncEngine
from sqlmodel.ext.asyncio.session import AsyncSession
import pytest_asyncio
from main import app
from app.core.init_db import (
add_main_skills,
original_final,
new_user_data_1,
new_user_data_2,
)
from app.core.settings import settings
from app.core.db import get_session
from app.core.security import hash_password
from app.models import Users, SQLModel
# Initilizing the engine and session
engine_test: AsyncEngine = create_async_engine(settings.TEST_DB_URI)
async_session_maker = async_sessionmaker(
bind=engine_test,
class_=AsyncSession,
expire_on_commit=False,
)
# # yielding session
async def override_get_session() -> AsyncSession: # type: ignore
async with async_session_maker() as session:
yield session
# # Dependency_overriding
app.dependency_overrides[get_session] = override_get_session
# Test data loading
# Event loop to avoid Future pending error
@pytest.fixture(scope="session")
def event_loop():
return asyncio.get_event_loop()
# making tables and inserting sample data with order1
@pytest.mark.order1
@pytest.fixture(scope="session", autouse=True)
async def create_tables():
async with engine_test.begin() as conn:
await conn.run_sync(SQLModel.metadata.drop_all)
await conn.run_sync(SQLModel.metadata.create_all)
async with async_session_maker() as session:
id_1 = uuid4()
id_2 = uuid4()
pytest.id_1 = id_1
pytest.id_2 = id_2
new_user_1 = Users(**new_user_data_1, id=id_1)
new_user_2 = Users(**new_user_data_2, id=id_2)
session.add(new_user_1)
session.add(new_user_2)
await session.commit()
await add_main_skills(session=session, skills=original_final)
yield
async with engine_test.begin() as conn:
await conn.run_sync(SQLModel.metadata.drop_all)
# yeilding async client
@pytest_asyncio.fixture
async def async_client():
async with AsyncClient(app=app, base_url=f"http://test") as client:
yield client