2010-01-12 10:29:45 +08:00
|
|
|
import datetime
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.conf import settings
|
2011-01-09 21:26:39 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.db import connection
|
2010-10-12 11:33:19 +08:00
|
|
|
from django.test import TestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
|
|
|
|
2010-10-12 11:33:19 +08:00
|
|
|
from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
|
2011-06-09 23:05:13 +08:00
|
|
|
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(
|
2010-03-17 03:32:11 +08:00
|
|
|
([(UniqueTogetherModel, ('ifield', 'cfield',)),
|
|
|
|
(UniqueTogetherModel, ('ifield', 'efield')),
|
|
|
|
(UniqueTogetherModel, ('id',)), ],
|
|
|
|
[]),
|
2010-01-05 11:56:19 +08:00
|
|
|
m._get_unique_checks()
|
|
|
|
)
|
|
|
|
|
|
|
|
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')]
|
2010-01-05 11:56:19 +08:00
|
|
|
), m._get_unique_checks()
|
|
|
|
)
|
|
|
|
|
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')]
|
|
|
|
), m._get_unique_checks(exclude='start_date')
|
|
|
|
)
|
|
|
|
|
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
|
2010-10-12 11:33:19 +08:00
|
|
|
def test():
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
|
|
setattr(mtv, '_adding', True)
|
|
|
|
mtv.full_clean()
|
|
|
|
self.assertNumQueries(0, test)
|
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
|
2010-10-12 11:33:19 +08:00
|
|
|
def test():
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', id=123)
|
|
|
|
setattr(mtv, '_adding', True)
|
|
|
|
mtv.full_clean()
|
|
|
|
self.assertNumQueries(1, test)
|
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
|
2010-10-12 11:33:19 +08:00
|
|
|
def test():
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
|
|
mtv.full_clean()
|
|
|
|
self.assertNumQueries(0, test)
|
2011-01-09 21:26:39 +08: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))
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.assertEqual(e.message_dict, {'title': [u'Title must be unique for Posted date.']})
|
|
|
|
else:
|
|
|
|
self.fail('unique_for_date checks should catch this.')
|
|
|
|
|
|
|
|
# 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))
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.assertEqual(e.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
|
|
|
|
else:
|
|
|
|
self.fail('unique_for_year checks should catch this.')
|
|
|
|
|
|
|
|
p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.assertEqual(e.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
|
|
|
|
else:
|
|
|
|
self.fail('unique_for_month checks should catch this.')
|
|
|
|
|
|
|
|
p = Post(title="Django 1.0 is released")
|
|
|
|
try:
|
|
|
|
p.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.assertEqual(e.message_dict, {'posted': [u'This field cannot be null.']})
|
|
|
|
else:
|
|
|
|
self.fail("Model validation shouldn't allow an absent value for a DateField without null=True.")
|
|
|
|
|
|
|
|
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.")
|
|
|
|
except:
|
|
|
|
self.fail("unique_for_date checks shouldn't explode 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.")
|
|
|
|
except:
|
|
|
|
self.fail("unique_for_year checks shouldn't explode 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.")
|
|
|
|
except:
|
|
|
|
self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
|
2011-06-09 23:05:13 +08:00
|
|
|
|
|
|
|
def test_unique_errors(self):
|
|
|
|
m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10)
|
|
|
|
m = UniqueErrorsModel(name='Some Name', number=11)
|
|
|
|
try:
|
|
|
|
m.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.assertEqual(e.message_dict, {'name': [u'Custom unique name message.']})
|
|
|
|
except:
|
|
|
|
self.fail('unique checks should catch this.')
|
|
|
|
|
|
|
|
m = UniqueErrorsModel(name='Some Other Name', number=10)
|
|
|
|
try:
|
|
|
|
m.full_clean()
|
|
|
|
except ValidationError, e:
|
|
|
|
self.assertEqual(e.message_dict, {'number': [u'Custom unique number message.']})
|
|
|
|
except:
|
|
|
|
self.fail('unique checks should catch this.')
|
|
|
|
|