From 8e082fe2ae2094ce211ff876ec68a63b40839587 Mon Sep 17 00:00:00 2001 From: dvirh Date: Wed, 26 Jun 2024 06:44:02 +0300 Subject: [PATCH 1/5] MockSet.annotate function handle case when row._annotated_fields is None --- django_mock_queries/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_mock_queries/query.py b/django_mock_queries/query.py index 3379636..ca39759 100644 --- a/django_mock_queries/query.py +++ b/django_mock_queries/query.py @@ -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]) From 5f6a09257b7a58a674a839246398228099107c2e Mon Sep 17 00:00:00 2001 From: dvirh Date: Wed, 26 Jun 2024 16:13:40 +0300 Subject: [PATCH 2/5] added test_mock_model_annotation_by_nested_field test --- tests/test_query.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_query.py b/tests/test_query.py index 95514d6..b8582a4 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1356,3 +1356,11 @@ 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_model_annotation_by_nested_field(): + 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" \ No newline at end of file From 0435ca3038ddce643a58b9865df98c8432bbf7af Mon Sep 17 00:00:00 2001 From: dvirh Date: Wed, 26 Jun 2024 16:17:03 +0300 Subject: [PATCH 3/5] added missing self argument --- tests/test_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_query.py b/tests/test_query.py index b8582a4..2fcc144 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1357,7 +1357,7 @@ def test_empty_queryset_filter(self): self.assertEqual(mockset.count(), 2) self.assertEqual(mockset.filter(Q()).count(), 2) - def test_mock_model_annotation_by_nested_field(): + def test_mock_model_annotation_by_nested_field(self): mockset = MockSet( MockModel(id=1, nested_mock=MockModel(id=1, field1="field_value")) ) From bf02f5d64b70aa3d6ac5d0c0cc3df96292105df5 Mon Sep 17 00:00:00 2001 From: dvirh Date: Wed, 26 Jun 2024 16:18:07 +0300 Subject: [PATCH 4/5] better test function name --- tests/test_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_query.py b/tests/test_query.py index 2fcc144..23f203c 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1357,7 +1357,7 @@ def test_empty_queryset_filter(self): self.assertEqual(mockset.count(), 2) self.assertEqual(mockset.filter(Q()).count(), 2) - def test_mock_model_annotation_by_nested_field(self): + def test_mock_set_annotation_by_nested_mock_model(self): mockset = MockSet( MockModel(id=1, nested_mock=MockModel(id=1, field1="field_value")) ) From 5bfc9a131ac22394a3abe8e758004dcb12494331 Mon Sep 17 00:00:00 2001 From: dvirh Date: Wed, 26 Jun 2024 16:24:34 +0300 Subject: [PATCH 5/5] formatting --- tests/test_query.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_query.py b/tests/test_query.py index 23f203c..a5bfc71 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -1356,11 +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" \ No newline at end of file + assert field1 == "field_value"