finish EnityService

This commit is contained in:
phernandez
2024-12-03 19:47:25 -06:00
parent da4a26a3d1
commit af2eb3ddea
3 changed files with 29 additions and 36 deletions
+13 -8
View File
@@ -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