mirror of
https://github.com/django/django.git
synced 2025-07-07 03:09:22 +00:00
[soc2009/model-validation] Tests for Model._get_unique_checks
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10883 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d484d021f2
commit
ea52757c10
@ -621,7 +621,7 @@ class Model(object):
|
||||
def _get_unique_checks(self):
|
||||
from django.db.models.fields import FieldDoesNotExist, Field as ModelField
|
||||
|
||||
unique_checks = self._meta.unique_together[:]
|
||||
unique_checks = list(self._meta.unique_together)
|
||||
# these are checks for the unique_for_<date/year/month>
|
||||
date_checks = []
|
||||
|
||||
|
@ -15,3 +15,25 @@ class ModelToValidate(models.Model):
|
||||
if self.number == 11:
|
||||
raise ValidationError('Invalid number supplied!')
|
||||
|
||||
class UniqueFieldsModel(models.Model):
|
||||
unique_charfield = models.CharField(max_length=100, unique=True)
|
||||
unique_integerfield = models.IntegerField(unique=True)
|
||||
non_unique_field = models.IntegerField()
|
||||
|
||||
class CustomPKModel(models.Model):
|
||||
my_pk_field = models.CharField(max_length=100, primary_key=True)
|
||||
|
||||
class UniqueTogetherModel(models.Model):
|
||||
cfield = models.CharField(max_length=100)
|
||||
ifield = models.IntegerField()
|
||||
efield = models.EmailField()
|
||||
|
||||
class Meta:
|
||||
unique_together = (('ifield', 'cfield',),('ifield', 'efield'), )
|
||||
|
||||
class UniqueForDateModel(models.Model):
|
||||
start_date = models.DateField()
|
||||
end_date = models.DateTimeField()
|
||||
count = models.IntegerField(unique_for_date="start_date", unique_for_year="end_date")
|
||||
order = models.IntegerField(unique_for_month="end_date")
|
||||
name = models.CharField(max_length=100)
|
||||
|
@ -1,7 +1,8 @@
|
||||
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
|
||||
from django.test import TestCase
|
||||
from django.db import models
|
||||
|
||||
from models import ModelToValidate
|
||||
from models import *
|
||||
|
||||
class BaseModelValidationTests(TestCase):
|
||||
def test_missing_required_field_raises_error(self):
|
||||
@ -37,3 +38,25 @@ class BaseModelValidationTests(TestCase):
|
||||
mtv=ModelToValidate(number=10, name='Some Name', parent_id=parent.pk)
|
||||
self.assertEqual(None, mtv.clean())
|
||||
|
||||
class GetUniqueCheckTests(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(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()
|
||||
)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user