mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
finish EnityService
This commit is contained in:
@@ -37,7 +37,12 @@ async def session(engine):
|
||||
"""Create an async session factory and yield a session"""
|
||||
async_session = async_sessionmaker(engine, expire_on_commit=False)
|
||||
async with async_session() as session:
|
||||
yield session
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def entity_repo(session):
|
||||
@@ -136,12 +141,12 @@ async def test_get_entity_not_found(entity_service):
|
||||
async def test_create_entity_db_error(entity_service, monkeypatch):
|
||||
"""Test handling of database errors during creation."""
|
||||
# Arrange - make db operations fail
|
||||
async def mock_db_fail(*args, **kwargs):
|
||||
raise DatabaseSyncError("Mock DB error")
|
||||
monkeypatch.setattr(entity_service, "_update_db_index", mock_db_fail)
|
||||
async def mock_create(*args, **kwargs):
|
||||
raise Exception("Mock DB error")
|
||||
monkeypatch.setattr(entity_service.entity_repo, "create", mock_create)
|
||||
|
||||
# Act
|
||||
with pytest.raises(DatabaseSyncError):
|
||||
# Act/Assert - both file and DB operations should fail
|
||||
with pytest.raises(Exception, match="Mock DB error"):
|
||||
await entity_service.create_entity(
|
||||
name="Test Entity",
|
||||
entity_type="test",
|
||||
@@ -150,8 +155,8 @@ async def test_create_entity_db_error(entity_service, monkeypatch):
|
||||
|
||||
async def test_delete_nonexistent_entity(entity_service):
|
||||
"""Test deleting an entity that doesn't exist."""
|
||||
result = await entity_service.delete_entity("nonexistent-id")
|
||||
assert result is True # Should succeed silently
|
||||
await entity_service.delete_entity("nonexistent-id")
|
||||
# If we get here, the deletion succeeded or failed silently as expected
|
||||
|
||||
# Edge Cases
|
||||
|
||||
|
||||
Reference in New Issue
Block a user