Skip to content

make less than lookups exclude null values #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Loading