mirror of
https://github.com/django/django.git
synced 2025-09-26 12:09:19 +00:00
Refs #28643 -- Added LTrim, RTrim, and Trim database functions.
Thanks Tim Graham and Mads Jensen for reviews.
This commit is contained in:
parent
47bb3b68ff
commit
9421aee35e
5 changed files with 104 additions and 7 deletions
40
tests/db_functions/test_trim.py
Normal file
40
tests/db_functions/test_trim.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
from django.db.models import CharField
|
||||
from django.db.models.functions import LTrim, RTrim, Trim
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import Author
|
||||
|
||||
|
||||
class TrimTests(TestCase):
|
||||
def test_trim(self):
|
||||
Author.objects.create(name=' John ', alias='j')
|
||||
Author.objects.create(name='Rhonda', alias='r')
|
||||
authors = Author.objects.annotate(
|
||||
ltrim=LTrim('name'),
|
||||
rtrim=RTrim('name'),
|
||||
trim=Trim('name'),
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
authors.order_by('alias'), [
|
||||
('John ', ' John', 'John'),
|
||||
('Rhonda', 'Rhonda', 'Rhonda'),
|
||||
],
|
||||
lambda a: (a.ltrim, a.rtrim, a.trim)
|
||||
)
|
||||
|
||||
def test_trim_transform(self):
|
||||
Author.objects.create(name=' John ')
|
||||
Author.objects.create(name='Rhonda')
|
||||
tests = (
|
||||
(LTrim, 'John '),
|
||||
(RTrim, ' John'),
|
||||
(Trim, 'John'),
|
||||
)
|
||||
for transform, trimmed_name in tests:
|
||||
with self.subTest(transform=transform):
|
||||
try:
|
||||
CharField.register_lookup(transform)
|
||||
authors = Author.objects.filter(**{'name__%s' % transform.lookup_name: trimmed_name})
|
||||
self.assertQuerysetEqual(authors, [' John '], lambda a: a.name)
|
||||
finally:
|
||||
CharField._unregister_lookup(transform)
|
Loading…
Add table
Add a link
Reference in a new issue