2017-12-19 19:54:58 +01:00
|
|
|
import datetime
|
2017-12-28 01:16:37 +01:00
|
|
|
import decimal
|
2021-06-22 06:10:54 +02:00
|
|
|
import unittest
|
2017-12-19 19:54:58 +01:00
|
|
|
|
2018-01-15 19:11:20 +01:00
|
|
|
from django.db import connection, models
|
2015-06-05 16:20:37 +02:00
|
|
|
from django.db.models.functions import Cast
|
2021-01-28 17:50:54 +01:00
|
|
|
from django.test import TestCase, ignore_warnings, skipUnlessDBFeature
|
2021-06-22 06:10:54 +02:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2015-06-05 16:20:37 +02:00
|
|
|
|
2018-10-15 15:57:22 +02:00
|
|
|
from ..models import Author, DTModel, Fan, FloatModel
|
2015-06-05 16:20:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CastTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
Author.objects.create(name="Bob", age=1, alias="1")
|
2015-06-05 16:20:37 +02:00
|
|
|
|
|
|
|
def test_cast_from_value(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
numbers = Author.objects.annotate(
|
|
|
|
cast_integer=Cast(models.Value("0"), models.IntegerField())
|
|
|
|
)
|
2015-06-05 16:20:37 +02:00
|
|
|
self.assertEqual(numbers.get().cast_integer, 0)
|
|
|
|
|
|
|
|
def test_cast_from_field(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
numbers = Author.objects.annotate(
|
|
|
|
cast_string=Cast("age", models.CharField(max_length=255)),
|
|
|
|
)
|
|
|
|
self.assertEqual(numbers.get().cast_string, "1")
|
2015-06-05 16:20:37 +02:00
|
|
|
|
2017-07-27 19:36:47 +02:00
|
|
|
def test_cast_to_char_field_without_max_length(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
numbers = Author.objects.annotate(cast_string=Cast("age", models.CharField()))
|
|
|
|
self.assertEqual(numbers.get().cast_string, "1")
|
2017-07-27 19:36:47 +02:00
|
|
|
|
2017-07-17 21:12:27 +02:00
|
|
|
# Silence "Truncated incorrect CHAR(1) value: 'Bob'".
|
2022-02-03 20:24:19 +01:00
|
|
|
@ignore_warnings(module="django.db.backends.mysql.base")
|
|
|
|
@skipUnlessDBFeature("supports_cast_with_precision")
|
2017-07-17 21:12:27 +02:00
|
|
|
def test_cast_to_char_field_with_max_length(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
names = Author.objects.annotate(
|
|
|
|
cast_string=Cast("name", models.CharField(max_length=1))
|
|
|
|
)
|
|
|
|
self.assertEqual(names.get().cast_string, "B")
|
2017-07-17 21:12:27 +02:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
@skipUnlessDBFeature("supports_cast_with_precision")
|
2018-10-15 15:57:22 +02:00
|
|
|
def test_cast_to_decimal_field(self):
|
|
|
|
FloatModel.objects.create(f1=-1.934, f2=3.467)
|
|
|
|
float_obj = FloatModel.objects.annotate(
|
2022-02-03 20:24:19 +01:00
|
|
|
cast_f1_decimal=Cast(
|
|
|
|
"f1", models.DecimalField(max_digits=8, decimal_places=2)
|
|
|
|
),
|
|
|
|
cast_f2_decimal=Cast(
|
|
|
|
"f2", models.DecimalField(max_digits=8, decimal_places=1)
|
|
|
|
),
|
2018-10-15 15:57:22 +02:00
|
|
|
).get()
|
2022-02-03 20:24:19 +01:00
|
|
|
self.assertEqual(float_obj.cast_f1_decimal, decimal.Decimal("-1.93"))
|
|
|
|
self.assertEqual(float_obj.cast_f2_decimal, decimal.Decimal("3.5"))
|
2018-10-15 15:57:22 +02:00
|
|
|
author_obj = Author.objects.annotate(
|
2022-02-03 20:24:19 +01:00
|
|
|
cast_alias_decimal=Cast(
|
|
|
|
"alias", models.DecimalField(max_digits=8, decimal_places=2)
|
|
|
|
),
|
2018-10-15 15:57:22 +02:00
|
|
|
).get()
|
2022-02-03 20:24:19 +01:00
|
|
|
self.assertEqual(author_obj.cast_alias_decimal, decimal.Decimal("1"))
|
2018-10-15 15:57:22 +02:00
|
|
|
|
2017-06-21 16:16:53 +02:00
|
|
|
def test_cast_to_integer(self):
|
|
|
|
for field_class in (
|
2018-07-25 08:00:11 +02:00
|
|
|
models.AutoField,
|
|
|
|
models.BigAutoField,
|
2019-07-26 23:05:22 +02:00
|
|
|
models.SmallAutoField,
|
2017-06-21 16:16:53 +02:00
|
|
|
models.IntegerField,
|
|
|
|
models.BigIntegerField,
|
|
|
|
models.SmallIntegerField,
|
2019-10-16 14:32:12 +02:00
|
|
|
models.PositiveBigIntegerField,
|
2017-06-21 16:16:53 +02:00
|
|
|
models.PositiveIntegerField,
|
|
|
|
models.PositiveSmallIntegerField,
|
|
|
|
):
|
|
|
|
with self.subTest(field_class=field_class):
|
2022-02-03 20:24:19 +01:00
|
|
|
numbers = Author.objects.annotate(cast_int=Cast("alias", field_class()))
|
2017-06-21 16:16:53 +02:00
|
|
|
self.assertEqual(numbers.get().cast_int, 1)
|
|
|
|
|
2020-09-08 16:21:41 +02:00
|
|
|
def test_cast_to_duration(self):
|
|
|
|
duration = datetime.timedelta(days=1, seconds=2, microseconds=3)
|
|
|
|
DTModel.objects.create(duration=duration)
|
|
|
|
dtm = DTModel.objects.annotate(
|
2022-02-03 20:24:19 +01:00
|
|
|
cast_duration=Cast("duration", models.DurationField()),
|
2020-09-08 16:21:41 +02:00
|
|
|
cast_neg_duration=Cast(-duration, models.DurationField()),
|
|
|
|
).get()
|
|
|
|
self.assertEqual(dtm.cast_duration, duration)
|
|
|
|
self.assertEqual(dtm.cast_neg_duration, -duration)
|
|
|
|
|
2018-09-18 13:59:10 +02:00
|
|
|
def test_cast_from_db_datetime_to_date(self):
|
|
|
|
dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567)
|
|
|
|
DTModel.objects.create(start_datetime=dt_value)
|
|
|
|
dtm = DTModel.objects.annotate(
|
2022-02-03 20:24:19 +01:00
|
|
|
start_datetime_as_date=Cast("start_datetime", models.DateField())
|
2018-09-18 13:59:10 +02:00
|
|
|
).first()
|
|
|
|
self.assertEqual(dtm.start_datetime_as_date, datetime.date(2018, 9, 28))
|
|
|
|
|
|
|
|
def test_cast_from_db_datetime_to_time(self):
|
|
|
|
dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567)
|
|
|
|
DTModel.objects.create(start_datetime=dt_value)
|
|
|
|
dtm = DTModel.objects.annotate(
|
2022-02-03 20:24:19 +01:00
|
|
|
start_datetime_as_time=Cast("start_datetime", models.TimeField())
|
2018-09-18 13:59:10 +02:00
|
|
|
).first()
|
2022-02-03 20:24:19 +01:00
|
|
|
rounded_ms = int(
|
|
|
|
round(0.234567, connection.features.time_cast_precision) * 10**6
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
dtm.start_datetime_as_time, datetime.time(12, 42, 10, rounded_ms)
|
|
|
|
)
|
2018-09-18 13:59:10 +02:00
|
|
|
|
|
|
|
def test_cast_from_db_date_to_datetime(self):
|
|
|
|
dt_value = datetime.date(2018, 9, 28)
|
|
|
|
DTModel.objects.create(start_date=dt_value)
|
2022-02-03 20:24:19 +01:00
|
|
|
dtm = DTModel.objects.annotate(
|
|
|
|
start_as_datetime=Cast("start_date", models.DateTimeField())
|
|
|
|
).first()
|
|
|
|
self.assertEqual(
|
|
|
|
dtm.start_as_datetime, datetime.datetime(2018, 9, 28, 0, 0, 0, 0)
|
|
|
|
)
|
2018-09-18 13:59:10 +02:00
|
|
|
|
|
|
|
def test_cast_from_db_datetime_to_date_group_by(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
author = Author.objects.create(name="John Smith", age=45)
|
2018-09-18 13:59:10 +02:00
|
|
|
dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567)
|
2022-02-03 20:24:19 +01:00
|
|
|
Fan.objects.create(name="Margaret", age=50, author=author, fan_since=dt_value)
|
|
|
|
fans = (
|
|
|
|
Fan.objects.values("author")
|
|
|
|
.annotate(
|
|
|
|
fan_for_day=Cast("fan_since", models.DateField()),
|
|
|
|
fans=models.Count("*"),
|
|
|
|
)
|
|
|
|
.values()
|
|
|
|
)
|
|
|
|
self.assertEqual(fans[0]["fan_for_day"], datetime.date(2018, 9, 28))
|
|
|
|
self.assertEqual(fans[0]["fans"], 1)
|
2018-09-18 13:59:10 +02:00
|
|
|
|
2017-12-19 19:54:58 +01:00
|
|
|
def test_cast_from_python_to_date(self):
|
|
|
|
today = datetime.date.today()
|
|
|
|
dates = Author.objects.annotate(cast_date=Cast(today, models.DateField()))
|
|
|
|
self.assertEqual(dates.get().cast_date, today)
|
|
|
|
|
|
|
|
def test_cast_from_python_to_datetime(self):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
dates = Author.objects.annotate(cast_datetime=Cast(now, models.DateTimeField()))
|
2018-09-18 13:59:10 +02:00
|
|
|
time_precision = datetime.timedelta(
|
2022-02-03 20:24:19 +01:00
|
|
|
microseconds=10 ** (6 - connection.features.time_cast_precision)
|
2018-09-18 13:59:10 +02:00
|
|
|
)
|
|
|
|
self.assertAlmostEqual(dates.get().cast_datetime, now, delta=time_precision)
|
2017-12-19 19:54:58 +01:00
|
|
|
|
2015-06-05 16:20:37 +02:00
|
|
|
def test_cast_from_python(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
numbers = Author.objects.annotate(
|
|
|
|
cast_float=Cast(decimal.Decimal(0.125), models.FloatField())
|
|
|
|
)
|
2017-12-28 01:16:37 +01:00
|
|
|
cast_float = numbers.get().cast_float
|
|
|
|
self.assertIsInstance(cast_float, float)
|
|
|
|
self.assertEqual(cast_float, 0.125)
|
2018-01-15 19:11:20 +01:00
|
|
|
|
2022-02-03 20:24:19 +01:00
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test")
|
2021-06-22 06:10:54 +02:00
|
|
|
def test_expression_wrapped_with_parentheses_on_postgresql(self):
|
|
|
|
"""
|
|
|
|
The SQL for the Cast expression is wrapped with parentheses in case
|
|
|
|
it's a complex expression.
|
|
|
|
"""
|
|
|
|
with CaptureQueriesContext(connection) as captured_queries:
|
2022-02-03 20:24:19 +01:00
|
|
|
list(
|
|
|
|
Author.objects.annotate(
|
|
|
|
cast_float=Cast(models.Avg("age"), models.FloatField()),
|
|
|
|
)
|
|
|
|
)
|
2021-06-22 06:10:54 +02:00
|
|
|
self.assertIn(
|
|
|
|
'(AVG("db_functions_author"."age"))::double precision',
|
2022-02-03 20:24:19 +01:00
|
|
|
captured_queries[0]["sql"],
|
2021-06-22 06:10:54 +02:00
|
|
|
)
|
|
|
|
|
2018-03-13 02:57:14 +01:00
|
|
|
def test_cast_to_text_field(self):
|
2022-02-03 20:24:19 +01:00
|
|
|
self.assertEqual(
|
|
|
|
Author.objects.values_list(
|
|
|
|
Cast("age", models.TextField()), flat=True
|
|
|
|
).get(),
|
|
|
|
"1",
|
|
|
|
)
|