diff options
Diffstat (limited to 'tests/model_fields/tests.py')
| -rw-r--r-- | tests/model_fields/tests.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 3d856d36c5..b27c07a92f 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -1,10 +1,12 @@ import pickle +import warnings from django import forms from django.core.exceptions import ValidationError -from django.db import models +from django.db import connection, models from django.test import SimpleTestCase, TestCase from django.utils.choices import CallableChoiceIterator +from django.utils.deprecation import RemovedInDjango70Warning from django.utils.functional import lazy from .models import ( @@ -147,6 +149,41 @@ class BasicFieldTests(SimpleTestCase): self.assertEqual(field_hash, hash(field)) + def test_get_placeholder_deprecation(self): + # RemovedInDjango70Warning: When the deprecation ends, remove + # completely. + msg = ( + "Field.get_placeholder is deprecated in favor of get_placeholder_sql. " + "Define model_fields.tests.BasicFieldTests." + "test_get_placeholder_deprecation.<locals>.SomeField.get_placeholder_sql " + "to return both SQL and parameters instead." + ) + with self.assertWarnsMessage(RemovedInDjango70Warning, msg): + + class SomeField(models.Field): + def get_placeholder(self, value, compiler, connection): + return "%s" + + def test_get_placeholder_sql_shim(self): + # RemovedInDjango70Warning: When the deprecation ends, remove + # completely. + with warnings.catch_warnings(record=True): + warnings.simplefilter("always") + + class SomeField(models.Field): + def get_placeholder(self, value, compiler, connection): + return "(1 + %s)" + + compiler = Bar.objects.all().query.get_compiler(connection=connection) + self.assertEqual( + SomeField().get_placeholder_sql(2, compiler, connection), + ("(1 + %s)", (2,)), + ) + self.assertEqual( + SomeField().get_placeholder_sql(models.Value(2), compiler, connection), + ("(1 + %s)", (2,)), + ) + class ChoicesTests(SimpleTestCase): @classmethod |
