PageRenderTime 142ms CodeModel.GetById 121ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/validation/test_unique.py

https://code.google.com/p/mango-py/
Python | 151 lines | 124 code | 22 blank | 5 comment | 0 complexity | 76320afb7ad7162dac1fd1f7cfb2bbc3 MD5 | raw file
  1import datetime
  2
  3from django.conf import settings
  4from django.core.exceptions import ValidationError
  5from django.db import connection
  6from django.test import TestCase
  7from django.utils import unittest
  8
  9from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
 10    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
 11
 12
 13class GetUniqueCheckTests(unittest.TestCase):
 14    def test_unique_fields_get_collected(self):
 15        m = UniqueFieldsModel()
 16        self.assertEqual(
 17            ([(UniqueFieldsModel, ('id',)),
 18              (UniqueFieldsModel, ('unique_charfield',)),
 19              (UniqueFieldsModel, ('unique_integerfield',))],
 20             []),
 21            m._get_unique_checks()
 22        )
 23
 24    def test_unique_together_gets_picked_up_and_converted_to_tuple(self):
 25        m = UniqueTogetherModel()
 26        self.assertEqual(
 27            ([(UniqueTogetherModel, ('ifield', 'cfield',)),
 28              (UniqueTogetherModel, ('ifield', 'efield')),
 29              (UniqueTogetherModel, ('id',)), ],
 30             []),
 31            m._get_unique_checks()
 32        )
 33
 34    def test_primary_key_is_considered_unique(self):
 35        m = CustomPKModel()
 36        self.assertEqual(([(CustomPKModel, ('my_pk_field',))], []), m._get_unique_checks())
 37
 38    def test_unique_for_date_gets_picked_up(self):
 39        m = UniqueForDateModel()
 40        self.assertEqual((
 41            [(UniqueForDateModel, ('id',))],
 42            [(UniqueForDateModel, 'date', 'count', 'start_date'),
 43             (UniqueForDateModel, 'year', 'count', 'end_date'),
 44             (UniqueForDateModel, 'month', 'order', 'end_date')]
 45            ), m._get_unique_checks()
 46        )
 47
 48    def test_unique_for_date_exclusion(self):
 49        m = UniqueForDateModel()
 50        self.assertEqual((
 51            [(UniqueForDateModel, ('id',))],
 52            [(UniqueForDateModel, 'year', 'count', 'end_date'),
 53             (UniqueForDateModel, 'month', 'order', 'end_date')]
 54            ), m._get_unique_checks(exclude='start_date')
 55        )
 56
 57class PerformUniqueChecksTest(TestCase):
 58    def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified(self):
 59        # Regression test for #12560
 60        def test():
 61            mtv = ModelToValidate(number=10, name='Some Name')
 62            setattr(mtv, '_adding', True)
 63            mtv.full_clean()
 64        self.assertNumQueries(0, test)
 65
 66    def test_primary_key_unique_check_performed_when_adding_and_pk_specified(self):
 67        # Regression test for #12560
 68        def test():
 69            mtv = ModelToValidate(number=10, name='Some Name', id=123)
 70            setattr(mtv, '_adding', True)
 71            mtv.full_clean()
 72        self.assertNumQueries(1, test)
 73
 74    def test_primary_key_unique_check_not_performed_when_not_adding(self):
 75        # Regression test for #12132
 76        def test():
 77            mtv = ModelToValidate(number=10, name='Some Name')
 78            mtv.full_clean()
 79        self.assertNumQueries(0, test)
 80
 81    def test_unique_for_date(self):
 82        p1 = Post.objects.create(title="Django 1.0 is released",
 83            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
 84
 85        p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
 86        try:
 87            p.full_clean()
 88        except ValidationError, e:
 89            self.assertEqual(e.message_dict, {'title': [u'Title must be unique for Posted date.']})
 90        else:
 91            self.fail('unique_for_date checks should catch this.')
 92
 93        # Should work without errors
 94        p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3))
 95        p.full_clean()
 96
 97        # Should work without errors
 98        p = Post(title="Django 1.0 is released", posted=datetime.datetime(2008, 9,4))
 99        p.full_clean()
100
101        p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
102        try:
103            p.full_clean()
104        except ValidationError, e:
105            self.assertEqual(e.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
106        else:
107            self.fail('unique_for_year checks should catch this.')
108
109        p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
110        try:
111            p.full_clean()
112        except ValidationError, e:
113            self.assertEqual(e.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
114        else:
115            self.fail('unique_for_month checks should catch this.')
116
117        p = Post(title="Django 1.0 is released")
118        try:
119            p.full_clean()
120        except ValidationError, e:
121            self.assertEqual(e.message_dict, {'posted': [u'This field cannot be null.']})
122        else:
123            self.fail("Model validation shouldn't allow an absent value for a DateField without null=True.")
124
125    def test_unique_for_date_with_nullable_date(self):
126        p1 = FlexibleDatePost.objects.create(title="Django 1.0 is released",
127            slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
128
129        p = FlexibleDatePost(title="Django 1.0 is released")
130        try:
131            p.full_clean()
132        except ValidationError, e:
133            self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.")
134        except:
135            self.fail("unique_for_date checks shouldn't explode when the associated DateField is None.")
136
137        p = FlexibleDatePost(slug="Django 1.0")
138        try:
139            p.full_clean()
140        except ValidationError, e:
141            self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
142        except:
143            self.fail("unique_for_year checks shouldn't explode when the associated DateField is None.")
144
145        p = FlexibleDatePost(subtitle="Finally")
146        try:
147            p.full_clean()
148        except ValidationError, e:
149            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
150        except:
151            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")