0
0
mirror of https://github.com/django/django.git synced 2024-11-28 10:48:32 +01:00
django/tests/db_functions/text/test_left.py
Gregor Gärtner f0c06f8ab7 Refs #33990 -- Renamed TransactionTestCase.assertQuerysetEqual() to assertQuerySetEqual().
Co-Authored-By: Michael Howitz <mh@gocept.com>
2022-10-08 08:07:38 +02:00

36 lines
1.3 KiB
Python

from django.db.models import IntegerField, Value
from django.db.models.functions import Left, Lower
from django.test import TestCase
from ..models import Author
class LeftTests(TestCase):
@classmethod
def setUpTestData(cls):
Author.objects.create(name="John Smith", alias="smithj")
Author.objects.create(name="Rhonda")
def test_basic(self):
authors = Author.objects.annotate(name_part=Left("name", 5))
self.assertQuerySetEqual(
authors.order_by("name"), ["John ", "Rhond"], lambda a: a.name_part
)
# If alias is null, set it to the first 2 lower characters of the name.
Author.objects.filter(alias__isnull=True).update(alias=Lower(Left("name", 2)))
self.assertQuerySetEqual(
authors.order_by("name"), ["smithj", "rh"], lambda a: a.alias
)
def test_invalid_length(self):
with self.assertRaisesMessage(ValueError, "'length' must be greater than 0"):
Author.objects.annotate(raises=Left("name", 0))
def test_expressions(self):
authors = Author.objects.annotate(
name_part=Left("name", Value(3, output_field=IntegerField()))
)
self.assertQuerySetEqual(
authors.order_by("name"), ["Joh", "Rho"], lambda a: a.name_part
)