2010-01-12 02:29:45 +00:00
|
|
|
import datetime
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2013-12-24 11:25:17 +00:00
|
|
|
from django.apps.registry import Apps
|
2011-01-09 13:26:39 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-11-20 09:28:22 +00:00
|
|
|
from django.db import models
|
2010-10-12 03:33:19 +00:00
|
|
|
from django.test import TestCase
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import (
|
|
|
|
CustomPKModel,
|
|
|
|
FlexibleDatePost,
|
|
|
|
ModelToValidate,
|
|
|
|
Post,
|
|
|
|
UniqueErrorsModel,
|
2021-12-04 20:03:38 +00:00
|
|
|
UniqueFieldsModel,
|
|
|
|
UniqueForDateModel,
|
|
|
|
UniqueFuncConstraintModel,
|
|
|
|
UniqueTogetherModel,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetUniqueCheckTests(unittest.TestCase):
|
|
|
|
def test_unique_fields_get_collected(self):
|
|
|
|
m = UniqueFieldsModel()
|
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
|
|
|
[
|
2010-03-16 19:32:11 +00:00
|
|
|
(UniqueFieldsModel, ("id",)),
|
|
|
|
(UniqueFieldsModel, ("unique_charfield",)),
|
|
|
|
(UniqueFieldsModel, ("unique_integerfield",)),
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
),
|
2010-01-05 03:56:19 +00:00
|
|
|
m._get_unique_checks(),
|
|
|
|
)
|
|
|
|
|
2010-02-10 00:49:33 +00:00
|
|
|
def test_unique_together_gets_picked_up_and_converted_to_tuple(self):
|
2010-01-05 03:56:19 +00:00
|
|
|
m = UniqueTogetherModel()
|
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
|
|
|
[
|
2013-11-20 09:28:22 +00:00
|
|
|
(UniqueTogetherModel, ("ifield", "cfield")),
|
2010-03-16 19:32:11 +00:00
|
|
|
(UniqueTogetherModel, ("ifield", "efield")),
|
2017-12-28 20:07:29 +00:00
|
|
|
(UniqueTogetherModel, ("id",)),
|
|
|
|
],
|
2010-03-16 19:32:11 +00:00
|
|
|
[],
|
|
|
|
),
|
2010-01-05 03:56:19 +00:00
|
|
|
m._get_unique_checks(),
|
|
|
|
)
|
|
|
|
|
2013-11-20 09:28:22 +00:00
|
|
|
def test_unique_together_normalization(self):
|
|
|
|
"""
|
|
|
|
Test the Meta.unique_together normalization with different sorts of
|
|
|
|
objects.
|
|
|
|
"""
|
|
|
|
data = {
|
2016-04-08 02:04:45 +00:00
|
|
|
"2-tuple": (("foo", "bar"), (("foo", "bar"),)),
|
|
|
|
"list": (["foo", "bar"], (("foo", "bar"),)),
|
2013-11-20 09:28:22 +00:00
|
|
|
"already normalized": (
|
|
|
|
(("foo", "bar"), ("bar", "baz")),
|
|
|
|
(("foo", "bar"), ("bar", "baz")),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2013-11-20 09:28:22 +00:00
|
|
|
"set": (
|
|
|
|
{("foo", "bar"), ("bar", "baz")}, # Ref #21469
|
|
|
|
(("foo", "bar"), ("bar", "baz")),
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2013-11-20 09:28:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:17:59 +00:00
|
|
|
for unique_together, normalized in data.values():
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2013-11-20 09:28:22 +00:00
|
|
|
class M(models.Model):
|
|
|
|
foo = models.IntegerField()
|
|
|
|
bar = models.IntegerField()
|
|
|
|
baz = models.IntegerField()
|
|
|
|
|
2017-01-19 14:48:01 +00:00
|
|
|
Meta = type(
|
2013-11-20 09:28:22 +00:00
|
|
|
"Meta", (), {"unique_together": unique_together, "apps": Apps()}
|
|
|
|
)
|
|
|
|
|
|
|
|
checks, _ = M()._get_unique_checks()
|
|
|
|
for t in normalized:
|
|
|
|
check = (M, t)
|
|
|
|
self.assertIn(check, checks)
|
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
def test_primary_key_is_considered_unique(self):
|
|
|
|
m = CustomPKModel()
|
2010-03-16 19:32:11 +00:00
|
|
|
self.assertEqual(
|
|
|
|
([(CustomPKModel, ("my_pk_field",))], []), m._get_unique_checks()
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_unique_for_date_gets_picked_up(self):
|
|
|
|
m = UniqueForDateModel()
|
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
2010-03-16 19:32:11 +00:00
|
|
|
[(UniqueForDateModel, ("id",))],
|
|
|
|
[
|
|
|
|
(UniqueForDateModel, "date", "count", "start_date"),
|
|
|
|
(UniqueForDateModel, "year", "count", "end_date"),
|
|
|
|
(UniqueForDateModel, "month", "order", "end_date"),
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2013-10-18 09:02:43 +00:00
|
|
|
),
|
|
|
|
m._get_unique_checks(),
|
2010-01-05 03:56:19 +00:00
|
|
|
)
|
|
|
|
|
2010-08-17 07:07:28 +00:00
|
|
|
def test_unique_for_date_exclusion(self):
|
|
|
|
m = UniqueForDateModel()
|
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
2010-08-17 07:07:28 +00:00
|
|
|
[(UniqueForDateModel, ("id",))],
|
|
|
|
[
|
|
|
|
(UniqueForDateModel, "year", "count", "end_date"),
|
|
|
|
(UniqueForDateModel, "month", "order", "end_date"),
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
2013-10-18 09:02:43 +00:00
|
|
|
),
|
|
|
|
m._get_unique_checks(exclude="start_date"),
|
2010-08-17 07:07:28 +00:00
|
|
|
)
|
|
|
|
|
2021-12-04 20:03:38 +00:00
|
|
|
def test_func_unique_constraint_ignored(self):
|
|
|
|
m = UniqueFuncConstraintModel()
|
|
|
|
self.assertEqual(
|
|
|
|
m._get_unique_checks(),
|
|
|
|
([(UniqueFuncConstraintModel, ("id",))], []),
|
|
|
|
)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-10-12 03:33:19 +00:00
|
|
|
class PerformUniqueChecksTest(TestCase):
|
2010-01-14 17:04:53 +00:00
|
|
|
def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
# Regression test for #12560
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(0):
|
2010-10-12 03:33:19 +00:00
|
|
|
mtv = ModelToValidate(number=10, name="Some Name")
|
|
|
|
setattr(mtv, "_adding", True)
|
|
|
|
mtv.full_clean()
|
2010-01-14 17:04:53 +00:00
|
|
|
|
|
|
|
def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
|
|
|
|
# Regression test for #12560
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 03:33:19 +00:00
|
|
|
mtv = ModelToValidate(number=10, name="Some Name", id=123)
|
|
|
|
setattr(mtv, "_adding", True)
|
|
|
|
mtv.full_clean()
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_primary_key_unique_check_not_performed_when_not_adding(self):
|
2010-01-14 17:04:53 +00:00
|
|
|
# Regression test for #12132
|
2011-10-14 17:03:08 +00:00
|
|
|
with self.assertNumQueries(0):
|
2010-10-12 03:33:19 +00:00
|
|
|
mtv = ModelToValidate(number=10, name="Some Name")
|
|
|
|
mtv.full_clean()
|
2011-01-09 13:26:39 +00:00
|
|
|
|
2024-08-05 06:22:29 +00:00
|
|
|
def test_unique_db_default(self):
|
|
|
|
UniqueFieldsModel.objects.create(unique_charfield="foo", non_unique_field=42)
|
|
|
|
um = UniqueFieldsModel(unique_charfield="bar", non_unique_field=42)
|
|
|
|
with self.assertRaises(ValidationError) as cm:
|
|
|
|
um.full_clean()
|
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict,
|
|
|
|
{
|
|
|
|
"unique_integerfield": [
|
|
|
|
"Unique fields model with this Unique integerfield already exists."
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2011-01-09 13:26:39 +00:00
|
|
|
def test_unique_for_date(self):
|
2016-04-08 02:04:45 +00:00
|
|
|
Post.objects.create(
|
|
|
|
title="Django 1.0 is released",
|
|
|
|
slug="Django 1.0",
|
|
|
|
subtitle="Finally",
|
|
|
|
posted=datetime.date(2008, 9, 3),
|
|
|
|
)
|
2011-01-09 13:26:39 +00:00
|
|
|
p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
|
2011-06-09 19:35:44 +00:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 13:26:39 +00:00
|
|
|
p.full_clean()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict,
|
|
|
|
{"title": ["Title must be unique for Posted date."]},
|
|
|
|
)
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
# Should work without errors
|
|
|
|
p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3))
|
|
|
|
p.full_clean()
|
|
|
|
|
|
|
|
# Should work without errors
|
2013-10-26 19:15:03 +00:00
|
|
|
p = Post(title="Django 1.0 is released", posted=datetime.datetime(2008, 9, 4))
|
2011-01-09 13:26:39 +00:00
|
|
|
p.full_clean()
|
|
|
|
|
|
|
|
p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
|
2011-06-09 19:35:44 +00:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 13:26:39 +00:00
|
|
|
p.full_clean()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict,
|
|
|
|
{"slug": ["Slug must be unique for Posted year."]},
|
|
|
|
)
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
|
2011-06-09 19:35:44 +00:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 13:26:39 +00:00
|
|
|
p.full_clean()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict,
|
|
|
|
{"subtitle": ["Subtitle must be unique for Posted month."]},
|
|
|
|
)
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
p = Post(title="Django 1.0 is released")
|
2011-06-09 19:35:44 +00:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 13:26:39 +00:00
|
|
|
p.full_clean()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict, {"posted": ["This field cannot be null."]}
|
|
|
|
)
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
def test_unique_for_date_with_nullable_date(self):
|
2016-06-28 15:21:26 +00:00
|
|
|
"""
|
|
|
|
unique_for_date/year/month checks shouldn't trigger when the
|
|
|
|
associated DateField is None.
|
|
|
|
"""
|
2016-04-08 02:04:45 +00:00
|
|
|
FlexibleDatePost.objects.create(
|
|
|
|
title="Django 1.0 is released",
|
|
|
|
slug="Django 1.0",
|
|
|
|
subtitle="Finally",
|
|
|
|
posted=datetime.date(2008, 9, 3),
|
|
|
|
)
|
2011-01-09 13:26:39 +00:00
|
|
|
p = FlexibleDatePost(title="Django 1.0 is released")
|
2016-06-28 15:21:26 +00:00
|
|
|
p.full_clean()
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
p = FlexibleDatePost(slug="Django 1.0")
|
2016-06-28 15:21:26 +00:00
|
|
|
p.full_clean()
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
p = FlexibleDatePost(subtitle="Finally")
|
2016-06-28 15:21:26 +00:00
|
|
|
p.full_clean()
|
2011-06-09 15:05:13 +00:00
|
|
|
|
|
|
|
def test_unique_errors(self):
|
2013-10-19 12:31:38 +00:00
|
|
|
UniqueErrorsModel.objects.create(name="Some Name", no=10)
|
2011-07-03 22:15:39 +00:00
|
|
|
m = UniqueErrorsModel(name="Some Name", no=11)
|
2011-06-09 19:35:44 +00:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-06-09 15:05:13 +00:00
|
|
|
m.full_clean()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict, {"name": ["Custom unique name message."]}
|
|
|
|
)
|
2011-06-09 15:05:13 +00:00
|
|
|
|
2011-07-03 22:15:39 +00:00
|
|
|
m = UniqueErrorsModel(name="Some Other Name", no=10)
|
2011-06-09 19:35:44 +00:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-06-09 15:05:13 +00:00
|
|
|
m.full_clean()
|
2012-06-07 16:08:47 +00:00
|
|
|
self.assertEqual(
|
|
|
|
cm.exception.message_dict, {"no": ["Custom unique number message."]}
|
|
|
|
)
|