mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
Knowledge service test fixtures
This commit is contained in:
@@ -32,7 +32,7 @@ class KnowledgeService:
|
||||
entity_service: EntityService,
|
||||
observation_service: ObservationService,
|
||||
relation_service: RelationService,
|
||||
file_service: FileService, # FileService
|
||||
file_service: FileService,
|
||||
knowledge_writer: KnowledgeWriter,
|
||||
):
|
||||
self.entity_service = entity_service
|
||||
|
||||
@@ -4,6 +4,7 @@ import tempfile
|
||||
from pathlib import Path
|
||||
from typing import AsyncGenerator
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
@@ -15,6 +16,7 @@ from sqlalchemy.ext.asyncio import (
|
||||
from basic_memory import db
|
||||
from basic_memory.config import ProjectConfig
|
||||
from basic_memory.db import DatabaseType
|
||||
from basic_memory.markdown.knowledge_writer import KnowledgeWriter
|
||||
from basic_memory.models import Base
|
||||
from basic_memory.models.knowledge import Entity
|
||||
from basic_memory.repository.document_repository import DocumentRepository
|
||||
@@ -27,6 +29,8 @@ from basic_memory.services import (
|
||||
RelationService,
|
||||
DocumentService,
|
||||
)
|
||||
from basic_memory.services.file_service import FileService
|
||||
from basic_memory.services.knowledge_service import KnowledgeService
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
@@ -135,6 +139,32 @@ async def observation_service(observation_repository: ObservationRepository) ->
|
||||
return ObservationService(observation_repository)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def file_service():
|
||||
"""Create FileService instance."""
|
||||
return FileService()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def knowledge_writer():
|
||||
"""Create writer instance."""
|
||||
return KnowledgeWriter()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def knowledge_service(
|
||||
entity_service: EntityService,
|
||||
observation_service: ObservationService,
|
||||
relation_service: RelationService,
|
||||
file_service: FileService,
|
||||
knowledge_writer: KnowledgeWriter,
|
||||
) -> KnowledgeService:
|
||||
"""Create KnowledgeService with dependencies."""
|
||||
return KnowledgeService(
|
||||
entity_service, observation_service, relation_service, file_service, knowledge_writer
|
||||
)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
async def sample_entity(entity_repository: EntityRepository) -> Entity:
|
||||
"""Create a sample entity for testing."""
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
"""Tests for file operations service."""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from basic_memory.services.exceptions import FileOperationError
|
||||
from basic_memory.services.file_service import FileService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def file_service():
|
||||
"""Create FileService instance."""
|
||||
return FileService()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_read_file(tmp_path: Path, file_service: FileService):
|
||||
"""Test basic write/read operations with checksums."""
|
||||
@@ -49,9 +44,9 @@ async def test_write_atomic(tmp_path: Path, file_service: FileService):
|
||||
temp_path = test_path.with_suffix(".tmp")
|
||||
|
||||
# Mock write_file_atomic to raise an error
|
||||
with patch('basic_memory.utils.file_utils.write_file_atomic') as mock_write:
|
||||
with patch("basic_memory.utils.file_utils.write_file_atomic") as mock_write:
|
||||
mock_write.side_effect = Exception("Write failed")
|
||||
|
||||
|
||||
# Attempt write that will fail
|
||||
with pytest.raises(FileOperationError):
|
||||
await file_service.write_file(test_path, "test content")
|
||||
@@ -133,7 +128,7 @@ async def test_error_handling_invalid_path(tmp_path: Path, file_service: FileSer
|
||||
# Try to write to a directory instead of file
|
||||
test_path = tmp_path / "test.md"
|
||||
test_path.mkdir() # Create a directory instead of a file
|
||||
|
||||
|
||||
with pytest.raises(FileOperationError):
|
||||
await file_service.write_file(test_path, "test")
|
||||
|
||||
@@ -141,6 +136,7 @@ async def test_error_handling_invalid_path(tmp_path: Path, file_service: FileSer
|
||||
@pytest.mark.asyncio
|
||||
async def test_frontmatter_invalid_metadata(file_service: FileService):
|
||||
"""Test error handling for invalid frontmatter metadata."""
|
||||
|
||||
# Create an object that can't be serialized to YAML
|
||||
class NonSerializable:
|
||||
def __getstate__(self):
|
||||
@@ -149,14 +145,10 @@ async def test_frontmatter_invalid_metadata(file_service: FileService):
|
||||
bad_metadata = {"bad": NonSerializable()}
|
||||
|
||||
# Attempting to add frontmatter with non-serializable content
|
||||
with patch('basic_memory.utils.file_utils.add_frontmatter') as mock_add:
|
||||
with patch("basic_memory.utils.file_utils.add_frontmatter") as mock_add:
|
||||
mock_add.side_effect = FileOperationError("Failed to serialize metadata")
|
||||
with pytest.raises(FileOperationError):
|
||||
await file_service.add_frontmatter(
|
||||
"content",
|
||||
id=123,
|
||||
metadata=bad_metadata
|
||||
)
|
||||
await file_service.add_frontmatter("content", id=123, metadata=bad_metadata)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -175,4 +167,4 @@ async def test_write_unicode_content(tmp_path: Path, file_service: FileService):
|
||||
await file_service.write_file(test_path, test_content)
|
||||
content, _ = await file_service.read_file(test_path)
|
||||
|
||||
assert content == test_content
|
||||
assert content == test_content
|
||||
|
||||
Reference in New Issue
Block a user