1
0
mirror of https://github.com/django/django.git synced 2024-12-23 17:46:27 +00:00
django/tests/db_functions/text/test_chr.py
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) 233c70f047 Fixed #29658 -- Registered model lookups in tests with a context manager.
2018-08-21 12:17:46 -04:00

31 lines
1.4 KiB
Python

from django.db.models import IntegerField
from django.db.models.functions import Chr, Left, Ord
from django.test import TestCase
from django.test.utils import register_lookup
from ..models import Author
class ChrTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.john = Author.objects.create(name='John Smith', alias='smithj')
cls.elena = Author.objects.create(name='Élena Jordan', alias='elena')
cls.rhonda = Author.objects.create(name='Rhonda')
def test_basic(self):
authors = Author.objects.annotate(first_initial=Left('name', 1))
self.assertCountEqual(authors.filter(first_initial=Chr(ord('J'))), [self.john])
self.assertCountEqual(authors.exclude(first_initial=Chr(ord('J'))), [self.elena, self.rhonda])
def test_non_ascii(self):
authors = Author.objects.annotate(first_initial=Left('name', 1))
self.assertCountEqual(authors.filter(first_initial=Chr(ord('É'))), [self.elena])
self.assertCountEqual(authors.exclude(first_initial=Chr(ord('É'))), [self.john, self.rhonda])
def test_transform(self):
with register_lookup(IntegerField, Chr):
authors = Author.objects.annotate(name_code_point=Ord('name'))
self.assertCountEqual(authors.filter(name_code_point__chr=Chr(ord('J'))), [self.john])
self.assertCountEqual(authors.exclude(name_code_point__chr=Chr(ord('J'))), [self.elena, self.rhonda])