From 485f65b3c010c25b8d75d1b40156db0fa9d5ca57 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Thu, 19 Sep 2019 12:36:15 +0200 Subject: [PATCH] Refs #29915 -- Added tests for using pattern lookups with values without hyphens for UUIDField. --- tests/model_fields/test_uuid.py | 72 ++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/tests/model_fields/test_uuid.py b/tests/model_fields/test_uuid.py index 11e4a689e1..875dac5c84 100644 --- a/tests/model_fields/test_uuid.py +++ b/tests/model_fields/test_uuid.py @@ -2,7 +2,9 @@ import json import uuid from django.core import exceptions, serializers -from django.db import IntegrityError, models +from django.db import IntegrityError, connection, models +from django.db.models import CharField, F, Value +from django.db.models.functions import Concat from django.test import ( SimpleTestCase, TestCase, TransactionTestCase, skipUnlessDBFeature, ) @@ -90,11 +92,35 @@ class TestQuerying(TestCase): NullableUUIDModel.objects.create(field=None), ] + def assertSequenceEqualWithoutHyphens(self, qs, result): + """ + Backends with a native datatype for UUID don't support fragment lookups + without hyphens because they store values with them. + """ + self.assertSequenceEqual( + qs, + [] if connection.features.has_native_uuid_field else result, + ) + def test_exact(self): self.assertSequenceEqual( NullableUUIDModel.objects.filter(field__exact='550e8400e29b41d4a716446655440000'), [self.objs[1]] ) + self.assertSequenceEqual( + NullableUUIDModel.objects.filter( + field__exact='550e8400-e29b-41d4-a716-446655440000' + ), + [self.objs[1]], + ) + + def test_iexact(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter( + field__iexact='550E8400E29B41D4A716446655440000' + ), + [self.objs[1]], + ) def test_isnull(self): self.assertSequenceEqual( @@ -102,6 +128,50 @@ class TestQuerying(TestCase): [self.objs[2]] ) + def test_contains(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter(field__contains='8400e29b'), + [self.objs[1]], + ) + + def test_icontains(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter(field__icontains='8400E29B'), + [self.objs[1]], + ) + + def test_startswith(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter(field__startswith='550e8400e29b4'), + [self.objs[1]], + ) + + def test_istartswith(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter(field__istartswith='550E8400E29B4'), + [self.objs[1]], + ) + + def test_endswith(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter(field__endswith='a716446655440000'), + [self.objs[1]], + ) + + def test_iendswith(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.filter(field__iendswith='A716446655440000'), + [self.objs[1]], + ) + + def test_filter_with_expr(self): + self.assertSequenceEqualWithoutHyphens( + NullableUUIDModel.objects.annotate( + value=Concat(Value('8400'), Value('e29b'), output_field=CharField()), + ).filter(field__contains=F('value')), + [self.objs[1]], + ) + class TestSerialization(SimpleTestCase): test_data = (