From aa61ed0d967444dcb77f66d36cc46fd99b396b9d Mon Sep 17 00:00:00 2001 From: phernandez Date: Fri, 27 Dec 2024 20:06:17 -0600 Subject: [PATCH] fix imports for sync --- src/basic_memory/cli/commands/status.py | 2 +- .../services/sync/document_sync_service.py | 2 +- .../services/sync/file_change_scanner.py | 2 +- .../services/sync/knowledge_sync_service.py | 2 +- src/basic_memory/services/{ => sync}/utils.py | 0 tests/cli/test_status.py | 88 +++++++------------ tests/conftest.py | 2 +- 7 files changed, 38 insertions(+), 60 deletions(-) rename src/basic_memory/services/{ => sync}/utils.py (100%) diff --git a/src/basic_memory/cli/commands/status.py b/src/basic_memory/cli/commands/status.py index 90e9401e..bb4a026b 100644 --- a/src/basic_memory/cli/commands/status.py +++ b/src/basic_memory/cli/commands/status.py @@ -15,7 +15,7 @@ from basic_memory.config import config from basic_memory.db import DatabaseType from basic_memory.repository import DocumentRepository, EntityRepository from basic_memory.services import FileChangeScanner -from basic_memory.services.utils import SyncReport, FileState +from basic_memory.services.sync.utils import SyncReport, FileState # Create rich console console = Console() diff --git a/src/basic_memory/services/sync/document_sync_service.py b/src/basic_memory/services/sync/document_sync_service.py index abded6b6..8f7524d2 100644 --- a/src/basic_memory/services/sync/document_sync_service.py +++ b/src/basic_memory/services/sync/document_sync_service.py @@ -6,7 +6,7 @@ from loguru import logger from basic_memory.services import DocumentService from basic_memory.services import FileChangeScanner -from basic_memory.services.utils import SyncReport +from basic_memory.services.sync. utils import SyncReport class DocumentSyncService: diff --git a/src/basic_memory/services/sync/file_change_scanner.py b/src/basic_memory/services/sync/file_change_scanner.py index af9e64fd..335e6d02 100644 --- a/src/basic_memory/services/sync/file_change_scanner.py +++ b/src/basic_memory/services/sync/file_change_scanner.py @@ -7,7 +7,7 @@ from loguru import logger from basic_memory.repository.document_repository import DocumentRepository from basic_memory.repository.entity_repository import EntityRepository -from basic_memory.services.utils import FileState, SyncReport +from basic_memory.services.sync.utils import FileState, SyncReport from basic_memory.utils.file_utils import compute_checksum diff --git a/src/basic_memory/services/sync/knowledge_sync_service.py b/src/basic_memory/services/sync/knowledge_sync_service.py index fa96cb14..21df534f 100644 --- a/src/basic_memory/services/sync/knowledge_sync_service.py +++ b/src/basic_memory/services/sync/knowledge_sync_service.py @@ -2,7 +2,7 @@ from pathlib import Path from basic_memory.markdown import KnowledgeParser from basic_memory.services import FileChangeScanner, KnowledgeService -from basic_memory.services.utils import FileState, SyncReport +from basic_memory.services.sync.utils import FileState, SyncReport class KnowledgeSyncService: diff --git a/src/basic_memory/services/utils.py b/src/basic_memory/services/sync/utils.py similarity index 100% rename from src/basic_memory/services/utils.py rename to src/basic_memory/services/sync/utils.py diff --git a/tests/cli/test_status.py b/tests/cli/test_status.py index e1ca69d9..87f8cfe7 100644 --- a/tests/cli/test_status.py +++ b/tests/cli/test_status.py @@ -1,12 +1,11 @@ """Test status command functionality.""" -from pathlib import Path import pytest from rich.console import Console from io import StringIO from basic_memory.cli.commands.status import display_changes, run_status -from basic_memory.services.file_sync_service import SyncReport, FileState +from basic_memory.services.sync.utils import SyncReport, FileState from basic_memory.utils.file_utils import compute_checksum @@ -34,15 +33,13 @@ async def test_display_compact_changes(console): deleted={"old/deleted.md"}, moved={ "new/location.md": FileState( - path="new/location.md", - checksum="abc123", - moved_from="old/location.md" + path="new/location.md", checksum="abc123", moved_from="old/location.md" ) - } + }, ) display_changes("Test Files", changes, verbose=False) output_text = output.getvalue() - + # Check directory summaries assert "docs/ +1 new" in output_text.replace(" ", " ") assert "docs/ ~1 modified" in output_text.replace(" ", " ") @@ -61,23 +58,23 @@ async def test_display_verbose_changes(console): "new/location.md": FileState( path="new/location.md", checksum="abc123def", # 8 chars for display - moved_from="old/location.md" + moved_from="old/location.md", ) }, checksums={ "docs/new.md": "def456789abcdef", "docs/mod.md": "ghi789abcdef123", - } + }, ) display_changes("Test Files", changes, verbose=True) output_text = output.getvalue() - + # Verify sections assert "New Files" in output_text assert "Modified" in output_text assert "Deleted" in output_text assert "Moved" in output_text - + # Check file listings with checksums assert "new.md (def45678)" in output_text assert "mod.md (ghi78789)" in output_text @@ -85,46 +82,41 @@ async def test_display_verbose_changes(console): async def test_end_to_end_status( - file_change_scanner, - test_config, - document_repository, - entity_repository + file_change_scanner, test_config, document_repository, entity_repository ): """Test complete status command with real files.""" # Create test files in both knowledge and documents directories docs_dir = test_config.documents_dir knowledge_dir = test_config.knowledge_dir - + # Create some test files docs_dir.mkdir(parents=True, exist_ok=True) knowledge_dir.mkdir(parents=True, exist_ok=True) - + # Create documents doc_path = docs_dir / "test.md" doc_path.write_text("test document") doc_checksum = await compute_checksum("test document") - + nested_dir = docs_dir / "subdir" nested_dir.mkdir(exist_ok=True) nested_path = nested_dir / "nested.md" nested_path.write_text("nested document") - + # Create knowledge files component_dir = knowledge_dir / "component" component_dir.mkdir(exist_ok=True) component_path = component_dir / "test.md" component_path.write_text("test component") - + # Add some files to DB with different paths to test moves - await document_repository.create({ - "path_id": "old/doc.md", - "file_path": "old/doc.md", - "checksum": "abc123" - }) + await document_repository.create( + {"path_id": "old/doc.md", "file_path": "old/doc.md", "checksum": "abc123"} + ) # Run status check await run_status(file_change_scanner, verbose=True) - + # Verify changes through sync service directly doc_changes = await file_change_scanner.find_document_changes(docs_dir) assert len(doc_changes.new) == 2 # test.md and nested.md @@ -137,32 +129,26 @@ async def test_end_to_end_status( assert "component/test.md" in knowledge_changes.new -async def test_status_with_case_changes( - file_change_scanner, - test_config, - document_repository -): +async def test_status_with_case_changes(file_change_scanner, test_config, document_repository): """Test status detection with case-sensitive path changes.""" docs_dir = test_config.documents_dir docs_dir.mkdir(parents=True, exist_ok=True) - + # Create file with initial case content = "test content" original_path = "Test.md" orig_file = docs_dir / original_path orig_file.write_text(content) checksum = await compute_checksum(content) - + # Add to DB - await document_repository.create({ - "path_id": original_path, - "file_path": original_path, - "checksum": checksum - }) - + await document_repository.create( + {"path_id": original_path, "file_path": original_path, "checksum": checksum} + ) + # Simulate case change in filesystem orig_file.rename(docs_dir / "test.md") - + # Check changes changes = await file_change_scanner.find_document_changes(docs_dir) assert len(changes.moved) == 1 @@ -170,32 +156,24 @@ async def test_status_with_case_changes( assert changes.moved["test.md"].moved_from == original_path -async def test_status_with_spaces( - file_change_scanner, - test_config, - document_repository -): +async def test_status_with_spaces(file_change_scanner, test_config, document_repository): """Test status handling files with spaces and special characters.""" docs_dir = test_config.documents_dir docs_dir.mkdir(parents=True, exist_ok=True) - + # Create file with spaces content = "test content" path = "My Document.md" file_path = docs_dir / path file_path.write_text(content) checksum = await compute_checksum(content) - + # Add to DB with same path - await document_repository.create({ - "path_id": path, - "file_path": path, - "checksum": checksum - }) - + await document_repository.create({"path_id": path, "file_path": path, "checksum": checksum}) + # Check changes changes = await file_change_scanner.find_document_changes(docs_dir) assert not changes.modified # File unchanged - assert not changes.moved # Path matches exactly + assert not changes.moved # Path matches exactly assert not changes.deleted - assert not changes.new \ No newline at end of file + assert not changes.new diff --git a/tests/conftest.py b/tests/conftest.py index e2d6c658..7d62cb65 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -147,7 +147,7 @@ def knowledge_parser(): @pytest_asyncio.fixture -def file_sync_service(document_repository, entity_repository) -> FileChangeScanner: +def file_change_scanner(document_repository, entity_repository) -> FileChangeScanner: """Create FileChangeScanner instance.""" return FileChangeScanner(document_repository, entity_repository)