feat(backend): data loader handles test cases
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Any
|
||||
import yaml
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
# Add src to path for imports
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
@@ -96,28 +97,41 @@ async def load_question(
|
||||
data: dict[str, Any] = yaml.safe_load(f)
|
||||
|
||||
slug = data["slug"]
|
||||
result = await session.execute(select(Question).where(Question.slug == slug))
|
||||
result = await session.execute(
|
||||
select(Question)
|
||||
.where(Question.slug == slug)
|
||||
.options(
|
||||
selectinload(Question.explanation),
|
||||
selectinload(Question.solutions),
|
||||
selectinload(Question.categories),
|
||||
selectinload(Question.patterns),
|
||||
)
|
||||
)
|
||||
existing = result.scalar_one_or_none()
|
||||
|
||||
if existing:
|
||||
question = existing
|
||||
question.title = data["title"]
|
||||
question.difficulty = Difficulty(data["difficulty"])
|
||||
question.difficulty = Difficulty(data["difficulty"].lower())
|
||||
question.description = data["description"]
|
||||
question.constraints = data.get("constraints")
|
||||
question.examples = data.get("examples")
|
||||
question.leetcode_id = data.get("leetcode_id")
|
||||
question.leetcode_url = data.get("leetcode_url")
|
||||
question.function_signature = data.get("function_signature")
|
||||
question.test_cases = data.get("test_cases")
|
||||
else:
|
||||
question = Question(
|
||||
title=data["title"],
|
||||
slug=slug,
|
||||
difficulty=Difficulty(data["difficulty"]),
|
||||
difficulty=Difficulty(data["difficulty"].lower()),
|
||||
description=data["description"],
|
||||
constraints=data.get("constraints"),
|
||||
examples=data.get("examples"),
|
||||
leetcode_id=data.get("leetcode_id"),
|
||||
leetcode_url=data.get("leetcode_url"),
|
||||
function_signature=data.get("function_signature"),
|
||||
test_cases=data.get("test_cases"),
|
||||
)
|
||||
session.add(question)
|
||||
|
||||
@@ -136,7 +150,7 @@ async def load_question(
|
||||
# Handle explanation
|
||||
if "explanation" in data:
|
||||
exp_data = data["explanation"]
|
||||
if question.explanation:
|
||||
if existing and existing.explanation:
|
||||
explanation = question.explanation
|
||||
explanation.approach = exp_data["approach"]
|
||||
explanation.intuition = exp_data["intuition"]
|
||||
|
||||
Reference in New Issue
Block a user