| 1 |
from django.db.models import CharField |
| 2 |
from django.db.models.functions import Lower |
| 3 |
from django.test import TestCase |
| 4 |
from django.test.utils import register_lookup |
| 5 |
|
| 6 |
from ..models import Author |
| 7 |
|
| 8 |
|
| 9 |
class LowerTests(TestCase): |
| 10 |
def test_basic(self): |
| 11 |
Author.objects.create(name="John Smith", alias="smithj") |
| 12 |
Author.objects.create(name="Rhonda") |
| 13 |
authors = Author.objects.annotate(lower_name=Lower("name")) |
| 14 |
self.assertQuerySetEqual( |
| 15 |
authors.order_by("name"), ["john smith", "rhonda"], lambda a: a.lower_name |
| 16 |
) |
| 17 |
Author.objects.update(name=Lower("name")) |
| 18 |
self.assertQuerySetEqual( |
| 19 |
authors.order_by("name"), |
| 20 |
[ |
| 21 |
("john smith", "john smith"), |
| 22 |
("rhonda", "rhonda"), |
| 23 |
], |
| 24 |
lambda a: (a.lower_name, a.name), |
| 25 |
) |
| 26 |
|
| 27 |
def test_num_args(self): |
| 28 |
with self.assertRaisesMessage( |
| 29 |
TypeError, "'Lower' takes exactly 1 argument (2 given)" |
| 30 |
): |
| 31 |
Author.objects.update(name=Lower("name", "name")) |
| 32 |
|
| 33 |
def test_transform(self): |
| 34 |
with register_lookup(CharField, Lower): |
| 35 |
Author.objects.create(name="John Smith", alias="smithj") |
| 36 |
Author.objects.create(name="Rhonda") |
| 37 |
authors = Author.objects.filter(name__lower__exact="john smith") |
| 38 |
self.assertQuerySetEqual( |
| 39 |
authors.order_by("name"), ["John Smith"], lambda a: a.name |
| 40 |
) |