From babfc65f811e5f746900cbaa64c25cd5dccb6069 Mon Sep 17 00:00:00 2001 From: Adam Zapletal Date: Tue, 12 Nov 2024 08:15:17 -0600 Subject: [PATCH] Refs #21286 -- Fixed and enabled TextField primary key serializer test data. The `TextPKData` serializer test data was failing on some database backends. This is because a primary key requires an index, and not all backends support indexes on `TextField`. This patch ensures that the `TextPKData` model and related test are only included with database backends that have the `supports_index_on_text_field` feature. --- tests/serializers/models/data.py | 7 +++++-- tests/serializers/test_data.py | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/serializers/models/data.py b/tests/serializers/models/data.py index bb76bfba48..b82e8b4b33 100644 --- a/tests/serializers/models/data.py +++ b/tests/serializers/models/data.py @@ -242,8 +242,11 @@ class SmallPKData(models.Model): data = models.SmallIntegerField(primary_key=True) -# class TextPKData(models.Model): -# data = models.TextField(primary_key=True) +class TextPKData(models.Model): + data = models.TextField(primary_key=True) + + class Meta: + required_db_features = ["supports_index_on_text_field"] class TimePKData(models.Model): diff --git a/tests/serializers/test_data.py b/tests/serializers/test_data.py index 1f8f38ba0f..808db41634 100644 --- a/tests/serializers/test_data.py +++ b/tests/serializers/test_data.py @@ -68,6 +68,7 @@ from .models import ( SmallPKData, Tag, TextData, + TextPKData, TimeData, TimePKData, UniqueAnchor, @@ -387,10 +388,15 @@ The end.""", (pk_obj, 750, SmallPKData, 12), (pk_obj, 751, SmallPKData, -12), (pk_obj, 752, SmallPKData, 0), - # (pk_obj, 760, TextPKData, """This is a long piece of text. - # It contains line breaks. - # Several of them. - # The end."""), + ( + pk_obj, + 760, + TextPKData, + """This is a long piece of text. + It contains line breaks. + Several of them. + The end.""", + ), (pk_obj, 770, TimePKData, datetime.time(10, 42, 37)), (pk_obj, 791, UUIDData, uuid_obj), (fk_obj, 792, FKToUUID, uuid_obj), @@ -429,6 +435,10 @@ if connection.features.interprets_empty_strings_as_nulls: ] +if not connection.features.supports_index_on_text_field: + test_data = [data for data in test_data if data[2] != TextPKData] + + class SerializerDataTests(TestCase): pass