mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #24932 -- Added Cast database function.
Thanks Ian Foote for the initial patch.
This commit is contained in:
parent
5336158990
commit
03b6947728
4 changed files with 77 additions and 0 deletions
24
tests/db_functions/test_cast.py
Normal file
24
tests/db_functions/test_cast.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
from django.db import models
|
||||
from django.db.models.expressions import Value
|
||||
from django.db.models.functions import Cast
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import Author
|
||||
|
||||
|
||||
class CastTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
Author.objects.create(name='Bob', age=1)
|
||||
|
||||
def test_cast_from_value(self):
|
||||
numbers = Author.objects.annotate(cast_integer=Cast(Value('0'), models.IntegerField()))
|
||||
self.assertEqual(numbers.get().cast_integer, 0)
|
||||
|
||||
def test_cast_from_field(self):
|
||||
numbers = Author.objects.annotate(cast_string=Cast('age', models.CharField(max_length=255)),)
|
||||
self.assertEqual(numbers.get().cast_string, '1')
|
||||
|
||||
def test_cast_from_python(self):
|
||||
numbers = Author.objects.annotate(cast_float=Cast(0, models.FloatField()))
|
||||
self.assertEqual(numbers.get().cast_float, 0.0)
|
Loading…
Add table
Add a link
Reference in a new issue