Skip to content

Commit

Permalink
Add set method to MockSet (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfantone authored Sep 8, 2024
1 parent 26257c5 commit e7d8c41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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

0 comments on commit e7d8c41

Please sign in to comment.