Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: soft delete #901

Merged
merged 7 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix .find_many_in_all()
add more test
  • Loading branch information
alm0ra committed Apr 19, 2024
commit 694ec28fb80d82d1b20b293c9f68fb13c3810b6b
5 changes: 2 additions & 3 deletions beanie/odm/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,8 +1248,8 @@ def find_many_in_all( # type: ignore
nesting_depth: Optional[int] = None,
nesting_depths_per_field: Optional[Dict[str, int]] = None,
**pymongo_kwargs,
) -> FindMany[FindType]:
return Document.find_many(
) -> Union[FindMany[FindType], FindMany["DocumentProjectionType"]]:
return cls._find_many_query_class(document_model=cls).find_many(
*args,
sort=sort,
skip=skip,
Expand All @@ -1259,7 +1259,6 @@ def find_many_in_all( # type: ignore
ignore_cache=ignore_cache,
fetch_links=fetch_links,
lazy_parse=lazy_parse,
with_children=with_children,
nesting_depth=nesting_depth,
nesting_depths_per_field=nesting_depths_per_field,
**pymongo_kwargs,
Expand Down
73 changes: 71 additions & 2 deletions tests/odm/documents/test_soft_delete.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from tests.odm.models import DocumentTestModelWithSoftDelete


async def test_insert_one_and_delete_one(document_soft_delete_not_inserted):
async def test_get_item(document_soft_delete_not_inserted):
# insert a document with soft delete
result = await document_soft_delete_not_inserted.insert()

Expand All @@ -17,12 +17,59 @@ async def test_insert_one_and_delete_one(document_soft_delete_not_inserted):
await document.delete()
assert document.is_deleted() is True

# check if document exist
# check if document exist with `.get()`
document = await DocumentTestModelWithSoftDelete.get(document_id=result.id)
assert document is None

# check document exist in trashed
results = (
await DocumentTestModelWithSoftDelete.find_many_in_all().to_list()
)
assert len(results) == 1


async def test_find_one(document_soft_delete_not_inserted):
result = await document_soft_delete_not_inserted.insert()

# # delete the document
await result.delete()

# check if document exist with `.find_one()`
document = await DocumentTestModelWithSoftDelete.find_one(
DocumentTestModelWithSoftDelete.id == result.id
)
assert document is None


async def test_find(documents_soft_delete_not_inserted):
# insert 3 documents
inserted_docs = []
for doc in documents_soft_delete_not_inserted:
result = await doc.insert()
inserted_docs.append(result)

# use `.find_many()` to get them all
results = await DocumentTestModelWithSoftDelete.find().to_list()
assert len(results) == 3

# delete one of them
await inserted_docs[0].delete()

# check items in with `.find_many()`
results = await DocumentTestModelWithSoftDelete.find_many().to_list()

assert len(results) == 2

founded_documents_id = [doc.id for doc in results]
assert inserted_docs[0].id not in founded_documents_id

# check in trashed items
results = (
await DocumentTestModelWithSoftDelete.find_many_in_all().to_list()
)
assert len(results) == 3


async def test_find_many(documents_soft_delete_not_inserted):
# insert 2 documents
item_1 = await documents_soft_delete_not_inserted[0].insert()
Expand All @@ -35,7 +82,29 @@ async def test_find_many(documents_soft_delete_not_inserted):
# delete one of them
await item_1.delete()

# check items in with `.find_many()`
results = await DocumentTestModelWithSoftDelete.find_many().to_list()

assert len(results) == 1
assert results[0].id == item_2.id

# check in trashed items
results = (
await DocumentTestModelWithSoftDelete.find_many_in_all().to_list()
)
assert len(results) == 2


async def test_hard_delete(document_soft_delete_not_inserted):
result = await document_soft_delete_not_inserted.insert()
await result.hard_delete()

# check items in with `.find_many()`
results = await DocumentTestModelWithSoftDelete.find_many().to_list()
assert len(results) == 0

# check in trashed
results = (
await DocumentTestModelWithSoftDelete.find_many_in_all().to_list()
)
assert len(results) == 0
Loading