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

MockSet.annotate function handle case when row._annotated_fields is None #175

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 @@ -122,7 +122,7 @@ def annotate(self, **kwargs):
results = list(self.items)
for key, value in kwargs.items():
for row in results:
if not hasattr(row, '_annotated_fields'):
if not (hasattr(row, '_annotated_fields') and isinstance(row._annotated_fields, list)):
row._annotated_fields = []
row._annotated_fields.append(key)
setattr(row, key, get_attribute(row, value)[0])
Expand Down
7 changes: 7 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,3 +1356,10 @@ def test_empty_queryset_filter(self):
mockset = MockSet(car1, car2)
self.assertEqual(mockset.count(), 2)
self.assertEqual(mockset.filter(Q()).count(), 2)

def test_mock_set_annotation_by_nested_mock_model(self):
mockset = MockSet(
MockModel(id=1, nested_mock=MockModel(id=1, field1="field_value"))
)
field1 = mockset.annotate(field1=models.F("nested_mock__field1")).values_list("field1")[0][0]
assert field1 == "field_value"