Skip to content

Commit

Permalink
feat: add delete_by_id to AbstractRepository (#96)
Browse files Browse the repository at this point in the history
* feat: add delete_by_id to AbstractRepository

* fix test for coverage
  • Loading branch information
jochman authored Apr 11, 2024
1 parent 096afaa commit 26a6fbe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pydantic_mongo/abstract_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def save_many(self, models: Iterable[T]):
def delete(self, model: T):
return self.get_collection().delete_one({"_id": model.id})

def delete_by_id(self, _id: Any):
return self.get_collection().delete_one({"_id": _id})

def find_one_by_id(self, _id: Any) -> Optional[T]:
"""
Find entity by id
Expand Down
14 changes: 14 additions & 0 deletions test/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ def test_delete(self, database):
result = spam_repository.find_one_by_id(spam.id)
assert result is None

def test_delete_by_id(self, database):
spam_repository = SpamRepository(database=database)
foo = Foo(count=1, size=1.0)
bar = Bar()
spam = Spam(foo=foo, bars=[bar])
spam_repository.save(spam)

result = spam_repository.find_one_by_id(spam.id)
assert result is not None

spam_repository.delete_by_id(spam.id)
result = spam_repository.find_one_by_id(spam.id)
assert result is None

def test_find_by_id(self, database):
spam_id = ObjectId("611827f2878b88b49ebb69fc")
database.spams.insert_one(
Expand Down

0 comments on commit 26a6fbe

Please sign in to comment.