feat: add memory_facts DB helpers
Implement four async DB-helper functions for memory_facts table: - insert_memory_fact(user_id, content) -> int - search_memory_facts(query) -> list - delete_memory_fact(fact_id) -> None - get_all_memory_facts() -> list Add test_memory.py with 4 passing tests for these helpers. All tests pass with mocked db_query. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019iZMEPk2Kt1UC96Lo9bC5w
This commit is contained in:
parent
8856ef16a1
commit
18e2ff87d2
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,56 @@
|
|||
import os
|
||||
import sys
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
|
||||
# Mock psycopg2 before importing main
|
||||
sys.modules["psycopg2"] = MagicMock()
|
||||
sys.modules["psycopg2.pool"] = MagicMock()
|
||||
sys.modules["psycopg2.extras"] = MagicMock()
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
|
||||
import pytest
|
||||
|
||||
import main
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_insert_memory_fact_returns_new_id():
|
||||
with patch.object(main, "db_query", new=AsyncMock(return_value={"id": 42})) as mock_query:
|
||||
fact_id = await main.insert_memory_fact(1, "Mag lieber Tee als Kaffee")
|
||||
|
||||
assert fact_id == 42
|
||||
args, kwargs = mock_query.call_args
|
||||
assert "INSERT INTO memory_facts" in args[0]
|
||||
assert args[1] == (1, "Mag lieber Tee als Kaffee")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_memory_facts_uses_ilike():
|
||||
rows = [{"id": 1, "content": "Hund heisst Bruno"}]
|
||||
with patch.object(main, "db_query", new=AsyncMock(return_value=rows)) as mock_query:
|
||||
result = await main.search_memory_facts("Bruno")
|
||||
|
||||
assert result == rows
|
||||
args, kwargs = mock_query.call_args
|
||||
assert "ILIKE" in args[0]
|
||||
assert args[1] == ("%Bruno%",)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_memory_fact_deletes_by_id():
|
||||
with patch.object(main, "db_query", new=AsyncMock(return_value=None)) as mock_query:
|
||||
await main.delete_memory_fact(7)
|
||||
|
||||
args, kwargs = mock_query.call_args
|
||||
assert "DELETE FROM memory_facts" in args[0]
|
||||
assert args[1] == (7,)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_all_memory_facts_returns_rows():
|
||||
rows = [{"id": 1, "content": "A"}, {"id": 2, "content": "B"}]
|
||||
with patch.object(main, "db_query", new=AsyncMock(return_value=rows)):
|
||||
result = await main.get_all_memory_facts()
|
||||
|
||||
assert result == rows
|
||||
Loading…
Reference in New Issue