Skip to content

Commit ae8b2a8

Browse files
committed
make less than lookups exclude null values
1 parent b4c24e4 commit ae8b2a8

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

django_mongodb/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ def _isnull_operator(a, b):
100100
"exact": lambda a, b: {"$eq": [a, b]},
101101
"gt": lambda a, b: {"$gt": [a, b]},
102102
"gte": lambda a, b: {"$gte": [a, b]},
103-
"lt": lambda a, b: {"$lt": [a, b]},
104-
"lte": lambda a, b: {"$lte": [a, b]},
103+
# MongoDB considers null less than zero. Exclude null values to match
104+
# SQL behavior.
105+
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, {"$ne": [a, None]}]},
106+
"lte": lambda a, b: {"$and": [{"$lte": [a, b]}, {"$ne": [a, None]}]},
105107
"in": lambda a, b: {"$in": [a, b]},
106108
"isnull": _isnull_operator,
107109
"range": lambda a, b: {

django_mongodb/features.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6262
# the result back to UTC.
6363
"db_functions.datetime.test_extract_trunc.DateFunctionWithTimeZoneTests.test_trunc_func_with_timezone",
6464
"db_functions.datetime.test_extract_trunc.DateFunctionWithTimeZoneTests.test_trunc_timezone_applied_before_truncation",
65-
# Length of null considered zero rather than null.
66-
"db_functions.text.test_length.LengthTests.test_basic",
6765
# Unexpected alias_refcount in alias_map.
6866
"queries.tests.Queries1Tests.test_order_by_tables",
6967
# The $sum aggregation returns 0 instead of None for null.

tests/lookup_/__init__.py

Whitespace-only changes.

tests/lookup_/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.db import models
2+
3+
4+
class Number(models.Model):
5+
num = models.IntegerField(blank=True, null=True)
6+
7+
class Meta:
8+
ordering = ("num",)
9+
10+
def __str__(self):
11+
return str(self.num)

tests/lookup_/tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.test import TestCase
2+
3+
from .models import Number
4+
5+
6+
class NumericLookupTests(TestCase):
7+
@classmethod
8+
def setUpTestData(cls):
9+
cls.objs = Number.objects.bulk_create(Number(num=x) for x in range(5))
10+
# Null values should be excluded in less than queries.
11+
Number.objects.create()
12+
13+
def test_lt(self):
14+
self.assertQuerySetEqual(Number.objects.filter(num__lt=3), self.objs[:3])
15+
16+
def test_lte(self):
17+
self.assertQuerySetEqual(Number.objects.filter(num__lte=3), self.objs[:4])

0 commit comments

Comments
 (0)