1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #29915 -- Added tests for using pattern lookups with values without hyphens for UUIDField.

This commit is contained in:
Ian Foote 2019-09-19 12:36:15 +02:00 committed by Mariusz Felisiak
parent 28e769dfe6
commit 485f65b3c0

View File

@ -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 = (