Skip to content

Make annotate return instance of actual MockSet class #179

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

Merged
merged 2 commits into from
Sep 6, 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
2 changes: 1 addition & 1 deletion django_mock_queries/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def annotate(self, **kwargs):
row._annotated_fields.append(key)
setattr(row, key, get_attribute(row, value)[0])

return MockSet(*results, clone=self)
return self._mockset_class()(*results, clone=self)

def aggregate(self, *args, **kwargs):
result = {}
Expand Down
7 changes: 7 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,13 @@ def test_annotate(self):

self.assertEqual(qs[2].color_or_car, 'kia')

def test_annotate_returns_current_class_instance(self):
class CustomMockSet(MockSet):
pass

qs = CustomMockSet(Car(model='golf', id=1))
self.assertIsInstance(qs.annotate(model=models.F('model')), CustomMockSet)

def test_query_values_raises_attribute_error_when_field_is_not_in_meta_concrete_fields(self):
qs = MockSet(MockModel(foo=1), MockModel(foo=2))
self.assertRaises(FieldError, qs.values, 'bar')
Expand Down