Skip to content

Commit

Permalink
Implement Thingy.update_many and Thingy.delete_many
Browse files Browse the repository at this point in the history
  • Loading branch information
ramnes committed Dec 21, 2022
1 parent 22af357 commit b3b8eca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mongo_thingy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,21 @@ def find_one(cls, filter=None, *args, **kwargs):
cursor = cls.find(filter, *args, **kwargs)
return cursor.first()

@classmethod
def delete_many(cls, filter=None, *args, **kwargs):
return cls.collection.delete_many(filter, *args, **kwargs)

@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)

@classmethod
def update_many(cls, filter, update, *args, **kwargs):
return cls.collection.update_many(filter, update, *args, **kwargs)

@classmethod
def update_one(cls, filter, update, *args, **kwargs):
if filter is not None and not isinstance(filter, Mapping):
Expand Down
50 changes: 50 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ def test_thingy_find_one(TestThingy, collection):
assert thingy is None


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

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

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


def test_thingy_delete_one(TestThingy, collection):
collection.insert_many(
[{"foo": "bar"}, {"bar": "qux"}, {"bar": "baz"}, {"bar": "baz"}]
Expand All @@ -349,6 +362,43 @@ def test_thingy_delete_one(TestThingy, collection):
assert TestThingy.find_one({"bar": "baz"}) is None


def test_thingy_update_many(TestThingy, collection):
collection.insert_many(
[{"foo": "bar"}, {"bar": "qux"}, {"bar": "baz"}, {"bar": "baz"}]
)
updated = TestThingy.update_many({"bar": "baz"}, {"$set": {"bar": "baaz"}})
assert updated.acknowledged
assert updated.matched_count == 2
assert updated.modified_count == 2
assert TestThingy.find_one({"bar": "baz"}) is None

updated = TestThingy.update_many(
{"new": "new"}, {"$set": {"bar": "baaz"}}, upsert=False
)
assert updated.acknowledged
assert updated.matched_count == 0
assert updated.modified_count == 0
assert TestThingy.find_one({"new": "new"}) is None

updated = TestThingy.update_many(
{"new": "new"}, {"$set": {"bar": "baaz"}}, upsert=True
)
assert updated.acknowledged
assert updated.matched_count == 0
assert updated.modified_count == 0
assert TestThingy.find_one({"new": "new"}).bar == "baaz"

updated = TestThingy.update_many(
{"new": "new"}, {"$set": {"already": "exists"}}, upsert=True
)
assert updated.acknowledged
assert updated.matched_count == 1
assert updated.modified_count == 1
new = TestThingy.find_one({"new": "new"})
assert new.already == "exists"
assert new.bar == "baaz"


def test_thingy_update_one(TestThingy, collection):
collection.insert_many(
[{"foo": "bar"}, {"bar": "qux"}, {"bar": "baz"}, {"bar": "baz"}]
Expand Down

0 comments on commit b3b8eca

Please sign in to comment.