refactor: fixture_loader and conftest refactored

This commit is contained in:
2025-11-26 16:16:26 +04:00
parent ed00d1483d
commit c8af9ffa4d
2 changed files with 10 additions and 19 deletions

View File

@@ -27,7 +27,7 @@ TEST_DATABASE_URL_FOR_ASYNCPG = f"postgresql://{PG_LOGIN}:{PG_PASSWORD}@127.0.0.
# -------------------------------------------------------------------
# Create test database + connection pool
# Create test database and session
# -------------------------------------------------------------------
@pytest.fixture(scope="function")
@@ -40,10 +40,9 @@ def event_loop():
@pytest.fixture(scope="function")
async def db_session() -> AsyncGenerator[AsyncSession, None]:
# Create test engine
test_engine = create_async_engine(
TEST_DATABASE_URL,
poolclass=StaticPool, # Useful for tests
poolclass=StaticPool,
)
# Test session factory
@@ -91,7 +90,7 @@ async def client(db_session: AsyncSession):
# -------------------------------------------------------------------
# Auth token fixture
# Authorized client fixture
# -------------------------------------------------------------------
@pytest.fixture(scope="function")

View File

@@ -4,7 +4,7 @@ from datetime import datetime
from pathlib import Path
from typing import Any
from sqlalchemy import text, Table, insert
from sqlalchemy import Table, insert
from sqlalchemy.ext.asyncio import AsyncSession
from models import User, Role, WorkShift, PayrollScheme, PayRate, user_pay_rate
@@ -52,7 +52,7 @@ class FixtureLoader:
if not os.path.exists(fixture_path):
print(f"Fixture file {fixture_path} not found")
return 0
return
with open(fixture_path, "r") as f:
data = json.load(f)
@@ -60,15 +60,12 @@ class FixtureLoader:
for item_data in data:
converted_data = {}
for key, value in item_data.items():
converted_data[key] = value
if isinstance(value, str) and len(value) == 19:
try:
# Try to parse as datetime
converted_data[key] = datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
except ValueError:
# If it fails, keep the original value
converted_data[key] = value
else:
converted_data[key] = value
db_item = model(**converted_data)
db.add(db_item)
@@ -86,18 +83,13 @@ class FixtureLoader:
if not os.path.exists(fixture_path):
print(f"Fixture file {fixture_path} not found")
return 0
return
with open(fixture_path, "r") as f:
data = json.load(f)
# Use SQLAlchemy insert for association tables
if data:
await db.execute(insert(table), data)
await db.commit()
if not data:
return
async def clear_fixtures(self, db: AsyncSession):
"""Clear all fixture data (useful for testing)"""
for fixture_file, _ in self._fixtures_to_load()[::-1]:
await db.execute(text("DELETE FROM " + fixture_file))
await db.execute(insert(table), data)
await db.commit()