From 233c70f0479beb3bff9027e6cff680882978fd4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Srinivas=20Reddy=20Thatiparthy=20=28=E0=B0=B6=E0=B1=8D?= =?UTF-8?q?=E0=B0=B0=E0=B1=80=E0=B0=A8=E0=B0=BF=E0=B0=B5=E0=B0=BE=E0=B0=B8?= =?UTF-8?q?=E0=B1=8D=20=E0=B0=B0=E0=B1=86=E0=B0=A1=E0=B1=8D=E0=B0=A1?= =?UTF-8?q?=E0=B0=BF=20=E0=B0=A4=E0=B0=BE=E0=B0=9F=E0=B0=BF=E0=B0=AA?= =?UTF-8?q?=E0=B0=B0=E0=B1=8D=E0=B0=A4=E0=B0=BF=29?= Date: Tue, 21 Aug 2018 21:47:46 +0530 Subject: [PATCH] Fixed #29658 -- Registered model lookups in tests with a context manager. --- django/test/utils.py | 15 +++++++++++++ tests/admin_changelist/tests.py | 12 +++------- tests/custom_lookups/tests.py | 29 +++++-------------------- tests/db_functions/math/test_abs.py | 6 ++--- tests/db_functions/math/test_acos.py | 6 ++--- tests/db_functions/math/test_asin.py | 6 ++--- tests/db_functions/math/test_atan.py | 6 ++--- tests/db_functions/math/test_ceil.py | 6 ++--- tests/db_functions/math/test_cos.py | 6 ++--- tests/db_functions/math/test_cot.py | 6 ++--- tests/db_functions/math/test_degrees.py | 6 ++--- tests/db_functions/math/test_exp.py | 6 ++--- tests/db_functions/math/test_floor.py | 6 ++--- tests/db_functions/math/test_ln.py | 6 ++--- tests/db_functions/math/test_radians.py | 6 ++--- tests/db_functions/math/test_round.py | 6 ++--- tests/db_functions/math/test_sin.py | 6 ++--- tests/db_functions/math/test_sqrt.py | 6 ++--- tests/db_functions/math/test_tan.py | 6 ++--- tests/db_functions/tests.py | 21 ++++++------------ tests/db_functions/text/test_chr.py | 6 ++--- tests/db_functions/text/test_length.py | 6 ++--- tests/db_functions/text/test_lower.py | 6 ++--- tests/db_functions/text/test_ord.py | 6 ++--- tests/db_functions/text/test_trim.py | 6 ++--- tests/db_functions/text/test_upper.py | 6 ++--- tests/distinct_on_fields/tests.py | 6 ++--- tests/queries/test_query.py | 6 ++--- 28 files changed, 78 insertions(+), 143 deletions(-) diff --git a/django/test/utils.py b/django/test/utils.py index 234f831cfe..e9852436aa 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -863,3 +863,18 @@ def tag(*tags): setattr(obj, 'tags', set(tags)) return obj return decorator + + +@contextmanager +def register_lookup(field, *lookups, lookup_name=None): + """ + Context manager to temporarily register lookups on a model field using + lookup_name (or the lookup's lookup_name if not provided). + """ + try: + for lookup in lookups: + field.register_lookup(lookup, lookup_name) + yield + finally: + for lookup in lookups: + field._unregister_lookup(lookup, lookup_name) diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index c12bfc7c93..ccf115a2e4 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -16,7 +16,7 @@ from django.db.models.lookups import Contains, Exact from django.template import Context, Template, TemplateSyntaxError from django.test import TestCase, override_settings from django.test.client import RequestFactory -from django.test.utils import CaptureQueriesContext +from django.test.utils import CaptureQueriesContext, register_lookup from django.urls import reverse from django.utils import formats @@ -480,8 +480,7 @@ class ChangeListTests(TestCase): m = ConcertAdmin(Concert, custom_site) m.search_fields = ['group__name__cc'] - Field.register_lookup(Contains, 'cc') - try: + with register_lookup(Field, Contains, lookup_name='cc'): request = self.factory.get('/', data={SEARCH_VAR: 'Hype'}) request.user = self.superuser cl = m.get_changelist_instance(request) @@ -491,8 +490,6 @@ class ChangeListTests(TestCase): request.user = self.superuser cl = m.get_changelist_instance(request) self.assertCountEqual(cl.queryset, []) - finally: - Field._unregister_lookup(Contains, 'cc') def test_spanning_relations_with_custom_lookup_in_search_fields(self): hype = Group.objects.create(name='The Hype') @@ -501,8 +498,7 @@ class ChangeListTests(TestCase): Membership.objects.create(music=vox, group=hype) # Register a custom lookup on IntegerField to ensure that field # traversing logic in ModelAdmin.get_search_results() works. - IntegerField.register_lookup(Exact, 'exactly') - try: + with register_lookup(IntegerField, Exact, lookup_name='exactly'): m = ConcertAdmin(Concert, custom_site) m.search_fields = ['group__members__age__exactly'] @@ -515,8 +511,6 @@ class ChangeListTests(TestCase): request.user = self.superuser cl = m.get_changelist_instance(request) self.assertCountEqual(cl.queryset, []) - finally: - IntegerField._unregister_lookup(Exact, 'exactly') def test_custom_lookup_with_pk_shortcut(self): self.assertEqual(CharPK._meta.pk.name, 'char_pk') # Not equal to 'pk'. diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py index 418525c3ed..4bf85339ed 100644 --- a/tests/custom_lookups/tests.py +++ b/tests/custom_lookups/tests.py @@ -1,4 +1,3 @@ -import contextlib import time import unittest from datetime import date, datetime @@ -6,22 +5,12 @@ from datetime import date, datetime from django.core.exceptions import FieldError from django.db import connection, models from django.test import TestCase, override_settings +from django.test.utils import register_lookup from django.utils import timezone from .models import Article, Author, MySQLUnixTimestamp -@contextlib.contextmanager -def register_lookup(field, *lookups): - try: - for lookup in lookups: - field.register_lookup(lookup) - yield - finally: - for lookup in lookups: - field._unregister_lookup(lookup) - - class Div3Lookup(models.Lookup): lookup_name = 'div3' @@ -231,22 +220,14 @@ class LookupTests(TestCase): def test_custom_name_lookup(self): a1 = Author.objects.create(name='a1', birthdate=date(1981, 2, 16)) Author.objects.create(name='a2', birthdate=date(2012, 2, 29)) - custom_lookup_name = 'isactually' - custom_transform_name = 'justtheyear' - try: - models.DateField.register_lookup(YearTransform) - models.DateField.register_lookup(YearTransform, custom_transform_name) - YearTransform.register_lookup(Exactly) - YearTransform.register_lookup(Exactly, custom_lookup_name) + with register_lookup(models.DateField, YearTransform), \ + register_lookup(models.DateField, YearTransform, lookup_name='justtheyear'), \ + register_lookup(YearTransform, Exactly), \ + register_lookup(YearTransform, Exactly, lookup_name='isactually'): qs1 = Author.objects.filter(birthdate__testyear__exactly=1981) qs2 = Author.objects.filter(birthdate__justtheyear__isactually=1981) self.assertSequenceEqual(qs1, [a1]) self.assertSequenceEqual(qs2, [a1]) - finally: - YearTransform._unregister_lookup(Exactly) - YearTransform._unregister_lookup(Exactly, custom_lookup_name) - models.DateField._unregister_lookup(YearTransform) - models.DateField._unregister_lookup(YearTransform, custom_transform_name) def test_custom_exact_lookup_none_rhs(self): """ diff --git a/tests/db_functions/math/test_abs.py b/tests/db_functions/math/test_abs.py index 99f2a336f7..484cd2e306 100644 --- a/tests/db_functions/math/test_abs.py +++ b/tests/db_functions/math/test_abs.py @@ -3,6 +3,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Abs from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -40,11 +41,8 @@ class AbsTests(TestCase): self.assertEqual(obj.big, -obj.big_abs) def test_transform(self): - try: - DecimalField.register_lookup(Abs) + with register_lookup(DecimalField, Abs): DecimalModel.objects.create(n1=Decimal('-1.5'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-0.5'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__abs__gt=1) self.assertQuerysetEqual(objs, [Decimal('-1.5')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Abs) diff --git a/tests/db_functions/math/test_acos.py b/tests/db_functions/math/test_acos.py index 041412123b..a9ba079e4f 100644 --- a/tests/db_functions/math/test_acos.py +++ b/tests/db_functions/math/test_acos.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import ACos from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class ACosTests(TestCase): self.assertAlmostEqual(obj.big_acos, math.acos(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(ACos) + with register_lookup(DecimalField, ACos): DecimalModel.objects.create(n1=Decimal('0.5'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-0.9'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__acos__lt=2) self.assertQuerysetEqual(objs, [Decimal('0.5')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(ACos) diff --git a/tests/db_functions/math/test_asin.py b/tests/db_functions/math/test_asin.py index 5572449834..dc135a6786 100644 --- a/tests/db_functions/math/test_asin.py +++ b/tests/db_functions/math/test_asin.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import ASin from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class ASinTests(TestCase): self.assertAlmostEqual(obj.big_asin, math.asin(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(ASin) + with register_lookup(DecimalField, ASin): DecimalModel.objects.create(n1=Decimal('0.1'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__asin__gt=1) self.assertQuerysetEqual(objs, [Decimal('1.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(ASin) diff --git a/tests/db_functions/math/test_atan.py b/tests/db_functions/math/test_atan.py index 62af5d1587..36c07ae306 100644 --- a/tests/db_functions/math/test_atan.py +++ b/tests/db_functions/math/test_atan.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import ATan from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class ATanTests(TestCase): self.assertAlmostEqual(obj.big_atan, math.atan(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(ATan) + with register_lookup(DecimalField, ATan): DecimalModel.objects.create(n1=Decimal('3.12'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-5'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__atan__gt=0) self.assertQuerysetEqual(objs, [Decimal('3.12')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(ATan) diff --git a/tests/db_functions/math/test_ceil.py b/tests/db_functions/math/test_ceil.py index 93e9713eb2..a62c33a19f 100644 --- a/tests/db_functions/math/test_ceil.py +++ b/tests/db_functions/math/test_ceil.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Ceil from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class CeilTests(TestCase): self.assertEqual(obj.big_ceil, math.ceil(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Ceil) + with register_lookup(DecimalField, Ceil): DecimalModel.objects.create(n1=Decimal('3.12'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('1.25'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__ceil__gt=3) self.assertQuerysetEqual(objs, [Decimal('3.12')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Ceil) diff --git a/tests/db_functions/math/test_cos.py b/tests/db_functions/math/test_cos.py index 4d6a16f5fc..15975e247d 100644 --- a/tests/db_functions/math/test_cos.py +++ b/tests/db_functions/math/test_cos.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Cos from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class CosTests(TestCase): self.assertAlmostEqual(obj.big_cos, math.cos(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Cos) + with register_lookup(DecimalField, Cos): DecimalModel.objects.create(n1=Decimal('-8.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('3.14'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__cos__gt=-0.2) self.assertQuerysetEqual(objs, [Decimal('-8.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Cos) diff --git a/tests/db_functions/math/test_cot.py b/tests/db_functions/math/test_cot.py index a1a13c9ee5..0407f3b45d 100644 --- a/tests/db_functions/math/test_cot.py +++ b/tests/db_functions/math/test_cot.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Cot from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class CotTests(TestCase): self.assertAlmostEqual(obj.big_cot, 1 / math.tan(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Cot) + with register_lookup(DecimalField, Cot): DecimalModel.objects.create(n1=Decimal('12.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__cot__gt=0) self.assertQuerysetEqual(objs, [Decimal('1.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Cot) diff --git a/tests/db_functions/math/test_degrees.py b/tests/db_functions/math/test_degrees.py index d3c70fcbee..e5a551992f 100644 --- a/tests/db_functions/math/test_degrees.py +++ b/tests/db_functions/math/test_degrees.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Degrees from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class DegreesTests(TestCase): self.assertAlmostEqual(obj.big_degrees, math.degrees(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Degrees) + with register_lookup(DecimalField, Degrees): DecimalModel.objects.create(n1=Decimal('5.4'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-30'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__degrees__gt=0) self.assertQuerysetEqual(objs, [Decimal('5.4')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Degrees) diff --git a/tests/db_functions/math/test_exp.py b/tests/db_functions/math/test_exp.py index c44f7adefe..0981d4fce3 100644 --- a/tests/db_functions/math/test_exp.py +++ b/tests/db_functions/math/test_exp.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Exp from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class ExpTests(TestCase): self.assertAlmostEqual(obj.big_exp, math.exp(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Exp) + with register_lookup(DecimalField, Exp): DecimalModel.objects.create(n1=Decimal('12.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__exp__gt=10) self.assertQuerysetEqual(objs, [Decimal('12.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Exp) diff --git a/tests/db_functions/math/test_floor.py b/tests/db_functions/math/test_floor.py index 8404184c3e..ee567cfea6 100644 --- a/tests/db_functions/math/test_floor.py +++ b/tests/db_functions/math/test_floor.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Floor from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class FloorTests(TestCase): self.assertEqual(obj.big_floor, math.floor(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Floor) + with register_lookup(DecimalField, Floor): DecimalModel.objects.create(n1=Decimal('5.4'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('3.4'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__floor__gt=4) self.assertQuerysetEqual(objs, [Decimal('5.4')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Floor) diff --git a/tests/db_functions/math/test_ln.py b/tests/db_functions/math/test_ln.py index f30cf8e84e..96d4599bb3 100644 --- a/tests/db_functions/math/test_ln.py +++ b/tests/db_functions/math/test_ln.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Ln from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class LnTests(TestCase): self.assertAlmostEqual(obj.big_ln, math.log(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Ln) + with register_lookup(DecimalField, Ln): DecimalModel.objects.create(n1=Decimal('12.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__ln__gt=0) self.assertQuerysetEqual(objs, [Decimal('12.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Ln) diff --git a/tests/db_functions/math/test_radians.py b/tests/db_functions/math/test_radians.py index 296c21e692..873659e7ad 100644 --- a/tests/db_functions/math/test_radians.py +++ b/tests/db_functions/math/test_radians.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Radians from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class RadiansTests(TestCase): self.assertAlmostEqual(obj.big_radians, math.radians(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Radians) + with register_lookup(DecimalField, Radians): DecimalModel.objects.create(n1=Decimal('2.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__radians__gt=0) self.assertQuerysetEqual(objs, [Decimal('2.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Radians) diff --git a/tests/db_functions/math/test_round.py b/tests/db_functions/math/test_round.py index 9ad390bd24..d242f2de0f 100644 --- a/tests/db_functions/math/test_round.py +++ b/tests/db_functions/math/test_round.py @@ -3,6 +3,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Round from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -40,11 +41,8 @@ class RoundTests(TestCase): self.assertEqual(obj.big_round, round(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Round) + with register_lookup(DecimalField, Round): DecimalModel.objects.create(n1=Decimal('2.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('-1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__round__gt=0) self.assertQuerysetEqual(objs, [Decimal('2.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Round) diff --git a/tests/db_functions/math/test_sin.py b/tests/db_functions/math/test_sin.py index efcf3319f0..0f7e0c7c0b 100644 --- a/tests/db_functions/math/test_sin.py +++ b/tests/db_functions/math/test_sin.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Sin from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class SinTests(TestCase): self.assertAlmostEqual(obj.big_sin, math.sin(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Sin) + with register_lookup(DecimalField, Sin): DecimalModel.objects.create(n1=Decimal('5.4'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('0.1'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__sin__lt=0) self.assertQuerysetEqual(objs, [Decimal('5.4')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Sin) diff --git a/tests/db_functions/math/test_sqrt.py b/tests/db_functions/math/test_sqrt.py index c391a6f5a9..81f13361e1 100644 --- a/tests/db_functions/math/test_sqrt.py +++ b/tests/db_functions/math/test_sqrt.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Sqrt from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class SqrtTests(TestCase): self.assertAlmostEqual(obj.big_sqrt, math.sqrt(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Sqrt) + with register_lookup(DecimalField, Sqrt): DecimalModel.objects.create(n1=Decimal('6.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('1.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__sqrt__gt=2) self.assertQuerysetEqual(objs, [Decimal('6.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Sqrt) diff --git a/tests/db_functions/math/test_tan.py b/tests/db_functions/math/test_tan.py index 1df294150f..82dcec94fc 100644 --- a/tests/db_functions/math/test_tan.py +++ b/tests/db_functions/math/test_tan.py @@ -4,6 +4,7 @@ from decimal import Decimal from django.db.models import DecimalField from django.db.models.functions import Tan from django.test import TestCase +from django.test.utils import register_lookup from ..models import DecimalModel, FloatModel, IntegerModel @@ -41,11 +42,8 @@ class TanTests(TestCase): self.assertAlmostEqual(obj.big_tan, math.tan(obj.big)) def test_transform(self): - try: - DecimalField.register_lookup(Tan) + with register_lookup(DecimalField, Tan): DecimalModel.objects.create(n1=Decimal('0.0'), n2=Decimal('0')) DecimalModel.objects.create(n1=Decimal('12.0'), n2=Decimal('0')) objs = DecimalModel.objects.filter(n1__tan__lt=0) self.assertQuerysetEqual(objs, [Decimal('12.0')], lambda a: a.n1) - finally: - DecimalField._unregister_lookup(Tan) diff --git a/tests/db_functions/tests.py b/tests/db_functions/tests.py index 9c06735377..e6d8a4fa0a 100644 --- a/tests/db_functions/tests.py +++ b/tests/db_functions/tests.py @@ -1,10 +1,15 @@ from django.db.models import CharField, Value as V from django.db.models.functions import Coalesce, Length, Upper from django.test import TestCase +from django.test.utils import register_lookup from .models import Author +class UpperBilateral(Upper): + bilateral = True + + class FunctionTests(TestCase): def test_nested_function_ordering(self): @@ -30,11 +35,7 @@ class FunctionTests(TestCase): ) def test_func_transform_bilateral(self): - class UpperBilateral(Upper): - bilateral = True - - try: - CharField.register_lookup(UpperBilateral) + with register_lookup(CharField, UpperBilateral): Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='john smith') @@ -44,15 +45,9 @@ class FunctionTests(TestCase): ], lambda a: a.name ) - finally: - CharField._unregister_lookup(UpperBilateral) def test_func_transform_bilateral_multivalue(self): - class UpperBilateral(Upper): - bilateral = True - - try: - CharField.register_lookup(UpperBilateral) + with register_lookup(CharField, UpperBilateral): Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__in=['john smith', 'rhonda']) @@ -63,8 +58,6 @@ class FunctionTests(TestCase): ], lambda a: a.name ) - finally: - CharField._unregister_lookup(UpperBilateral) def test_function_as_filter(self): Author.objects.create(name='John Smith', alias='SMITHJ') diff --git a/tests/db_functions/text/test_chr.py b/tests/db_functions/text/test_chr.py index d48793457a..67a2a6a98d 100644 --- a/tests/db_functions/text/test_chr.py +++ b/tests/db_functions/text/test_chr.py @@ -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) diff --git a/tests/db_functions/text/test_length.py b/tests/db_functions/text/test_length.py index 8fbe6887aa..62d1d1c775 100644 --- a/tests/db_functions/text/test_length.py +++ b/tests/db_functions/text/test_length.py @@ -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) diff --git a/tests/db_functions/text/test_lower.py b/tests/db_functions/text/test_lower.py index f438682f1d..2f8dd6257d 100644 --- a/tests/db_functions/text/test_lower.py +++ b/tests/db_functions/text/test_lower.py @@ -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) diff --git a/tests/db_functions/text/test_ord.py b/tests/db_functions/text/test_ord.py index c4ad730f6c..dd704d3c9a 100644 --- a/tests/db_functions/text/test_ord.py +++ b/tests/db_functions/text/test_ord.py @@ -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) diff --git a/tests/db_functions/text/test_trim.py b/tests/db_functions/text/test_trim.py index 3144aef028..9834cef1c6 100644 --- a/tests/db_functions/text/test_trim.py +++ b/tests/db_functions/text/test_trim.py @@ -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) diff --git a/tests/db_functions/text/test_upper.py b/tests/db_functions/text/test_upper.py index 091e815d6a..5b5df0af33 100644 --- a/tests/db_functions/text/test_upper.py +++ b/tests/db_functions/text/test_upper.py @@ -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) diff --git a/tests/distinct_on_fields/tests.py b/tests/distinct_on_fields/tests.py index ae4eb3bd19..6ed73cd75a 100644 --- a/tests/distinct_on_fields/tests.py +++ b/tests/distinct_on_fields/tests.py @@ -1,6 +1,7 @@ from django.db.models import CharField, Max from django.db.models.functions import Lower from django.test import TestCase, skipUnlessDBFeature +from django.test.utils import register_lookup from .models import Celebrity, Fan, Staff, StaffTag, Tag @@ -100,14 +101,11 @@ class DistinctOnTests(TestCase): new_name = self.t1.name.upper() self.assertNotEqual(self.t1.name, new_name) Tag.objects.create(name=new_name) - CharField.register_lookup(Lower) - try: + with register_lookup(CharField, Lower): self.assertCountEqual( Tag.objects.order_by().distinct('name__lower'), [self.t1, self.t2, self.t3, self.t4, self.t5], ) - finally: - CharField._unregister_lookup(Lower) def test_distinct_not_implemented_checks(self): # distinct + annotate not allowed diff --git a/tests/queries/test_query.py b/tests/queries/test_query.py index 10ea8eb0f2..f8f43d9577 100644 --- a/tests/queries/test_query.py +++ b/tests/queries/test_query.py @@ -9,6 +9,7 @@ from django.db.models.lookups import Exact, GreaterThan, IsNull, LessThan from django.db.models.sql.query import Query from django.db.models.sql.where import OR from django.test import TestCase +from django.test.utils import register_lookup from .models import Author, Item, ObjectC, Ranking @@ -49,11 +50,8 @@ class TestQuery(TestCase): def test_transform(self): query = Query(Author) - CharField.register_lookup(Lower, 'lower') - try: + with register_lookup(CharField, Lower): where = query.build_where(~Q(name__lower='foo')) - finally: - CharField._unregister_lookup(Lower, 'lower') lookup = where.children[0] self.assertIsInstance(lookup, Exact) self.assertIsInstance(lookup.lhs, Lower)