diff options
| author | Claude Paroz <claude@2xlibre.net> | 2015-06-05 15:20:37 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-03-29 08:14:33 -0400 |
| commit | 03b6947728466e4e907487f30dd4dfec94a8eb2f (patch) | |
| tree | 923a8045ea4b2cc686a072ccbfa1be73f9f5fa06 /tests/db_functions | |
| parent | 53361589905c7c07692cd77f398ce6cb5ac39779 (diff) | |
Fixed #24932 -- Added Cast database function.
Thanks Ian Foote for the initial patch.
Diffstat (limited to 'tests/db_functions')
| -rw-r--r-- | tests/db_functions/test_cast.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/db_functions/test_cast.py b/tests/db_functions/test_cast.py new file mode 100644 index 0000000000..39d4f44f9c --- /dev/null +++ b/tests/db_functions/test_cast.py @@ -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) |
