Skip to content

Commit

Permalink
make less than lookups exclude null values
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 3, 2025
1 parent a40e0ae commit c191e50
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
6 changes: 4 additions & 2 deletions django_mongodb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ def _isnull_operator(a, b):
"exact": lambda a, b: {"$eq": [a, b]},
"gt": lambda a, b: {"$gt": [a, b]},
"gte": lambda a, b: {"$gte": [a, b]},
"lt": lambda a, b: {"$lt": [a, b]},
"lte": lambda a, b: {"$lte": [a, b]},
# MongoDB considers null less than zero. Exclude null values to match
# SQL behavior.
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, {"$ne": [a, None]}]},
"lte": lambda a, b: {"$and": [{"$lte": [a, b]}, {"$ne": [a, None]}]},
"in": lambda a, b: {"$in": [a, b]},
"isnull": _isnull_operator,
"range": lambda a, b: {
Expand Down
2 changes: 0 additions & 2 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
# the result back to UTC.
"db_functions.datetime.test_extract_trunc.DateFunctionWithTimeZoneTests.test_trunc_func_with_timezone",
"db_functions.datetime.test_extract_trunc.DateFunctionWithTimeZoneTests.test_trunc_timezone_applied_before_truncation",
# Length of null considered zero rather than null.
"db_functions.text.test_length.LengthTests.test_basic",
# Unexpected alias_refcount in alias_map.
"queries.tests.Queries1Tests.test_order_by_tables",
# The $sum aggregation returns 0 instead of None for null.
Expand Down
Empty file added tests/lookup_/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/lookup_/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models


class Number(models.Model):
num = models.IntegerField(blank=True, null=True)

class Meta:
ordering = ("num",)

def __str__(self):
return str(self.num)
17 changes: 17 additions & 0 deletions tests/lookup_/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.test import TestCase

from .models import Number


class NumericLookupTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.objs = Number.objects.bulk_create(Number(num=x) for x in range(5))
# Null values should be excluded in less than queries.
Number.objects.create()

def test_lt(self):
self.assertQuerySetEqual(Number.objects.filter(num__lt=3), self.objs[:3])

def test_lte(self):
self.assertQuerySetEqual(Number.objects.filter(num__lte=3), self.objs[:4])

0 comments on commit c191e50

Please sign in to comment.