mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #29658 -- Registered model lookups in tests with a context manager.
This commit is contained in:
parent
201017df30
commit
233c70f047
28 changed files with 78 additions and 143 deletions
|
@ -1,6 +1,7 @@
|
|||
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
|
||||
|
||||
|
@ -23,10 +24,7 @@ class ChrTests(TestCase):
|
|||
self.assertCountEqual(authors.exclude(first_initial=Chr(ord('É'))), [self.john, self.rhonda])
|
||||
|
||||
def test_transform(self):
|
||||
try:
|
||||
IntegerField.register_lookup(Chr)
|
||||
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])
|
||||
finally:
|
||||
IntegerField._unregister_lookup(Chr)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.db.models import CharField
|
||||
from django.db.models.functions import Length
|
||||
from django.test import TestCase
|
||||
from django.test.utils import register_lookup
|
||||
|
||||
from ..models import Author
|
||||
|
||||
|
@ -35,8 +36,7 @@ class LengthTests(TestCase):
|
|||
)
|
||||
|
||||
def test_transform(self):
|
||||
try:
|
||||
CharField.register_lookup(Length)
|
||||
with register_lookup(CharField, Length):
|
||||
Author.objects.create(name='John Smith', alias='smithj')
|
||||
Author.objects.create(name='Rhonda')
|
||||
authors = Author.objects.filter(name__length__gt=7)
|
||||
|
@ -44,5 +44,3 @@ class LengthTests(TestCase):
|
|||
authors.order_by('name'), ['John Smith'],
|
||||
lambda a: a.name
|
||||
)
|
||||
finally:
|
||||
CharField._unregister_lookup(Length)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.db.models import CharField
|
||||
from django.db.models.functions import Lower
|
||||
from django.test import TestCase
|
||||
from django.test.utils import register_lookup
|
||||
|
||||
from ..models import Author
|
||||
|
||||
|
@ -29,8 +30,7 @@ class LowerTests(TestCase):
|
|||
Author.objects.update(name=Lower('name', 'name'))
|
||||
|
||||
def test_transform(self):
|
||||
try:
|
||||
CharField.register_lookup(Lower)
|
||||
with register_lookup(CharField, Lower):
|
||||
Author.objects.create(name='John Smith', alias='smithj')
|
||||
Author.objects.create(name='Rhonda')
|
||||
authors = Author.objects.filter(name__lower__exact='john smith')
|
||||
|
@ -38,5 +38,3 @@ class LowerTests(TestCase):
|
|||
authors.order_by('name'), ['John Smith'],
|
||||
lambda a: a.name
|
||||
)
|
||||
finally:
|
||||
CharField._unregister_lookup(Lower)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.db.models import CharField, Value
|
||||
from django.db.models.functions import Left, Ord
|
||||
from django.test import TestCase
|
||||
from django.test.utils import register_lookup
|
||||
|
||||
from ..models import Author
|
||||
|
||||
|
@ -18,10 +19,7 @@ class OrdTests(TestCase):
|
|||
self.assertCountEqual(authors.exclude(name_part__gt=Ord(Value('John'))), [self.john])
|
||||
|
||||
def test_transform(self):
|
||||
try:
|
||||
CharField.register_lookup(Ord)
|
||||
with register_lookup(CharField, Ord):
|
||||
authors = Author.objects.annotate(first_initial=Left('name', 1))
|
||||
self.assertCountEqual(authors.filter(first_initial__ord=ord('J')), [self.john])
|
||||
self.assertCountEqual(authors.exclude(first_initial__ord=ord('J')), [self.elena, self.rhonda])
|
||||
finally:
|
||||
CharField._unregister_lookup(Ord)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.db.models import CharField
|
||||
from django.db.models.functions import LTrim, RTrim, Trim
|
||||
from django.test import TestCase
|
||||
from django.test.utils import register_lookup
|
||||
|
||||
from ..models import Author
|
||||
|
||||
|
@ -32,9 +33,6 @@ class TrimTests(TestCase):
|
|||
)
|
||||
for transform, trimmed_name in tests:
|
||||
with self.subTest(transform=transform):
|
||||
try:
|
||||
CharField.register_lookup(transform)
|
||||
with register_lookup(CharField, 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)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from django.db.models import CharField
|
||||
from django.db.models.functions import Upper
|
||||
from django.test import TestCase
|
||||
from django.test.utils import register_lookup
|
||||
|
||||
from ..models import Author
|
||||
|
||||
|
@ -28,8 +29,7 @@ class UpperTests(TestCase):
|
|||
)
|
||||
|
||||
def test_transform(self):
|
||||
try:
|
||||
CharField.register_lookup(Upper)
|
||||
with register_lookup(CharField, Upper):
|
||||
Author.objects.create(name='John Smith', alias='smithj')
|
||||
Author.objects.create(name='Rhonda')
|
||||
authors = Author.objects.filter(name__upper__exact='JOHN SMITH')
|
||||
|
@ -39,5 +39,3 @@ class UpperTests(TestCase):
|
|||
],
|
||||
lambda a: a.name
|
||||
)
|
||||
finally:
|
||||
CharField._unregister_lookup(Upper)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue