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

Add return value to MockSet delete #154

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
11 changes: 9 additions & 2 deletions django_mock_queries/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import random
from collections import OrderedDict, namedtuple
from collections import defaultdict, OrderedDict, namedtuple
from six import with_metaclass
try:
from unittest.mock import Mock, MagicMock, PropertyMock
Expand Down Expand Up @@ -266,16 +266,23 @@ def update(self, **attrs):
return count

def _delete_recursive(self, *items_to_remove, **attrs):
sum_deleted = 0
items_deleted = defaultdict(int)

for item in matches(*items_to_remove, **attrs):
self.items.remove(item)
self.fire(item, self.EVENT_DELETED)
sum_deleted += 1
items_deleted[item._meta.label] += 1

if self.clone is not None:
self.clone._delete_recursive(*items_to_remove, **attrs)

return sum_deleted, items_deleted
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need to store a sum_deleted variable.
You can just use return sum(items_delete.values()), items_deleted


def delete(self, **attrs):
# Delete normally doesn't take **attrs - they're only needed for remove
self._delete_recursive(*self.items, **attrs)
return self._delete_recursive(*self.items, **attrs)

# The following 2 methods were kept for backwards compatibility and
# should be removed in the future since they are covered by filter & delete
Expand Down