Spaces:
Sleeping
Sleeping
Download tests/test_memory.py from baljot07/studiq-backend: direct link, hf CLI and curl.
- Browser
- Download file 5.45 kB
-
https://huggingface.co/spaces/baljot07/studiq-backend/resolve/main/tests/test_memory.py
- Command line
-
hf download hf://spaces/baljot07/studiq-backend/tests/test_memory.py
-
curl -L -o test_memory.py https://huggingface.co/spaces/baljot07/studiq-backend/resolve/main/tests/test_memory.py
5.45 kB
| """Tests for the daily memory generation service.""" | |
| import uuid | |
| from unittest.mock import AsyncMock, patch | |
| import pytest | |
| from httpx import AsyncClient | |
| from sqlalchemy import select | |
| from app.models.memory import DailyMemory | |
| from app.models.message import Message | |
| # ββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def _create_user_with_messages(client: AsyncClient, msg_count: int = 6): | |
| """Create a user and insert fake chat messages.""" | |
| from tests.conftest import create_authenticated_user | |
| user_id_str, token = await create_authenticated_user(client) | |
| user_id = uuid.UUID(user_id_str) | |
| # Insert messages directly into DB | |
| from tests.conftest import TestSessionLocal | |
| async with TestSessionLocal() as db: | |
| for i in range(msg_count): | |
| role = "user" if i % 2 == 0 else "assistant" | |
| content = ( | |
| f"What is normalization? (msg {i})" | |
| if role == "user" | |
| else f"Normalization is the process of organizing data... (msg {i})" | |
| ) | |
| db.add( | |
| Message( | |
| user_id=user_id, | |
| role=role, | |
| content=content, | |
| ) | |
| ) | |
| await db.commit() | |
| return user_id, token | |
| def _auth(token: str) -> dict: | |
| return {"Authorization": f"Bearer {token}"} | |
| # ββ Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def test_memory_generated_after_threshold(client: AsyncClient): | |
| """Memory should be generated when message count >= 4.""" | |
| user_id, token = await _create_user_with_messages(client, msg_count=6) | |
| mock_summary = "Student asked about normalization in DBMS. They understood 1NF but struggled with 3NF." | |
| with patch( | |
| "app.services.memory_service.call_llm", | |
| new_callable=AsyncMock, | |
| return_value=mock_summary, | |
| ): | |
| from app.services.memory_service import maybe_update_daily_memory | |
| from tests.conftest import TestSessionLocal | |
| async with TestSessionLocal() as db: | |
| await maybe_update_daily_memory(user_id, db) | |
| # Check memory was created | |
| async with TestSessionLocal() as db: | |
| result = await db.execute( | |
| select(DailyMemory).where(DailyMemory.user_id == user_id) | |
| ) | |
| memory = result.scalar_one_or_none() | |
| assert memory is not None | |
| assert "normalization" in memory.summary.lower() | |
| assert memory.message_count == 6 | |
| async def test_memory_skipped_below_threshold(client: AsyncClient): | |
| """Memory should NOT be generated when message count < 4.""" | |
| user_id, token = await _create_user_with_messages(client, msg_count=2) | |
| from app.services.memory_service import maybe_update_daily_memory | |
| from tests.conftest import TestSessionLocal | |
| async with TestSessionLocal() as db: | |
| await maybe_update_daily_memory(user_id, db) | |
| # Check no memory was created | |
| async with TestSessionLocal() as db: | |
| result = await db.execute( | |
| select(DailyMemory).where(DailyMemory.user_id == user_id) | |
| ) | |
| memory = result.scalar_one_or_none() | |
| assert memory is None | |
| async def test_memory_updates_on_new_messages(client: AsyncClient): | |
| """Memory should update when 4+ new messages arrive after initial summary.""" | |
| user_id, token = await _create_user_with_messages(client, msg_count=6) | |
| mock_summary_1 = "First summary about normalization." | |
| mock_summary_2 = "Updated summary with linked lists added." | |
| from app.services.memory_service import maybe_update_daily_memory | |
| from tests.conftest import TestSessionLocal | |
| # First memory generation | |
| with patch( | |
| "app.services.memory_service.call_llm", | |
| new_callable=AsyncMock, | |
| return_value=mock_summary_1, | |
| ): | |
| async with TestSessionLocal() as db: | |
| await maybe_update_daily_memory(user_id, db) | |
| # Add 4 more messages | |
| async with TestSessionLocal() as db: | |
| for i in range(4): | |
| role = "user" if i % 2 == 0 else "assistant" | |
| db.add( | |
| Message( | |
| user_id=user_id, | |
| role=role, | |
| content=f"Tell me about linked lists (extra msg {i})", | |
| ) | |
| ) | |
| await db.commit() | |
| # Second memory generation | |
| with patch( | |
| "app.services.memory_service.call_llm", | |
| new_callable=AsyncMock, | |
| return_value=mock_summary_2, | |
| ): | |
| async with TestSessionLocal() as db: | |
| await maybe_update_daily_memory(user_id, db) | |
| # Check memory was updated | |
| async with TestSessionLocal() as db: | |
| result = await db.execute( | |
| select(DailyMemory).where(DailyMemory.user_id == user_id) | |
| ) | |
| memory = result.scalar_one_or_none() | |
| assert memory is not None | |
| assert "linked lists" in memory.summary.lower() | |
| assert memory.message_count == 10 | |