refactor: fixture_loader and conftest refactored
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user