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 set method to MockSet #185

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions django_mock_queries/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def distinct(self, *fields):
results[key] = item
return self._mockset_class()(*results.values(), clone=self)

def set(self, objs, **attrs):
self.delete(**attrs)
self.add(*objs)

def _raise_does_not_exist(self):
does_not_exist = getattr(self.model, 'DoesNotExist', ObjectDoesNotExist)
raise does_not_exist()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,3 +1383,13 @@ def test_mock_set_annotation_by_nested_mock_model(self):
)
field1 = mockset.annotate(field1=models.F("nested_mock__field1")).values_list("field1")[0][0]
assert field1 == "field_value"

def test_set_replaces_all_items(self):
mockset = MockSet(
MockModel(id=1, field="value_1", mock_name="item1"),
MockModel(id=2, field="value_2", mock_name="item2"),
)
mockset.set([MockModel(id=3, field="value_3", mock_name="item3")])

assert len(mockset) == 1
assert mockset[0].id == 3