Skip to content

Commit

Permalink
add tests for Value.as_mql()
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 3, 2025
1 parent a5304b7 commit 996aebb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Empty file added tests/expressions_/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions tests/expressions_/test_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import datetime
import uuid
from decimal import Decimal

from bson import Decimal128
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),
{"$literal": datetime.datetime(2025, 1, 1)},
)

def test_datetime(self):
self.assertEqual(
Value(datetime.datetime(2025, 1, 1, 9, 8, 7)).as_mql(None, None),
{"$literal": datetime.datetime(2025, 1, 1)},
)

def test_decimal(self):
self.assertEqual(Value(Decimal("1.0")).as_mql(None, None), {"$literal": Decimal128("1.0")})

def test_time(self):
self.assertEqual(
Value(datetime.time(9, 8, 7)).as_mql(None, None),
{"$literal": datetime.datetime(1, 1, 1, 9, 8, 7)},
)

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

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

def test_str(self):
self.assertEqual(Value("foo").as_mql(None, None), {"$literal": "foo"})

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

0 comments on commit 996aebb

Please sign in to comment.