Skip to content

Commit 98cd049

Browse files
committed
fixup! ⚡️(back) restrict documents to restore using only the queryset
1 parent 724130e commit 98cd049

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/backend/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def restore(self):
802802
.first()
803803
)
804804
self.ancestors_deleted_at = ancestors_deleted_at
805-
self.save()
805+
self.save(update_fields=["deleted_at", "ancestors_deleted_at"])
806806

807807
self.get_descendants().exclude(
808808
models.Q(deleted_at__isnull=False)

src/backend/core/tests/test_models_documents.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -767,21 +767,22 @@ def test_models_documents_nb_accesses_cache_is_invalidated_on_access_removal(
767767
assert cache.get(key) == 0 # Cache should now contain the new value
768768

769769

770-
def test_models_documents_restore():
770+
def test_models_documents_restore(django_assert_num_queries):
771771
"""The restore method should restore a soft-deleted document."""
772772
document = factories.DocumentFactory()
773773
document.soft_delete()
774774
document.refresh_from_db()
775775
assert document.deleted_at is not None
776776
assert document.ancestors_deleted_at == document.deleted_at
777777

778-
document.restore()
778+
with django_assert_num_queries(6):
779+
document.restore()
779780
document.refresh_from_db()
780781
assert document.deleted_at is None
781782
assert document.ancestors_deleted_at == document.deleted_at
782783

783784

784-
def test_models_documents_restore_complex():
785+
def test_models_documents_restore_complex(django_assert_num_queries):
785786
"""The restore method should restore a soft-deleted document and its ancestors."""
786787
grand_parent = factories.DocumentFactory()
787788
parent = factories.DocumentFactory(parent=grand_parent)
@@ -817,7 +818,8 @@ def test_models_documents_restore_complex():
817818
assert child2.ancestors_deleted_at == document.deleted_at
818819

819820
# Restore the item
820-
document.restore()
821+
with django_assert_num_queries(8):
822+
document.restore()
821823
document.refresh_from_db()
822824
child1.refresh_from_db()
823825
child2.refresh_from_db()
@@ -829,7 +831,7 @@ def test_models_documents_restore_complex():
829831
assert child2.ancestors_deleted_at == grand_parent.deleted_at
830832

831833

832-
def test_models_documents_restore_complex_bis():
834+
def test_models_documents_restore_complex_bis(django_assert_num_queries):
833835
"""The restore method should restore a soft-deleted item and its ancestors."""
834836
grand_parent = factories.DocumentFactory()
835837
parent = factories.DocumentFactory(parent=grand_parent)
@@ -866,7 +868,9 @@ def test_models_documents_restore_complex_bis():
866868

867869
# Restoring the grand parent should not restore the document
868870
# as it was deleted before the grand parent
869-
grand_parent.restore()
871+
with django_assert_num_queries(7):
872+
grand_parent.restore()
873+
870874
grand_parent.refresh_from_db()
871875
parent.refresh_from_db()
872876
document.refresh_from_db()

0 commit comments

Comments
 (0)