Skip to content

Commit

Permalink
fix model_fields.test_jsonfield.TestSaveLoad.test_bulk_update_custom_…
Browse files Browse the repository at this point in the history
…get_prep_value

django/django@9525135
  • Loading branch information
timgraham committed Feb 25, 2025
1 parent 7931488 commit 29623f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 6 additions & 0 deletions django_mongodb_backend/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def when(self, compiler, connection):

def value(self, compiler, connection): # noqa: ARG001
value = self.value
output_field = self._output_field_or_none
if output_field is not None:
if self.for_save:
value = output_field.get_db_prep_save(value, connection=connection)
else:
value = output_field.get_db_prep_value(value, connection=connection)
if isinstance(value, int):
# Wrap numbers in $literal to prevent ambiguity when Value appears in
# $project.
Expand Down
2 changes: 2 additions & 0 deletions django_mongodb_backend/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"prefetch_related.tests.NestedPrefetchTests.test_nested_prefetch_is_not_overwritten_by_related_object",
"prefetch_related.tests.NullableTest.test_prefetch_nullable",
"prefetch_related.tests.Ticket19607Tests.test_bug",
# {'$project': {'name': Decimal128('1')} is broken? (gives None)
"expressions.tests.ValueTests.test_output_field_decimalfield",
}
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
_django_test_expected_failures_bitwise = {
Expand Down
19 changes: 11 additions & 8 deletions tests/expressions_/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,44 @@
from decimal import Decimal

from bson import Decimal128
from django.db import connection
from django.db.models import Value
from django.test import SimpleTestCase


class ValueTests(SimpleTestCase):
def test_date(self):
self.assertEqual(
Value(datetime.date(2025, 1, 1)).as_mql(None, None),
Value(datetime.date(2025, 1, 1)).as_mql(None, connection),
datetime.datetime(2025, 1, 1),
)

def test_datetime(self):
self.assertEqual(
Value(datetime.datetime(2025, 1, 1, 9, 8, 7)).as_mql(None, None),
Value(datetime.datetime(2025, 1, 1, 9, 8, 7)).as_mql(None, connection),
datetime.datetime(2025, 1, 1, 9, 8, 7),
)

def test_decimal(self):
self.assertEqual(Value(Decimal("1.0")).as_mql(None, None), Decimal128("1.0"))
self.assertEqual(Value(Decimal("1.0")).as_mql(None, connection), Decimal128("1.0"))

def test_time(self):
self.assertEqual(
Value(datetime.time(9, 8, 7)).as_mql(None, None),
Value(datetime.time(9, 8, 7)).as_mql(None, connection),
datetime.datetime(1, 1, 1, 9, 8, 7),
)

def test_timedelta(self):
self.assertEqual(Value(datetime.timedelta(3600)).as_mql(None, None), 311040000000.0)
self.assertEqual(
Value(datetime.timedelta(3600)).as_mql(None, connection), {"$literal": 311040000000}
)

def test_int(self):
self.assertEqual(Value(1).as_mql(None, None), {"$literal": 1})
self.assertEqual(Value(1).as_mql(None, connection), {"$literal": 1})

def test_str(self):
self.assertEqual(Value("foo").as_mql(None, None), "foo")
self.assertEqual(Value("foo").as_mql(None, connection), "foo")

def test_uuid(self):
value = uuid.UUID(int=1)
self.assertEqual(Value(value).as_mql(None, None), "00000000000000000000000000000001")
self.assertEqual(Value(value).as_mql(None, connection), "00000000000000000000000000000001")

0 comments on commit 29623f6

Please sign in to comment.