Skip to content

Commit

Permalink
Add support for delete_one (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowtter committed Dec 15, 2022
1 parent 4086534 commit 8ba93d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mongo_thingy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def find_one(cls, filter=None, *args, **kwargs):
cursor = cls.find(filter, *args, **kwargs)
return cursor.first()

@classmethod
def delete_one(cls, filter=None, *args, **kwargs):
if filter is not None and not isinstance(filter, Mapping):
filter = {"_id": filter}

return cls.collection.delete_one(filter, *args, **kwargs)

@property
def id(self):
return self.__dict__.get("id") or self._id
Expand Down
21 changes: 21 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,27 @@ def test_thingy_find_one(TestThingy, collection):
assert thingy is None


def test_thingy_delete_one(TestThingy, collection):
collection.insert_many(
[{"foo": "bar"}, {"bar": "qux"}, {"bar": "baz"}, {"bar": "baz"}]
)
TestThingy.delete_one({"foo": "bar"})
assert TestThingy.find_one({"foo": "bar"}) is None

qux = TestThingy.find_one({"bar": "qux"})

TestThingy.delete_one(qux.id)
assert TestThingy.find_one({"bar": "qux"}) is None

TestThingy.delete_one(qux.id)
assert TestThingy.find_one({"bar": "qux"}) is None

TestThingy.delete_one({"bar": "baz"})
assert TestThingy.find_one({"bar": "baz"}) is not None
TestThingy.delete_one({"bar": "baz"})
assert TestThingy.find_one({"bar": "baz"}) is None


@pytest.mark.ignore_backends("montydb")
def test_thingy_find_one_and_replace(TestThingy, collection):
collection.insert_many([{"bar": "baz"}, {"bar": "qux"}])
Expand Down

0 comments on commit 8ba93d3

Please sign in to comment.