mirror of
https://github.com/django/django.git
synced 2024-11-19 07:54:07 +00:00
225c413b08
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12403 bcc190cf-cafb-0310-a4f2-bffc1f526a37
69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
import unittest
|
|
import datetime
|
|
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(
|
|
([('id',), ('unique_charfield',), ('unique_integerfield',)], []),
|
|
m._get_unique_checks()
|
|
)
|
|
|
|
def test_unique_together_gets_picked_up_and_converted_to_tuple(self):
|
|
m = UniqueTogetherModel()
|
|
self.assertEqual(
|
|
([('ifield', 'cfield',),('ifield', 'efield'), ('id',), ], []),
|
|
m._get_unique_checks()
|
|
)
|
|
|
|
def test_primary_key_is_considered_unique(self):
|
|
m = CustomPKModel()
|
|
self.assertEqual(([('my_pk_field',)], []), m._get_unique_checks())
|
|
|
|
def test_unique_for_date_gets_picked_up(self):
|
|
m = UniqueForDateModel()
|
|
self.assertEqual((
|
|
[('id',)],
|
|
[('date', 'count', 'start_date'), ('year', 'count', 'end_date'), ('month', 'order', 'end_date')]
|
|
), m._get_unique_checks()
|
|
)
|
|
|
|
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()
|
|
|
|
def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self):
|
|
# Regression test for #12560
|
|
query_count = len(connection.queries)
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
setattr(mtv, '_adding', True)
|
|
mtv.full_clean()
|
|
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))
|
|
|
|
def test_primary_key_unique_check_not_performed_when_not_adding(self):
|
|
# Regression test for #12132
|
|
query_count= len(connection.queries)
|
|
mtv = ModelToValidate(number=10, name='Some Name')
|
|
mtv.full_clean()
|
|
self.assertEqual(query_count, len(connection.queries))
|
|
|