mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
This partly reverts commitfdfbc66331unnecessary sinceb69b0c3fe8.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import datetime
|
||||
import decimal
|
||||
import unittest
|
||||
|
||||
from django.db import connection, models
|
||||
from django.db.models.functions import Cast
|
||||
from django.test import TestCase, ignore_warnings, skipUnlessDBFeature
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
|
||||
from ..models import Author, DTModel, Fan, FloatModel
|
||||
|
||||
@@ -125,5 +127,20 @@ class CastTests(TestCase):
|
||||
self.assertIsInstance(cast_float, float)
|
||||
self.assertEqual(cast_float, 0.125)
|
||||
|
||||
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL test')
|
||||
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:
|
||||
list(Author.objects.annotate(
|
||||
cast_float=Cast(models.Avg('age'), models.FloatField()),
|
||||
))
|
||||
self.assertIn(
|
||||
'(AVG("db_functions_author"."age"))::double precision',
|
||||
captured_queries[0]['sql'],
|
||||
)
|
||||
|
||||
def test_cast_to_text_field(self):
|
||||
self.assertEqual(Author.objects.values_list(Cast('age', models.TextField()), flat=True).get(), '1')
|
||||
|
||||
Reference in New Issue
Block a user