Skip to content

Commit 471a002

Browse files
committed
test improvement.
1 parent ba1ee02 commit 471a002

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

django_mongodb/compiler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,19 @@ def pre_sql_setup(self, with_col_aliases=False):
7373
# the SELECT clause are already part of the GROUP BY.
7474
if not is_ref:
7575
group_expressions |= set(expr.get_group_by_cols())
76+
77+
for expr, *_ in self.select:
78+
group_expressions |= set(expr.get_group_by_cols())
79+
7680
having_group_by = self.having.get_group_by_cols() if self.having else ()
7781
for expr in having_group_by:
7882
group_expressions.add(expr)
7983
if isinstance(self.query.group_by, tuple | list):
8084
group_expressions |= set(self.query.group_by)
8185

86+
if self.query.group_by is None:
87+
group_expressions = set()
88+
8289
all_strings = "".join(
8390
str(col.as_mql(self, self.connection)) for col in group_expressions
8491
)

django_mongodb/features.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class DatabaseFeatures(BaseDatabaseFeatures):
3838
"expressions.tests.NegatedExpressionTests.test_filter",
3939
"expressions_case.tests.CaseExpressionTests.test_order_by_conditional_implicit",
4040
# NotSupportedError: order_by() expression not supported.
41+
# Lookup in order_by() not supported:
42+
# argument of type '<database function>' is not iterable
43+
"aggregation.tests.AggregateTestCase.test_aggregation_order_by_not_selected_annotation_values",
4144
"db_functions.comparison.test_coalesce.CoalesceTests.test_ordering",
4245
"db_functions.tests.FunctionTests.test_nested_function_ordering",
4346
"db_functions.text.test_length.LengthTests.test_ordering",
@@ -104,6 +107,13 @@ class DatabaseFeatures(BaseDatabaseFeatures):
104107
"queries.test_explain.ExplainUnsupportedTests.test_message",
105108
# filter() on related model + update() doesn't work.
106109
"queries.tests.Queries5Tests.test_ticket9848",
110+
# Variance is not implemented in mongo as an aggregation,
111+
# we should transform it into StdDev * 2.
112+
"aggregation.test_filter_argument.FilteredAggregateTests.test_filtered_numerical_aggregates",
113+
# Sum returns 0 instead of None in mongodb.
114+
"aggregation.test_filter_argument.FilteredAggregateTests.test_plain_annotate",
115+
"aggregation.tests.AggregateTestCase.test_annotation_expressions",
116+
"aggregation.tests.AggregateTestCase.test_reverse_fkey_annotate",
107117
}
108118
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
109119
_django_test_expected_failures_bitwise = {
@@ -279,6 +289,7 @@ def django_test_expected_failures(self):
279289
"timezones.tests.NewDatabaseTests.test_query_annotation",
280290
},
281291
"Exists is not supported on MongoDB.": {
292+
"aggregation.test_filter_argument.FilteredAggregateTests.test_filtered_aggregate_ref_multiple_subquery_annotation",
282293
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_exists_none_query",
283294
"delete_regress.tests.DeleteTests.test_self_reference_with_through_m2m_at_second_level",
284295
"expressions.tests.BasicExpressionsTests.test_annotation_with_deeply_nested_outerref",
@@ -323,6 +334,14 @@ def django_test_expected_failures(self):
323334
"queries.tests.Ticket22429Tests.test_ticket_22429",
324335
},
325336
"Subquery is not supported on MongoDB.": {
337+
"aggregation.tests.AggregateAnnotationPruningTests.test_referenced_composed_subquery_requires_wrapping",
338+
"aggregation.tests.AggregateAnnotationPruningTests.test_referenced_subquery_requires_wrapping",
339+
"aggregation.tests.AggregateTestCase.test_aggregation_nested_subquery_outerref",
340+
"aggregation.tests.AggregateTestCase.test_aggregation_subquery_annotation",
341+
"aggregation.tests.AggregateTestCase.test_aggregation_subquery_annotation_multivalued",
342+
"aggregation.tests.AggregateTestCase.test_aggregation_subquery_annotation_related_field",
343+
"aggregation.tests.AggregateTestCase.test_aggregation_subquery_annotation_values",
344+
"aggregation.tests.AggregateTestCase.test_aggregation_subquery_annotation_values_collision",
326345
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_filter_with_subquery",
327346
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_subquery_outerref_transform",
328347
"annotations.tests.NonAggregateAnnotationTestCase.test_empty_queryset_annotation",
@@ -499,6 +518,7 @@ def django_test_expected_failures(self):
499518
"queries.tests.Queries6Tests.test_col_alias_quoted",
500519
},
501520
"Test executes raw SQL.": {
521+
"aggregation.tests.AggregateTestCase.test_coalesced_empty_result_set",
502522
"annotations.tests.NonAggregateAnnotationTestCase.test_raw_sql_with_inherited_field",
503523
"delete_regress.tests.DeleteLockingTest.test_concurrent_delete",
504524
"expressions.tests.BasicExpressionsTests.test_annotate_values_filter",
@@ -590,6 +610,7 @@ def django_test_expected_failures(self):
590610
"model_fields.test_jsonfield.TestQuerying.test_none_key_exclude",
591611
},
592612
"Randomized ordering isn't supported by MongoDB.": {
613+
"aggregation.tests.AggregateTestCase.test_aggregation_random_ordering",
593614
"ordering.tests.OrderingTests.test_random_ordering",
594615
},
595616
"Queries without a collection aren't supported on MongoDB.": {
@@ -605,6 +626,7 @@ def django_test_expected_failures(self):
605626
"aggregation.tests.AggregateAnnotationPruningTests.test_non_aggregate_annotation_pruned",
606627
"aggregation.tests.AggregateTestCase.test_add_implementation",
607628
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_datetime_from_database",
629+
"aggregation.tests.AggregateTestCase.test_multi_arg_aggregate",
608630
# SQL custom values.
609631
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_date_from_database",
610632
# No sql generate
@@ -616,7 +638,17 @@ def django_test_expected_failures(self):
616638
"aggregation.tests.AggregateAnnotationPruningTests.test_referenced_aggregate_annotation_kept",
617639
# Using a QuerySet in annotate() is not supported on MongoDB
618640
"aggregation.tests.AggregateTestCase.test_group_by_subquery_annotation",
619-
}
641+
# Query set with annotate not supported.
642+
"aggregation.tests.AggregateTestCase.test_group_by_reference_subquery",
643+
"aggregation.tests.AggregateTestCase.test_filter_in_subquery_or_aggregation",
644+
# Exists is not supported in mongodb
645+
"aggregation.tests.AggregateTestCase.test_aggregation_exists_multivalued_outeref",
646+
"aggregation.tests.AggregateTestCase.test_group_by_exists_annotation",
647+
"aggregation.tests.AggregateTestCase.test_exists_none_with_aggregate",
648+
"aggregation.tests.AggregateTestCase.test_exists_extra_where_with_aggregate",
649+
# Queryset dates not supported
650+
"aggregation.tests.AggregateTestCase.test_dates_with_aggregation",
651+
},
620652
}
621653

622654
@cached_property

django_mongodb/functions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def aggregate(self, compiler, connection, **extra_context): # noqa: ARG001
8686
node = self
8787
lhs_mql = process_lhs(node, compiler, connection)
8888
operator = MONGO_AGGREGATION.get(self.__class__)
89+
if self.__class__ in (StdDev, Variance) and "_SAMP" in self.functions:
90+
operator = operator.replace("Pop", "Samp")
8991
return {f"${operator}": lhs_mql}
9092

9193

0 commit comments

Comments
 (0)