mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-04-15 22:26:25 +02:00
fix: suppress UPDATE callbacks for objects deleted in the same transaction (#205)
This commit is contained in:
@@ -231,6 +231,13 @@ class EventSession(AsyncSession):
|
||||
k: v for k, v in field_changes.items() if k not in transient_ids
|
||||
}
|
||||
|
||||
# Suppress updates for deleted objects (row is gone, refresh would fail).
|
||||
if deletes and field_changes:
|
||||
deleted_ids = {id(o) for o, _ in deletes}
|
||||
field_changes = {
|
||||
k: v for k, v in field_changes.items() if k not in deleted_ids
|
||||
}
|
||||
|
||||
# Suppress updates for newly created objects (CREATE-only semantics).
|
||||
if creates and field_changes:
|
||||
create_ids = {id(o) for o in creates}
|
||||
|
||||
@@ -1041,6 +1041,25 @@ class TestTransientObject:
|
||||
assert len(creates) == 1
|
||||
assert len(deletes) == 1
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_update_then_delete_suppresses_update_callback(self, mixin_session):
|
||||
"""UPDATE callback is suppressed when the object is also deleted in the same transaction."""
|
||||
obj = WatchedModel(status="initial", other="x")
|
||||
mixin_session.add(obj)
|
||||
await mixin_session.commit()
|
||||
|
||||
_test_events.clear()
|
||||
|
||||
obj.status = "changed"
|
||||
await mixin_session.flush()
|
||||
await mixin_session.delete(obj)
|
||||
await mixin_session.commit()
|
||||
|
||||
updates = [e for e in _test_events if e["event"] == "update"]
|
||||
deletes = [e for e in _test_events if e["event"] == "delete"]
|
||||
assert updates == []
|
||||
assert len(deletes) == 1
|
||||
|
||||
|
||||
class TestPolymorphism:
|
||||
"""Event dispatch with STI (Single Table Inheritance)."""
|
||||
|
||||
Reference in New Issue
Block a user