| 1 |
import json |
| 2 |
import uuid |
| 3 |
|
| 4 |
from django.core import exceptions, serializers |
| 5 |
from django.db import IntegrityError, connection, models |
| 6 |
from django.db.models import CharField, F, Value |
| 7 |
from django.db.models.functions import Concat, Repeat |
| 8 |
from django.test import ( |
| 9 |
SimpleTestCase, |
| 10 |
TestCase, |
| 11 |
TransactionTestCase, |
| 12 |
skipUnlessDBFeature, |
| 13 |
) |
| 14 |
|
| 15 |
from .models import ( |
| 16 |
NullableUUIDModel, |
| 17 |
PrimaryKeyUUIDModel, |
| 18 |
RelatedToUUIDModel, |
| 19 |
UUIDGrandchild, |
| 20 |
UUIDModel, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
class TestSaveLoad(TestCase): |
| 25 |
def test_uuid_instance(self): |
| 26 |
instance = UUIDModel.objects.create(field=uuid.uuid4()) |
| 27 |
loaded = UUIDModel.objects.get() |
| 28 |
self.assertEqual(loaded.field, instance.field) |
| 29 |
|
| 30 |
def test_str_instance_no_hyphens(self): |
| 31 |
UUIDModel.objects.create(field="550e8400e29b41d4a716446655440000") |
| 32 |
loaded = UUIDModel.objects.get() |
| 33 |
self.assertEqual(loaded.field, uuid.UUID("550e8400e29b41d4a716446655440000")) |
| 34 |
|
| 35 |
def test_str_instance_hyphens(self): |
| 36 |
UUIDModel.objects.create(field="550e8400-e29b-41d4-a716-446655440000") |
| 37 |
loaded = UUIDModel.objects.get() |
| 38 |
self.assertEqual(loaded.field, uuid.UUID("550e8400e29b41d4a716446655440000")) |
| 39 |
|
| 40 |
def test_str_instance_bad_hyphens(self): |
| 41 |
UUIDModel.objects.create(field="550e84-00-e29b-41d4-a716-4-466-55440000") |
| 42 |
loaded = UUIDModel.objects.get() |
| 43 |
self.assertEqual(loaded.field, uuid.UUID("550e8400e29b41d4a716446655440000")) |
| 44 |
|
| 45 |
def test_null_handling(self): |
| 46 |
NullableUUIDModel.objects.create(field=None) |
| 47 |
loaded = NullableUUIDModel.objects.get() |
| 48 |
self.assertIsNone(loaded.field) |
| 49 |
|
| 50 |
def test_pk_validated(self): |
| 51 |
with self.assertRaisesMessage( |
| 52 |
exceptions.ValidationError, "is not a valid UUID" |
| 53 |
): |
| 54 |
PrimaryKeyUUIDModel.objects.get(pk={}) |
| 55 |
|
| 56 |
with self.assertRaisesMessage( |
| 57 |
exceptions.ValidationError, "is not a valid UUID" |
| 58 |
): |
| 59 |
PrimaryKeyUUIDModel.objects.get(pk=[]) |
| 60 |
|
| 61 |
def test_wrong_value(self): |
| 62 |
with self.assertRaisesMessage( |
| 63 |
exceptions.ValidationError, "is not a valid UUID" |
| 64 |
): |
| 65 |
UUIDModel.objects.get(field="not-a-uuid") |
| 66 |
|
| 67 |
with self.assertRaisesMessage( |
| 68 |
exceptions.ValidationError, "is not a valid UUID" |
| 69 |
): |
| 70 |
UUIDModel.objects.create(field="not-a-uuid") |
| 71 |
|
| 72 |
|
| 73 |
class TestMethods(SimpleTestCase): |
| 74 |
def test_deconstruct(self): |
| 75 |
field = models.UUIDField() |
| 76 |
name, path, args, kwargs = field.deconstruct() |
| 77 |
self.assertEqual(kwargs, {}) |
| 78 |
|
| 79 |
def test_to_python(self): |
| 80 |
self.assertIsNone(models.UUIDField().to_python(None)) |
| 81 |
|
| 82 |
def test_to_python_int_values(self): |
| 83 |
self.assertEqual( |
| 84 |
models.UUIDField().to_python(0), |
| 85 |
uuid.UUID("00000000-0000-0000-0000-000000000000"), |
| 86 |
) |
| 87 |
# Works for integers less than 128 bits. |
| 88 |
self.assertEqual( |
| 89 |
models.UUIDField().to_python((2**128) - 1), |
| 90 |
uuid.UUID("ffffffff-ffff-ffff-ffff-ffffffffffff"), |
| 91 |
) |
| 92 |
|
| 93 |
def test_to_python_int_too_large(self): |
| 94 |
# Fails for integers larger than 128 bits. |
| 95 |
with self.assertRaises(exceptions.ValidationError): |
| 96 |
models.UUIDField().to_python(2**128) |
| 97 |
|
| 98 |
|
| 99 |
class TestQuerying(TestCase): |
| 100 |
@classmethod |
| 101 |
def setUpTestData(cls): |
| 102 |
cls.objs = [ |
| 103 |
NullableUUIDModel.objects.create( |
| 104 |
field=uuid.UUID("25d405be-4895-4d50-9b2e-d6695359ce47"), |
| 105 |
), |
| 106 |
NullableUUIDModel.objects.create(field="550e8400e29b41d4a716446655440000"), |
| 107 |
NullableUUIDModel.objects.create(field=None), |
| 108 |
] |
| 109 |
|
| 110 |
def assertSequenceEqualWithoutHyphens(self, qs, result): |
| 111 |
""" |
| 112 |
Backends with a native datatype for UUID don't support fragment lookups |
| 113 |
without hyphens because they store values with them. |
| 114 |
""" |
| 115 |
self.assertSequenceEqual( |
| 116 |
qs, |
| 117 |
[] if connection.features.has_native_uuid_field else result, |
| 118 |
) |
| 119 |
|
| 120 |
def test_exact(self): |
| 121 |
self.assertSequenceEqual( |
| 122 |
NullableUUIDModel.objects.filter( |
| 123 |
field__exact="550e8400e29b41d4a716446655440000" |
| 124 |
), |
| 125 |
[self.objs[1]], |
| 126 |
) |
| 127 |
self.assertSequenceEqual( |
| 128 |
NullableUUIDModel.objects.filter( |
| 129 |
field__exact="550e8400-e29b-41d4-a716-446655440000" |
| 130 |
), |
| 131 |
[self.objs[1]], |
| 132 |
) |
| 133 |
|
| 134 |
def test_iexact(self): |
| 135 |
self.assertSequenceEqualWithoutHyphens( |
| 136 |
NullableUUIDModel.objects.filter( |
| 137 |
field__iexact="550E8400E29B41D4A716446655440000" |
| 138 |
), |
| 139 |
[self.objs[1]], |
| 140 |
) |
| 141 |
self.assertSequenceEqual( |
| 142 |
NullableUUIDModel.objects.filter( |
| 143 |
field__iexact="550E8400-E29B-41D4-A716-446655440000" |
| 144 |
), |
| 145 |
[self.objs[1]], |
| 146 |
) |
| 147 |
|
| 148 |
def test_isnull(self): |
| 149 |
self.assertSequenceEqual( |
| 150 |
NullableUUIDModel.objects.filter(field__isnull=True), [self.objs[2]] |
| 151 |
) |
| 152 |
|
| 153 |
def test_contains(self): |
| 154 |
self.assertSequenceEqualWithoutHyphens( |
| 155 |
NullableUUIDModel.objects.filter(field__contains="8400e29b"), |
| 156 |
[self.objs[1]], |
| 157 |
) |
| 158 |
self.assertSequenceEqual( |
| 159 |
NullableUUIDModel.objects.filter(field__contains="8400-e29b"), |
| 160 |
[self.objs[1]], |
| 161 |
) |
| 162 |
|
| 163 |
def test_icontains(self): |
| 164 |
self.assertSequenceEqualWithoutHyphens( |
| 165 |
NullableUUIDModel.objects.filter(field__icontains="8400E29B"), |
| 166 |
[self.objs[1]], |
| 167 |
) |
| 168 |
self.assertSequenceEqual( |
| 169 |
NullableUUIDModel.objects.filter(field__icontains="8400-E29B"), |
| 170 |
[self.objs[1]], |
| 171 |
) |
| 172 |
|
| 173 |
def test_startswith(self): |
| 174 |
self.assertSequenceEqualWithoutHyphens( |
| 175 |
NullableUUIDModel.objects.filter(field__startswith="550e8400e29b4"), |
| 176 |
[self.objs[1]], |
| 177 |
) |
| 178 |
self.assertSequenceEqual( |
| 179 |
NullableUUIDModel.objects.filter(field__startswith="550e8400-e29b-4"), |
| 180 |
[self.objs[1]], |
| 181 |
) |
| 182 |
|
| 183 |
def test_istartswith(self): |
| 184 |
self.assertSequenceEqualWithoutHyphens( |
| 185 |
NullableUUIDModel.objects.filter(field__istartswith="550E8400E29B4"), |
| 186 |
[self.objs[1]], |
| 187 |
) |
| 188 |
self.assertSequenceEqual( |
| 189 |
NullableUUIDModel.objects.filter(field__istartswith="550E8400-E29B-4"), |
| 190 |
[self.objs[1]], |
| 191 |
) |
| 192 |
|
| 193 |
def test_endswith(self): |
| 194 |
self.assertSequenceEqualWithoutHyphens( |
| 195 |
NullableUUIDModel.objects.filter(field__endswith="a716446655440000"), |
| 196 |
[self.objs[1]], |
| 197 |
) |
| 198 |
self.assertSequenceEqual( |
| 199 |
NullableUUIDModel.objects.filter(field__endswith="a716-446655440000"), |
| 200 |
[self.objs[1]], |
| 201 |
) |
| 202 |
|
| 203 |
def test_iendswith(self): |
| 204 |
self.assertSequenceEqualWithoutHyphens( |
| 205 |
NullableUUIDModel.objects.filter(field__iendswith="A716446655440000"), |
| 206 |
[self.objs[1]], |
| 207 |
) |
| 208 |
self.assertSequenceEqual( |
| 209 |
NullableUUIDModel.objects.filter(field__iendswith="A716-446655440000"), |
| 210 |
[self.objs[1]], |
| 211 |
) |
| 212 |
|
| 213 |
def test_filter_with_expr(self): |
| 214 |
self.assertSequenceEqualWithoutHyphens( |
| 215 |
NullableUUIDModel.objects.annotate( |
| 216 |
value=Concat(Value("8400"), Value("e29b"), output_field=CharField()), |
| 217 |
).filter(field__contains=F("value")), |
| 218 |
[self.objs[1]], |
| 219 |
) |
| 220 |
self.assertSequenceEqual( |
| 221 |
NullableUUIDModel.objects.annotate( |
| 222 |
value=Concat( |
| 223 |
Value("8400"), Value("-"), Value("e29b"), output_field=CharField() |
| 224 |
), |
| 225 |
).filter(field__contains=F("value")), |
| 226 |
[self.objs[1]], |
| 227 |
) |
| 228 |
self.assertSequenceEqual( |
| 229 |
NullableUUIDModel.objects.annotate( |
| 230 |
value=Repeat(Value("0"), 4, output_field=CharField()), |
| 231 |
).filter(field__contains=F("value")), |
| 232 |
[self.objs[1]], |
| 233 |
) |
| 234 |
|
| 235 |
|
| 236 |
class TestSerialization(SimpleTestCase): |
| 237 |
test_data = ( |
| 238 |
'[{"fields": {"field": "550e8400-e29b-41d4-a716-446655440000"}, ' |
| 239 |
'"model": "model_fields.uuidmodel", "pk": null}]' |
| 240 |
) |
| 241 |
nullable_test_data = ( |
| 242 |
'[{"fields": {"field": null}, ' |
| 243 |
'"model": "model_fields.nullableuuidmodel", "pk": null}]' |
| 244 |
) |
| 245 |
|
| 246 |
def test_dumping(self): |
| 247 |
instance = UUIDModel(field=uuid.UUID("550e8400e29b41d4a716446655440000")) |
| 248 |
data = serializers.serialize("json", [instance]) |
| 249 |
self.assertEqual(json.loads(data), json.loads(self.test_data)) |
| 250 |
|
| 251 |
def test_loading(self): |
| 252 |
instance = list(serializers.deserialize("json", self.test_data))[0].object |
| 253 |
self.assertEqual( |
| 254 |
instance.field, uuid.UUID("550e8400-e29b-41d4-a716-446655440000") |
| 255 |
) |
| 256 |
|
| 257 |
def test_nullable_loading(self): |
| 258 |
instance = list(serializers.deserialize("json", self.nullable_test_data))[ |
| 259 |
0 |
| 260 |
].object |
| 261 |
self.assertIsNone(instance.field) |
| 262 |
|
| 263 |
|
| 264 |
class TestValidation(SimpleTestCase): |
| 265 |
def test_invalid_uuid(self): |
| 266 |
field = models.UUIDField() |
| 267 |
with self.assertRaises(exceptions.ValidationError) as cm: |
| 268 |
field.clean("550e8400", None) |
| 269 |
self.assertEqual(cm.exception.code, "invalid") |
| 270 |
self.assertEqual( |
| 271 |
cm.exception.message % cm.exception.params, |
| 272 |
"“550e8400” is not a valid UUID.", |
| 273 |
) |
| 274 |
|
| 275 |
def test_uuid_instance_ok(self): |
| 276 |
field = models.UUIDField() |
| 277 |
field.clean(uuid.uuid4(), None) # no error |
| 278 |
|
| 279 |
|
| 280 |
class TestAsPrimaryKey(TestCase): |
| 281 |
def test_creation(self): |
| 282 |
PrimaryKeyUUIDModel.objects.create() |
| 283 |
loaded = PrimaryKeyUUIDModel.objects.get() |
| 284 |
self.assertIsInstance(loaded.pk, uuid.UUID) |
| 285 |
|
| 286 |
def test_uuid_pk_on_save(self): |
| 287 |
saved = PrimaryKeyUUIDModel.objects.create(id=None) |
| 288 |
loaded = PrimaryKeyUUIDModel.objects.get() |
| 289 |
self.assertIsNotNone(loaded.id, None) |
| 290 |
self.assertEqual(loaded.id, saved.id) |
| 291 |
|
| 292 |
def test_uuid_pk_on_bulk_create(self): |
| 293 |
u1 = PrimaryKeyUUIDModel() |
| 294 |
u2 = PrimaryKeyUUIDModel(id=None) |
| 295 |
PrimaryKeyUUIDModel.objects.bulk_create([u1, u2]) |
| 296 |
# The two objects were correctly created. |
| 297 |
u1_found = PrimaryKeyUUIDModel.objects.filter(id=u1.id).exists() |
| 298 |
u2_found = PrimaryKeyUUIDModel.objects.exclude(id=u1.id).exists() |
| 299 |
self.assertTrue(u1_found) |
| 300 |
self.assertTrue(u2_found) |
| 301 |
self.assertEqual(PrimaryKeyUUIDModel.objects.count(), 2) |
| 302 |
|
| 303 |
def test_underlying_field(self): |
| 304 |
pk_model = PrimaryKeyUUIDModel.objects.create() |
| 305 |
RelatedToUUIDModel.objects.create(uuid_fk=pk_model) |
| 306 |
related = RelatedToUUIDModel.objects.get() |
| 307 |
self.assertEqual(related.uuid_fk.pk, related.uuid_fk_id) |
| 308 |
|
| 309 |
def test_update_with_related_model_instance(self): |
| 310 |
# regression for #24611 |
| 311 |
u1 = PrimaryKeyUUIDModel.objects.create() |
| 312 |
u2 = PrimaryKeyUUIDModel.objects.create() |
| 313 |
r = RelatedToUUIDModel.objects.create(uuid_fk=u1) |
| 314 |
RelatedToUUIDModel.objects.update(uuid_fk=u2) |
| 315 |
r.refresh_from_db() |
| 316 |
self.assertEqual(r.uuid_fk, u2) |
| 317 |
|
| 318 |
def test_update_with_related_model_id(self): |
| 319 |
u1 = PrimaryKeyUUIDModel.objects.create() |
| 320 |
u2 = PrimaryKeyUUIDModel.objects.create() |
| 321 |
r = RelatedToUUIDModel.objects.create(uuid_fk=u1) |
| 322 |
RelatedToUUIDModel.objects.update(uuid_fk=u2.pk) |
| 323 |
r.refresh_from_db() |
| 324 |
self.assertEqual(r.uuid_fk, u2) |
| 325 |
|
| 326 |
def test_two_level_foreign_keys(self): |
| 327 |
gc = UUIDGrandchild() |
| 328 |
# exercises ForeignKey.get_db_prep_value() |
| 329 |
gc.save() |
| 330 |
self.assertIsInstance(gc.uuidchild_ptr_id, uuid.UUID) |
| 331 |
gc.refresh_from_db() |
| 332 |
self.assertIsInstance(gc.uuidchild_ptr_id, uuid.UUID) |
| 333 |
|
| 334 |
|
| 335 |
class TestAsPrimaryKeyTransactionTests(TransactionTestCase): |
| 336 |
# Need a TransactionTestCase to avoid deferring FK constraint checking. |
| 337 |
available_apps = ["model_fields"] |
| 338 |
|
| 339 |
@skipUnlessDBFeature("supports_foreign_keys") |
| 340 |
def test_unsaved_fk(self): |
| 341 |
u1 = PrimaryKeyUUIDModel() |
| 342 |
with self.assertRaises(IntegrityError): |
| 343 |
RelatedToUUIDModel.objects.create(uuid_fk=u1) |