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