Skip to content

Commit 4049efc

Browse files
committed
Fix some tests.
1 parent 6570aa2 commit 4049efc

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

django_mongodb/compiler.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django.core.exceptions import EmptyResultSet, FullResultSet
22
from django.db import DatabaseError, IntegrityError, NotSupportedError
33
from django.db.models import Count, Expression
4-
from django.db.models.aggregates import Aggregate
4+
from django.db.models.aggregates import Aggregate, Variance
55
from django.db.models.expressions import Col, OrderBy, Value
6+
from django.db.models.functions.comparison import Coalesce
7+
from django.db.models.functions.math import Power
68
from django.db.models.sql import compiler
79
from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, MULTI, ORDER_DIR, SINGLE
810
from django.utils.functional import cached_property
@@ -45,7 +47,11 @@ def _prepare_expressions_for_pipeline(self, expression, target):
4547
group[alias] = sub_expr.as_mql(self, self.connection)
4648
replacing_expr = inner_column
4749

48-
sub_expr.as_mql(self, self.connection)
50+
if isinstance(sub_expr, Count):
51+
replacing_expr = Coalesce(replacing_expr, 0)
52+
if isinstance(sub_expr, Variance):
53+
replacing_expr = Power(replacing_expr, 2)
54+
4955
replacements[sub_expr] = replacing_expr
5056
return replacements, group
5157

django_mongodb/functions.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,11 @@ def count(self, compiler, connection, **extra_context):
140140
node = self
141141
lhs_mql = process_lhs(self, compiler, connection)
142142
null_cond = {"$in": [{"$type": lhs_mql}, ["missing", "null"]]}
143-
cond = {
144-
"$cond": {"if": null_cond, "then": None, "else": lhs_mql if self.distinct else 1}
145-
}
146-
operator = "$sum"
147-
return {operator: cond}
143+
cond = {"$cond": {"if": null_cond, "then": 0, "else": lhs_mql if self.distinct else 1}}
144+
return {"$sum": cond}
148145

149-
operator = "$size"
150146
lhs_mql = process_lhs(self, compiler, connection)
151-
152147
exits_null = {"$cond": {"if": {"$in": [{"$literal": None}, lhs_mql]}, "then": -1, "else": 0}}
153-
154148
return {"$add": [{"$size": lhs_mql}, exits_null]}
155149

156150

django_mongodb/operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def convert_floatfield_value(self, value, expression, connection):
132132
return None if value is None else float(value)
133133

134134
def convert_integerfield_value(self, value, expression, connection):
135-
return None if value is None else float(value)
135+
return None if value is None else int(value)
136136

137137
def convert_jsonfield_value(self, value, expression, connection):
138138
"""

0 commit comments

Comments
 (0)