Debian Codegraph @

tests/db_functions/text/test_chr.py

raw
1 from django.db.models import F, IntegerField
2 from django.db.models.functions import Chr, Left, Ord
3 from django.test import TestCase
4 from django.test.utils import register_lookup
5
6 from ..models import Author
7
8
9 class ChrTests(TestCase):
10 @classmethod
11 def setUpTestData(cls):
12 cls.john = Author.objects.create(name="John Smith", alias="smithj")
13 cls.elena = Author.objects.create(name="Élena Jordan", alias="elena")
14 cls.rhonda = Author.objects.create(name="Rhonda")
15
16 def test_basic(self):
17 authors = Author.objects.annotate(first_initial=Left("name", 1))
18 self.assertCountEqual(authors.filter(first_initial=Chr(ord("J"))), [self.john])
19 self.assertCountEqual(
20 authors.exclude(first_initial=Chr(ord("J"))), [self.elena, self.rhonda]
21 )
22
23 def test_non_ascii(self):
24 authors = Author.objects.annotate(first_initial=Left("name", 1))
25 self.assertCountEqual(authors.filter(first_initial=Chr(ord("É"))), [self.elena])
26 self.assertCountEqual(
27 authors.exclude(first_initial=Chr(ord("É"))), [self.john, self.rhonda]
28 )
29
30 def test_transform(self):
31 with register_lookup(IntegerField, Chr):
32 authors = Author.objects.annotate(name_code_point=Ord("name"))
33 self.assertCountEqual(
34 authors.filter(name_code_point__chr=Chr(ord("J"))), [self.john]
35 )
36 self.assertCountEqual(
37 authors.exclude(name_code_point__chr=Chr(ord("J"))),
38 [self.elena, self.rhonda],
39 )
40
41 def test_annotate(self):
42 authors = Author.objects.annotate(
43 first_initial=Left("name", 1),
44 initial_chr=Chr(ord("J")),
45 )
46 self.assertSequenceEqual(
47 authors.filter(first_initial=F("initial_chr")),
48 [self.john],
49 )