Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: persist notebook answer images

wangyuxy9805-web44ae34f65377
diff --git a/deeptutor/services/session/sqlite_store.py b/deeptutor/services/session/sqlite_store.py
index 866cc60c0..8b2f9a047 100644
--- a/deeptutor/services/session/sqlite_store.py
+++ b/deeptutor/services/session/sqlite_store.py
@@ -2320,7 +2320,7 @@ class SQLiteSessionStore:
material_title, section_id, section_title, score_trend,
is_correct, resolved, bookmarked, followup_session_id,
created_at, updated_at
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, '', ?, ?)
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, '', ?, ?)
ON CONFLICT(session_id, turn_id, question_id) DO UPDATE SET
question = excluded.question,
question_type = excluded.question_type,
diff --git a/tests/api/test_notebook_router.py b/tests/api/test_notebook_router.py
index fecb9fc05..b8a29ee25 100644
--- a/tests/api/test_notebook_router.py
+++ b/tests/api/test_notebook_router.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
+import base64
import importlib
from pathlib import Path
@@ -14,6 +15,7 @@ notebook_router = importlib.import_module("deeptutor.api.routers.question_notebo
sessions_router = importlib.import_module("deeptutor.api.routers.sessions").router
from deeptutor.services.session.sqlite_store import SQLiteSessionStore
+from deeptutor.services.storage.attachment_store import LocalDiskAttachmentStore
def _build_app(store: SQLiteSessionStore) -> FastAPI:
@@ -107,6 +109,52 @@ def test_list_entries_empty(store: SQLiteSessionStore) -> None:
assert resp.json() == {"items": [], "total": 0}
+def test_upsert_entry_persists_base64_answer_image(
+ store: SQLiteSessionStore, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ session = asyncio.run(store.create_session(title="Image answer"))
+ attachment_store = LocalDiskAttachmentStore(root=tmp_path / "attachments")
+ monkeypatch.setattr(
+ "deeptutor.api.routers.question_notebook.get_attachment_store",
+ lambda: attachment_store,
+ )
+
+ with TestClient(_build_app(store)) as client:
+ response = client.post(
+ "/api/question-notebook/entries/upsert",
+ json={
+ "session_id": session["id"],
+ "question_id": "image-question",
+ "question": "Identify the diagram.",
+ "user_answer_images": [
+ {
+ "id": "answer-image-1",
+ "base64": base64.b64encode(b"image-bytes").decode("ascii"),
+ "filename": "answer.png",
+ "mime_type": "image/png",
+ }
+ ],
+ },
+ )
+
+ assert response.status_code == 200
+ assert response.json()["user_answer_images"] == [
+ {
+ "id": "answer-image-1",
+ "url": (f"/files/attachments/{session['id']}/answer-image-1/answer.png"),
+ "filename": "answer.png",
+ "mime_type": "image/png",
+ }
+ ]
+ stored_path = attachment_store.resolve_path(
+ session_id=session["id"],
+ attachment_id="answer-image-1",
+ filename="answer.png",
+ )
+ assert stored_path is not None
+ assert stored_path.read_bytes() == b"image-bytes"
+
+
def test_list_entries_filters_by_course_and_total(
store: SQLiteSessionStore, course_service
) -> None: