2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-06-10 20:16:56 +08:00
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
import datetime
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps.registry import Apps
|
2011-01-09 21:26:39 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-11-20 17:28:22 +08:00
|
|
|
from django.db import models
|
2010-10-12 11:33:19 +08:00
|
|
|
from django.test import TestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from .models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
|
|
|
|
UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost,
|
|
|
|
UniqueErrorsModel)
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GetUniqueCheckTests(unittest.TestCase):
|
|
|
|
def test_unique_fields_get_collected(self):
|
|
|
|
m = UniqueFieldsModel()
|
|
|
|
self.assertEqual(
|
2010-03-17 03:32:11 +08:00
|
|
|
([(UniqueFieldsModel, ('id',)),
|
|
|
|
(UniqueFieldsModel, ('unique_charfield',)),
|
|
|
|
(UniqueFieldsModel, ('unique_integerfield',))],
|
|
|
|
[]),
|
2010-01-05 11:56:19 +08:00
|
|
|
m._get_unique_checks()
|
|
|
|
)
|
|
|
|
|
2010-02-10 08:49:33 +08:00
|
|
|
def test_unique_together_gets_picked_up_and_converted_to_tuple(self):
|
2010-01-05 11:56:19 +08:00
|
|
|
m = UniqueTogetherModel()
|
|
|
|
self.assertEqual(
|
2013-11-20 17:28:22 +08:00
|
|
|
([(UniqueTogetherModel, ('ifield', 'cfield')),
|
2010-03-17 03:32:11 +08:00
|
|
|
(UniqueTogetherModel, ('ifield', 'efield')),
|
|
|
|
(UniqueTogetherModel, ('id',)), ],
|
|
|
|
[]),
|
2010-01-05 11:56:19 +08:00
|
|
|
m._get_unique_checks()
|
|
|
|
)
|
|
|
|
|
2013-11-20 17:28:22 +08:00
|
|
|
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 test_name, (unique_together, normalized) in data.items():
|
|
|
|
class M(models.Model):
|
|
|
|
foo = models.IntegerField()
|
|
|
|
bar = models.IntegerField()
|
|
|
|
baz = models.IntegerField()
|
|
|
|
|
|
|
|
Meta = type(str('Meta'), (), {
|
|
|
|
'unique_together': unique_together,
|
2013-12-24 19:25:17 +08:00
|
|
|
'apps': Apps()
|
2013-11-20 17:28:22 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
checks, _ = M()._get_unique_checks()
|
|
|
|
for t in normalized:
|
|
|
|
check = (M, t)
|
|
|
|
self.assertIn(check, checks)
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
def test_primary_key_is_considered_unique(self):
|
|
|
|
m = CustomPKModel()
|
2010-03-17 03:32:11 +08:00
|
|
|
self.assertEqual(([(CustomPKModel, ('my_pk_field',))], []), m._get_unique_checks())
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
def test_unique_for_date_gets_picked_up(self):
|
|
|
|
m = UniqueForDateModel()
|
|
|
|
self.assertEqual((
|
2010-03-17 03:32:11 +08:00
|
|
|
[(UniqueForDateModel, ('id',))],
|
|
|
|
[(UniqueForDateModel, 'date', 'count', 'start_date'),
|
|
|
|
(UniqueForDateModel, 'year', 'count', 'end_date'),
|
|
|
|
(UniqueForDateModel, 'month', 'order', 'end_date')]
|
2013-10-18 17:02:43 +08:00
|
|
|
), m._get_unique_checks()
|
2010-01-05 11:56:19 +08:00
|
|
|
)
|
|
|
|
|
2010-08-17 15:07:28 +08:00
|
|
|
def test_unique_for_date_exclusion(self):
|
|
|
|
m = UniqueForDateModel()
|
|
|
|
self.assertEqual((
|
|
|
|
[(UniqueForDateModel, ('id',))],
|
|
|
|
[(UniqueForDateModel, 'year', 'count', 'end_date'),
|
|
|
|
(UniqueForDateModel, 'month', 'order', 'end_date')]
|
2013-10-18 17:02:43 +08:00
|
|
|
), m._get_unique_checks(exclude='start_date')
|
2010-08-17 15:07:28 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2010-10-12 11:33:19 +08:00
|
|
|
class PerformUniqueChecksTest(TestCase):
|
2010-01-15 01:04:53 +08:00
|
|
|
def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self):
|
|
|
|
# Regression test for #12560
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(0):
|
2010-10-12 11:33:19 +08:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
|
|
setattr(mtv, '_adding', True)
|
|
|
|
mtv.full_clean()
|
2010-01-15 01:04:53 +08:00
|
|
|
|
|
|
|
def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
|
|
|
|
# Regression test for #12560
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(1):
|
2010-10-12 11:33:19 +08:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', id=123)
|
|
|
|
setattr(mtv, '_adding', True)
|
|
|
|
mtv.full_clean()
|
2010-01-05 11:56:19 +08:00
|
|
|
|
|
|
|
def test_primary_key_unique_check_not_performed_when_not_adding(self):
|
2010-01-15 01:04:53 +08:00
|
|
|
# Regression test for #12132
|
2011-10-15 01:03:08 +08:00
|
|
|
with self.assertNumQueries(0):
|
2010-10-12 11:33:19 +08:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
|
|
mtv.full_clean()
|
2011-01-09 21:26:39 +08:00
|
|
|
|
|
|
|
def test_unique_for_date(self):
|
2013-10-19 20:31:38 +08:00
|
|
|
Post.objects.create(title="Django 1.0 is released",
|
2011-01-09 21:26:39 +08:00
|
|
|
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))
|
2011-06-10 03:35:44 +08:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 21:26:39 +08:00
|
|
|
p.full_clean()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'title': ['Title must be unique for Posted date.']})
|
2011-01-09 21:26:39 +08: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-27 03:15:03 +08:00
|
|
|
p = Post(title="Django 1.0 is released", posted=datetime.datetime(2008, 9, 4))
|
2011-01-09 21:26:39 +08:00
|
|
|
p.full_clean()
|
|
|
|
|
|
|
|
p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
|
2011-06-10 03:35:44 +08:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 21:26:39 +08:00
|
|
|
p.full_clean()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'slug': ['Slug must be unique for Posted year.']})
|
2011-01-09 21:26:39 +08:00
|
|
|
|
|
|
|
p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
|
2011-06-10 03:35:44 +08:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 21:26:39 +08:00
|
|
|
p.full_clean()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'subtitle': ['Subtitle must be unique for Posted month.']})
|
2011-01-09 21:26:39 +08:00
|
|
|
|
|
|
|
p = Post(title="Django 1.0 is released")
|
2011-06-10 03:35:44 +08:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-01-09 21:26:39 +08:00
|
|
|
p.full_clean()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'posted': ['This field cannot be null.']})
|
2011-01-09 21:26:39 +08:00
|
|
|
|
|
|
|
def test_unique_for_date_with_nullable_date(self):
|
2013-10-19 20:31:38 +08:00
|
|
|
FlexibleDatePost.objects.create(title="Django 1.0 is released",
|
2011-01-09 21:26:39 +08:00
|
|
|
slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
|
|
|
|
|
|
|
|
p = FlexibleDatePost(title="Django 1.0 is released")
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
2012-04-29 00:09:37 +08:00
|
|
|
except ValidationError:
|
2011-01-09 21:26:39 +08:00
|
|
|
self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.")
|
|
|
|
|
|
|
|
p = FlexibleDatePost(slug="Django 1.0")
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
2012-04-29 00:09:37 +08:00
|
|
|
except ValidationError:
|
2011-01-09 21:26:39 +08:00
|
|
|
self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
|
|
|
|
|
|
|
|
p = FlexibleDatePost(subtitle="Finally")
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
2012-04-29 00:09:37 +08:00
|
|
|
except ValidationError:
|
2011-01-09 21:26:39 +08:00
|
|
|
self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
|
2011-06-09 23:05:13 +08:00
|
|
|
|
|
|
|
def test_unique_errors(self):
|
2013-10-19 20:31:38 +08:00
|
|
|
UniqueErrorsModel.objects.create(name='Some Name', no=10)
|
2011-07-04 06:15:39 +08:00
|
|
|
m = UniqueErrorsModel(name='Some Name', no=11)
|
2011-06-10 03:35:44 +08:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-06-09 23:05:13 +08:00
|
|
|
m.full_clean()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'name': ['Custom unique name message.']})
|
2011-06-09 23:05:13 +08:00
|
|
|
|
2011-07-04 06:15:39 +08:00
|
|
|
m = UniqueErrorsModel(name='Some Other Name', no=10)
|
2011-06-10 03:35:44 +08:00
|
|
|
with self.assertRaises(ValidationError) as cm:
|
2011-06-09 23:05:13 +08:00
|
|
|
m.full_clean()
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'no': ['Custom unique number message.']})
|