2011-10-13 18:04:12 +00:00
|
|
|
from __future__ import with_statement, absolute_import
|
2011-06-10 12:16:56 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
import datetime
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2011-01-09 13:26:39 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2010-10-12 03:33:19 +00:00
|
|
|
from django.test import TestCase
|
2010-10-11 12:55:17 +00:00
|
|
|
from django.utils import unittest
|
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
from .models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
|
|
|
|
UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost,
|
|
|
|
UniqueErrorsModel)
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetUniqueCheckTests(unittest.TestCase):
|
|
|
|
def test_unique_fields_get_collected(self):
|
|
|
|
m = UniqueFieldsModel()
|
|
|
|
self.assertEqual(
|
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(
|
2010-03-16 19:32:11 +00:00
|
|
|
([(UniqueTogetherModel, ('ifield', 'cfield',)),
|
|
|
|
(UniqueTogetherModel, ('ifield', 'efield')),
|
|
|
|
(UniqueTogetherModel, ('id',)), ],
|
|
|
|
[]),
|
2010-01-05 03:56:19 +00:00
|
|
|
m._get_unique_checks()
|
|
|
|
)
|
|
|
|
|
|
|
|
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())
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
def test_unique_for_date_gets_picked_up(self):
|
|
|
|
m = UniqueForDateModel()
|
|
|
|
self.assertEqual((
|
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')]
|
2010-01-05 03:56:19 +00:00
|
|
|
), m._get_unique_checks()
|
|
|
|
)
|
|
|
|
|
2010-08-17 07:07:28 +00:00
|
|
|
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')
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
def test_unique_for_date(self):
|
|
|
|
p1 = 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))
|
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()
|
2011-06-09 19:35:44 +00:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'title': [u'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
|
|
|
|
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))
|
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()
|
2011-06-09 19:35:44 +00:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'slug': [u'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()
|
2011-06-09 19:35:44 +00:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'subtitle': [u'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()
|
2011-06-09 19:35:44 +00:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'posted': [u'This field cannot be null.']})
|
2011-01-09 13:26:39 +00:00
|
|
|
|
|
|
|
def test_unique_for_date_with_nullable_date(self):
|
|
|
|
p1 = 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")
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
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()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
|
|
|
|
|
|
|
|
p = FlexibleDatePost(subtitle="Finally")
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
|
2011-06-09 15:05:13 +00:00
|
|
|
|
|
|
|
def test_unique_errors(self):
|
2011-07-03 22:15:39 +00:00
|
|
|
m1 = UniqueErrorsModel.objects.create(name='Some Name', no=10)
|
|
|
|
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()
|
2011-06-09 19:35:44 +00:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'name': [u'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()
|
2011-07-03 22:15:39 +00:00
|
|
|
self.assertEqual(cm.exception.message_dict, {'no': [u'Custom unique number message.']})
|
2011-06-10 12:16:56 +00:00
|
|
|
|