Skip to content

Commit

Permalink
Add proper return value to MockSet delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolás Fantone committed Sep 6, 2024
1 parent 26257c5 commit 49fbfd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
25 changes: 21 additions & 4 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 OrderedDict, defaultdict, namedtuple
from unittest.mock import Mock, MagicMock, PropertyMock

from .constants import *
Expand Down Expand Up @@ -263,16 +263,22 @@ def update(self, **attrs):
return count

def _delete_recursive(self, *items_to_remove, **attrs):
removed_items = defaultdict(int)

for item in matches(*items_to_remove, **attrs):
self.items.remove(item)
self.fire(item, self.EVENT_DELETED)
item_label = item._meta.label if hasattr(item, '_meta') else type(item).__name__
removed_items[item_label] += 1

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

return sum(removed_items.values()), removed_items

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 Expand Up @@ -433,7 +439,8 @@ def __init__(self, *args, **kwargs):
super(MockModel, self).__init__(*args, **kwargs)

self.save = PropertyMock()
self.__meta = MockOptions(*self.get_fields())
object_name = self.get('mock_name', type(self).__name__)
self.__meta = MockOptions(object_name, *self.get_fields())

def __getattr__(self, item):
return self.get(item, None)
Expand Down Expand Up @@ -467,9 +474,19 @@ def create_model(*fields):


class MockOptions:
def __init__(self, *field_names):
def __init__(self, object_name, *field_names):
self.load_fields(*field_names)
self.get_latest_by = None
self.object_name = object_name
self.model_name = object_name.lower()

@property
def label(self):
return self.object_name

@property
def label_lower(self):
return self.model_name

def load_fields(self, *field_names):
fields = {name: MockField(name) for name in field_names}
Expand Down
8 changes: 6 additions & 2 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,20 +706,24 @@ def test_query_delete_all_entries(self):
item_2 = MockModel(foo=1, bar='b', mock_name='item_2')

self.mock_set.add(item_1, item_2)
self.mock_set.delete()
deleted_count, deleted_items = self.mock_set.delete()

assert len(self.mock_set) == 0, len(self.mock_set)
assert deleted_count == 2
assert deleted_items == {'item_1': 1, 'item_2': 1}

def test_query_delete_entries_propagated_from_nested_qs(self):
item_1 = MockModel(foo=1, bar='a', mock_name='item_1')
item_2 = MockModel(foo=1, bar='b', mock_name='item_2')

self.mock_set.add(item_1, item_2)
self.mock_set.filter(bar='b').delete()
deleted_count, deleted_items = self.mock_set.filter(bar='b').delete()

assert len(self.mock_set) == 1, len(self.mock_set)
assert item_1 in self.mock_set
assert item_2 not in self.mock_set
assert deleted_count == 1
assert deleted_items == {'item_2': 1}

def test_query_gets_unique_match_by_attrs_from_set(self):
item_1 = MockModel(foo=1)
Expand Down

0 comments on commit 49fbfd5

Please sign in to comment.