2010-01-05 11:56:19 +08:00
|
|
|
import unittest
|
2010-01-12 10:29:45 +08:00
|
|
|
import datetime
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import connection
|
|
|
|
from models import CustomPKModel, UniqueTogetherModel, UniqueFieldsModel, UniqueForDateModel, ModelToValidate
|
|
|
|
|
|
|
|
|
|
|
|
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-01-05 11:56:19 +08:00
|
|
|
class PerformUniqueChecksTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Set debug to True to gain access to connection.queries.
|
|
|
|
self._old_debug, settings.DEBUG = settings.DEBUG, True
|
|
|
|
super(PerformUniqueChecksTest, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Restore old debug value.
|
|
|
|
settings.DEBUG = self._old_debug
|
|
|
|
super(PerformUniqueChecksTest, self).tearDown()
|
|
|
|
|
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
|
|
|
|
query_count = len(connection.queries)
|
2010-01-05 11:56:19 +08:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
|
|
setattr(mtv, '_adding', True)
|
2010-01-12 10:29:45 +08:00
|
|
|
mtv.full_clean()
|
2010-01-15 01:04:53 +08:00
|
|
|
self.assertEqual(query_count, len(connection.queries))
|
|
|
|
|
|
|
|
def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
|
|
|
|
# Regression test for #12560
|
|
|
|
query_count = len(connection.queries)
|
|
|
|
mtv = ModelToValidate(number=10, name='Some Name', id=123)
|
|
|
|
setattr(mtv, '_adding', True)
|
|
|
|
mtv.full_clean()
|
|
|
|
self.assertEqual(query_count + 1, len(connection.queries))
|
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
|
|
|
|
query_count= len(connection.queries)
|
2010-01-05 11:56:19 +08:00
|
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
2010-01-12 10:29:45 +08:00
|
|
|
mtv.full_clean()
|
2010-01-15 01:04:53 +08:00
|
|
|
self.assertEqual(query_count, len(connection.queries))
|
2010-01-12 10:29:45 +08:00
|
|
|
|