1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
import datetime
import unittest
from django.apps.registry import Apps
from django.core.exceptions import ValidationError
from django.db import models
from django.test import TestCase
from .models import (
CustomPKModel,
FlexibleDatePost,
ModelToValidate,
Post,
UniqueErrorsModel,
UniqueFieldsModel,
UniqueForDateModel,
UniqueFuncConstraintModel,
UniqueTogetherModel,
)
class GetUniqueCheckTests(unittest.TestCase):
def test_unique_fields_get_collected(self):
m = UniqueFieldsModel()
self.assertEqual(
(
[
(UniqueFieldsModel, ("id",)),
(UniqueFieldsModel, ("unique_charfield",)),
(UniqueFieldsModel, ("unique_integerfield",)),
],
[],
),
m._get_unique_checks(),
)
def test_unique_together_gets_picked_up_and_converted_to_tuple(self):
m = UniqueTogetherModel()
self.assertEqual(
(
[
(UniqueTogetherModel, ("ifield", "cfield")),
(UniqueTogetherModel, ("ifield", "efield")),
(UniqueTogetherModel, ("id",)),
],
[],
),
m._get_unique_checks(),
)
def test_unique_together_normalization(self):
"""
Test the Meta.unique_together normalization with different sorts of
objects.
"""
data = {
"2-tuple": (("foo", "bar"), (("foo", "bar"),)),
"list": (["foo", "bar"], (("foo", "bar"),)),
"already normalized": (
(("foo", "bar"), ("bar", "baz")),
(("foo", "bar"), ("bar", "baz")),
),
"set": (
{("foo", "bar"), ("bar", "baz")}, # Ref #21469
(("foo", "bar"), ("bar", "baz")),
),
}
for unique_together, normalized in data.values():
class M(models.Model):
foo = models.IntegerField()
bar = models.IntegerField()
baz = models.IntegerField()
Meta = type(
"Meta", (), {"unique_together": unique_together, "apps": Apps()}
)
checks, _ = M()._get_unique_checks()
for t in normalized:
check = (M, t)
self.assertIn(check, checks)
def test_primary_key_is_considered_unique(self):
m = CustomPKModel()
self.assertEqual(
([(CustomPKModel, ("my_pk_field",))], []), m._get_unique_checks()
)
def test_unique_for_date_gets_picked_up(self):
m = UniqueForDateModel()
self.assertEqual(
(
[(UniqueForDateModel, ("id",))],
[
(UniqueForDateModel, "date", "count", "start_date"),
(UniqueForDateModel, "year", "count", "end_date"),
(UniqueForDateModel, "month", "order", "end_date"),
],
),
m._get_unique_checks(),
)
def test_unique_for_date_exclusion(self):
m = UniqueForDateModel()
self.assertEqual(
(
[(UniqueForDateModel, ("id",))],
[
(UniqueForDateModel, "year", "count", "end_date"),
(UniqueForDateModel, "month", "order", "end_date"),
],
),
m._get_unique_checks(exclude="start_date"),
)
def test_func_unique_constraint_ignored(self):
m = UniqueFuncConstraintModel()
self.assertEqual(
m._get_unique_checks(),
([(UniqueFuncConstraintModel, ("id",))], []),
)
class PerformUniqueChecksTest(TestCase):
def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(
self,
):
# Regression test for #12560
with self.assertNumQueries(0):
mtv = ModelToValidate(number=10, name="Some Name")
setattr(mtv, "_adding", True)
mtv.full_clean()
def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
# Regression test for #12560
with self.assertNumQueries(1):
mtv = ModelToValidate(number=10, name="Some Name", id=123)
setattr(mtv, "_adding", True)
mtv.full_clean()
def test_primary_key_unique_check_not_performed_when_not_adding(self):
# Regression test for #12132
with self.assertNumQueries(0):
mtv = ModelToValidate(number=10, name="Some Name")
mtv.full_clean()
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."
]
},
)
def test_unique_for_date(self):
Post.objects.create(
title="Django 1.0 is released",
slug="Django 1.0",
subtitle="Finally",
posted=datetime.date(2008, 9, 3),
)
p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
with self.assertRaises(ValidationError) as cm:
p.full_clean()
self.assertEqual(
cm.exception.message_dict,
{"title": ["Title must be unique for Posted date."]},
)
# 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
p = Post(title="Django 1.0 is released", posted=datetime.datetime(2008, 9, 4))
p.full_clean()
p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
with self.assertRaises(ValidationError) as cm:
p.full_clean()
self.assertEqual(
cm.exception.message_dict,
{"slug": ["Slug must be unique for Posted year."]},
)
p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
with self.assertRaises(ValidationError) as cm:
p.full_clean()
self.assertEqual(
cm.exception.message_dict,
{"subtitle": ["Subtitle must be unique for Posted month."]},
)
p = Post(title="Django 1.0 is released")
with self.assertRaises(ValidationError) as cm:
p.full_clean()
self.assertEqual(
cm.exception.message_dict, {"posted": ["This field cannot be null."]}
)
def test_unique_for_date_with_nullable_date(self):
"""
unique_for_date/year/month checks shouldn't trigger when the
associated DateField is None.
"""
FlexibleDatePost.objects.create(
title="Django 1.0 is released",
slug="Django 1.0",
subtitle="Finally",
posted=datetime.date(2008, 9, 3),
)
p = FlexibleDatePost(title="Django 1.0 is released")
p.full_clean()
p = FlexibleDatePost(slug="Django 1.0")
p.full_clean()
p = FlexibleDatePost(subtitle="Finally")
p.full_clean()
def test_unique_errors(self):
UniqueErrorsModel.objects.create(name="Some Name", no=10)
m = UniqueErrorsModel(name="Some Name", no=11)
with self.assertRaises(ValidationError) as cm:
m.full_clean()
self.assertEqual(
cm.exception.message_dict, {"name": ["Custom unique name message."]}
)
m = UniqueErrorsModel(name="Some Other Name", no=10)
with self.assertRaises(ValidationError) as cm:
m.full_clean()
self.assertEqual(
cm.exception.message_dict, {"no": ["Custom unique number message."]}
)
|