fix document api

This commit is contained in:
phernandez
2024-12-21 22:53:55 -06:00
parent 1db97b59a4
commit f4bc0cabd9
4 changed files with 229 additions and 86 deletions
+138 -2
View File
@@ -5,8 +5,6 @@ from pathlib import Path
import pytest
from httpx import AsyncClient
from basic_memory.schemas.response import DocumentCreateResponse
@pytest.mark.asyncio
async def test_create_document(client: AsyncClient, tmp_path: Path):
@@ -36,6 +34,19 @@ async def test_create_document(client: AsyncClient, tmp_path: Path):
assert "# Test" in content # Has our content
@pytest.mark.asyncio
async def test_create_document_invalid_path(client: AsyncClient, tmp_path: Path):
"""Test creating document in non-existent directory."""
test_doc = {
"path": str(tmp_path / "nonexistent" / "test.md"),
"content": "test content",
"doc_metadata": {"type": "test"},
}
response = await client.post("/documents/", json=test_doc)
assert response.status_code == 201 # We now create parent directories
@pytest.mark.asyncio
async def test_get_document(client: AsyncClient, tmp_path: Path):
"""Test document retrieval endpoint."""
@@ -48,6 +59,7 @@ async def test_get_document(client: AsyncClient, tmp_path: Path):
# Create document
create_response = await client.post("/documents/", json=test_doc)
assert create_response.status_code == 201
created = create_response.json()
# Get document
response = await client.get(f"/documents/{test_doc['path']}")
@@ -63,3 +75,127 @@ async def test_get_document(client: AsyncClient, tmp_path: Path):
assert "id:" in content # Has generated ID
assert "# Test" in content # Has heading
assert "This is a test document" in content # Has body
@pytest.mark.asyncio
async def test_get_nonexistent_document(client: AsyncClient, tmp_path: Path):
"""Test getting a document that doesn't exist."""
response = await client.get(f"/documents/{tmp_path}/nonexistent.md")
assert response.status_code == 404
assert "not found" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_update_document(client: AsyncClient, tmp_path: Path):
"""Test document update endpoint using document ID."""
# Create initial document
test_doc = {
"path": str(tmp_path / "test.md"),
"content": "# Original\nOriginal content.",
"doc_metadata": {"type": "test", "status": "draft"},
}
create_response = await client.post("/documents/", json=test_doc)
assert create_response.status_code == 201
created = create_response.json()
# Update the document
update_doc = {
"id": created["id"],
"content": "# Updated\nUpdated content.",
"doc_metadata": {"type": "test", "status": "final"},
}
response = await client.put(f"/documents/{created['id']}", json=update_doc)
assert response.status_code == 200
data = response.json()
assert data["doc_metadata"] == update_doc["doc_metadata"]
assert "# Updated" in data["content"]
assert "Updated content" in data["content"]
# Verify file was updated
doc_path = Path(test_doc["path"])
content = doc_path.read_text()
assert "# Updated" in content
assert "Updated content" in content
@pytest.mark.asyncio
async def test_update_nonexistent_document(client: AsyncClient):
"""Test updating a document that doesn't exist."""
update_doc = {
"id": 99999, # Non-existent ID
"content": "new content",
"doc_metadata": {"type": "test"},
}
response = await client.put("/documents/99999", json=update_doc)
assert response.status_code == 404
assert "not found" in response.json()["detail"].lower()
@pytest.mark.asyncio
async def test_delete_document(client: AsyncClient, tmp_path: Path):
"""Test document deletion endpoint."""
test_doc = {
"path": str(tmp_path / "test.md"),
"content": "# Test\nTest content.",
"doc_metadata": {"type": "test"},
}
# Create document
create_response = await client.post("/documents/", json=test_doc)
assert create_response.status_code == 201
# Delete document
response = await client.delete(f"/documents/{test_doc['path']}")
assert response.status_code == 204
# Verify document is gone
doc_path = Path(test_doc["path"])
assert not doc_path.exists()
# Verify 404 on subsequent get
get_response = await client.get(f"/documents/{test_doc['path']}")
assert get_response.status_code == 404
@pytest.mark.asyncio
async def test_list_documents(client: AsyncClient, tmp_path: Path):
"""Test document listing endpoint."""
# Create a few test documents
docs = [
{
"path": str(tmp_path / "doc1.md"),
"content": "# Doc 1",
"doc_metadata": {"type": "test", "number": 1},
},
{
"path": str(tmp_path / "doc2.md"),
"content": "# Doc 2",
"doc_metadata": {"type": "test", "number": 2},
},
]
# Create all documents
created_docs = []
for doc in docs:
response = await client.post("/documents/", json=doc)
assert response.status_code == 201
created_docs.append(response.json())
# List all documents
response = await client.get("/documents/")
assert response.status_code == 200
data = response.json()
# We should have both documents
assert len(data) == 2
# Verify all paths are present
paths = {item["path"] for item in data}
expected_paths = {doc["path"] for doc in docs}
assert paths == expected_paths
# Verify metadata was preserved
for item in data:
matching_doc = next(d for d in docs if d["path"] == item["path"])
assert item["doc_metadata"] == matching_doc["doc_metadata"]