feat(trash): services/trash.py — delete() + cascade-stamp by batch

This commit is contained in:
2026-05-28 20:01:35 -04:00
parent 7f0d99d383
commit ce47ebc7de
2 changed files with 175 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
"""Tests for services/trash.py — mocks async_session (no real DB)."""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
def _make_mock_session():
s = AsyncMock()
s.__aenter__ = AsyncMock(return_value=s)
s.__aexit__ = AsyncMock(return_value=False)
s.commit = AsyncMock()
return s
def _exists_result(found=True):
r = MagicMock()
r.first.return_value = (1,) if found else None
return r
@pytest.mark.asyncio
async def test_delete_note_returns_batch_and_commits():
session = _make_mock_session()
# 1 execute for _exists_alive, then 2 for the note cascade (sub-tasks + self)
session.execute = AsyncMock(side_effect=[_exists_result(True), MagicMock(), MagicMock()])
with patch("fabledassistant.services.trash.async_session") as cls:
cls.return_value = session
from fabledassistant.services.trash import delete
batch = await delete(user_id=1, entity_type="note", entity_id=5)
assert isinstance(batch, str) and len(batch) > 0
assert session.commit.called
# exists-check + 2 cascade updates
assert session.execute.await_count == 3
@pytest.mark.asyncio
async def test_delete_returns_none_when_not_found():
session = _make_mock_session()
session.execute = AsyncMock(return_value=_exists_result(False))
with patch("fabledassistant.services.trash.async_session") as cls:
cls.return_value = session
from fabledassistant.services.trash import delete
batch = await delete(user_id=1, entity_type="task", entity_id=999)
assert batch is None
assert not session.commit.called
# only the existence check ran
assert session.execute.await_count == 1
@pytest.mark.asyncio
async def test_delete_project_cascades_three_tables():
session = _make_mock_session()
# exists-check + 3 cascade updates (notes, milestones, project)
session.execute = AsyncMock(side_effect=[
_exists_result(True), MagicMock(), MagicMock(), MagicMock(),
])
with patch("fabledassistant.services.trash.async_session") as cls:
cls.return_value = session
from fabledassistant.services.trash import delete
batch = await delete(user_id=1, entity_type="project", entity_id=3)
assert isinstance(batch, str)
assert session.execute.await_count == 4
@pytest.mark.asyncio
async def test_delete_rulebook_cascades_topics_and_rules():
session = _make_mock_session()
topic_ids_result = MagicMock()
topic_ids_result.scalars.return_value.all.return_value = [10, 11]
# exists-check, topic-id select, then 3 updates (rules, topics, rulebook)
session.execute = AsyncMock(side_effect=[
_exists_result(True), topic_ids_result, MagicMock(), MagicMock(), MagicMock(),
])
with patch("fabledassistant.services.trash.async_session") as cls:
cls.return_value = session
from fabledassistant.services.trash import delete
batch = await delete(user_id=7, entity_type="rulebook", entity_id=2)
assert isinstance(batch, str)
assert session.execute.await_count == 5