Ticket #15130: 15130.3-tests.diff

File 15130.3-tests.diff, 10.0 KB (added by Ramiro Morales, 14 years ago)

New tests

  • tests/modeltests/validation/models.py

    diff -r 9d422208c684 tests/modeltests/validation/models.py
    a b  
    11from datetime import datetime
    22from django.core.exceptions import ValidationError
    33from django.db import models
    4 from django.test import TestCase
    54
    65
    76def validate_answer_to_universe(value):
     
    4544    order = models.IntegerField(unique_for_month="end_date")
    4645    name = models.CharField(max_length=100)
    4746
     47class UniqueForXModel(models.Model):
     48    datetime1 = models.DateTimeField()
     49    date1 = models.DateField()
     50    date2 = models.DateField()
     51    udate = models.IntegerField(unique_for_date="datetime1")
     52    umonth = models.IntegerField(unique_for_month="date1")
     53    uyear = models.IntegerField(unique_for_year="date2")
     54    name = models.CharField(max_length=20)
     55
    4856class CustomMessagesModel(models.Model):
    4957    other  = models.IntegerField(blank=True, null=True)
    5058    number = models.IntegerField(db_column='number_val',
  • tests/modeltests/validation/test_unique.py

    diff -r 9d422208c684 tests/modeltests/validation/test_unique.py
    a b  
    11import datetime
    22
    3 from django.conf import settings
    43from django.core.exceptions import ValidationError
    5 from django.db import connection
    64from django.test import TestCase
    75from django.utils import unittest
    86
    97from models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
    10     UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost)
     8    UniqueForDateModel, ModelToValidate, Post, FlexibleDatePost, UniqueForXModel)
    119
    1210
    1311class GetUniqueCheckTests(unittest.TestCase):
     
    149147            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
    150148        except:
    151149            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
     150
     151class UniqueMultiDbIsolationTests(TestCase):
     152    multi_db = True
     153
     154    def test_unique_order1(self):
     155        "Attempt to save two model instances that don't pass Field.unique checks to different DBs."
     156        self._test_unique('default', 'other')
     157
     158    def test_unique_order2(self):
     159        "Attempt to save two model instances that don't pass Field.unique checks to different DBs."
     160        self._test_unique('other', 'default')
     161
     162    def test_unique_together_order1(self):
     163        "Attempt to save two model instances that don't pass Meta.unique_together checks to different DBs."
     164        self._test_unique_together('default', 'other')
     165
     166    def test_unique_together_order2(self):
     167        "Attempt to save two model instances that don't pass Meta.unique_together checks to different DBs."
     168        self._test_unique_together('other', 'default')
     169
     170    def test_unique_for_date_order1(self):
     171        "Attempt to save two model instances that don't pass Field.unique_for_date checks to different DBs."
     172        self._test_unique_for_date('default', 'other')
     173
     174    def test_unique_for_date_order2(self):
     175        "Attempt to save two model instances that don't pass Field.unique_for_date checks to different DBs."
     176        self._test_unique_for_date('other', 'default')
     177
     178    def test_unique_for_month_order1(self):
     179        "Attempt to save two model instances that don't pass Field.unique_for_month checks to different DBs."
     180        self._test_unique_for_month('default', 'other')
     181
     182    def test_unique_for_month_order2(self):
     183        "Attempt to save two model instances that don't pass Field.unique_for_month checks to different DBs."
     184        self._test_unique_for_month('other', 'default')
     185
     186    def test_unique_for_year_order1(self):
     187        "Attempt to save two model instances that don't pass Field.unique_for_year checks to different DBs."
     188        self._test_unique_for_year('default', 'other')
     189
     190    def test_unique_for_year_order2(self):
     191        "Attempt to save two model instances that don't pass Field.unique_for_year checks to different DBs."
     192        self._test_unique_for_year('other', 'default')
     193
     194    def _test_unique(self, first_db, second_db):
     195        # Create a model instance in the first DB
     196        UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3).save(using=first_db)
     197        # Another different instance in the second DB so its internal state points to such DB
     198        b = UniqueFieldsModel(unique_charfield='Bye world', unique_integerfield=0, non_unique_field=3)
     199        b.save(using=second_db)
     200        # Modify it to clash with the first instance
     201        b.unique_charfield = 'Hello world'
     202        b.unique_integerfield = 42
     203        b.pk = None
     204        try:
     205            b.full_clean()
     206        except ValidationError, e:
     207            self.fail("unique field validation shouldn't erroneosuly trigger when the identical model instances are on different databases. Got: %s" % e)
     208        else:
     209            b.save()
     210            self.assertEqual(UniqueFieldsModel.objects.using(first_db).count(), 1)
     211            self.assertEqual(UniqueFieldsModel.objects.using(second_db).count(), 2)
     212
     213    def _test_unique_together(self, first_db, second_db):
     214        # Create a model instance in the first DB
     215        UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org').save(using=first_db)
     216        # Another different instance in the second DB so its internal state points to such DB
     217        b = UniqueTogetherModel(cfield='Bye world', ifield=0, efield='user@example.com')
     218        b.save(using=second_db)
     219        # Modify it to clash with the first instance
     220        b.cfield = 'Hello world'
     221        b.ifield = 42
     222        b.pk = None
     223        try:
     224            b.full_clean()
     225        except ValidationError, e:
     226            self.fail("Meta.unique_together validation shouldn't erroneosuly trigger when the identical model instances are on different databases. Got: %s" % e)
     227        else:
     228            b.save()
     229            self.assertEqual(UniqueTogetherModel.objects.using(first_db).count(), 1)
     230            self.assertEqual(UniqueTogetherModel.objects.using(second_db).count(), 2)
     231
     232    def _test_unique_for_date(self, first_db, second_db):
     233        today = datetime.date.today()
     234        now = datetime.datetime.now()
     235        # Create a model instance in the first DB
     236        UniqueForXModel(datetime1=now, date1=today, date2=today, udate=10, umonth=20, uyear=30, name='Foo').save(using=first_db)
     237        # Another different instance in the second DB so its internal state points to such DB
     238        b = UniqueForXModel(datetime1=now, date1=today, date2=today, udate=0, umonth=0, uyear=0, name='Bar')
     239        b.save(using=second_db)
     240        # Modify it to clash with the first instance
     241        b.udate = 10
     242        b.umonth = 2
     243        b.uyear = 3
     244        b.pk = None
     245        try:
     246            b.full_clean()
     247        except ValidationError, e:
     248            self.fail("Meta.unique_for_date validation shouldn't erroneosuly trigger when the identical model instances are on different databases. Got: %s" % e)
     249        else:
     250            b.save()
     251            self.assertEqual(UniqueForXModel.objects.using(first_db).count(), 1)
     252            self.assertEqual(UniqueForXModel.objects.using(second_db).count(), 2)
     253
     254    def _test_unique_for_month(self, first_db, second_db):
     255        today = datetime.date.today()
     256        now = datetime.datetime.now()
     257        # Create a model instance in the first DB
     258        UniqueForXModel(datetime1=now, date1=today, date2=today, udate=10, umonth=20, uyear=30, name='Foo').save(using=first_db)
     259        # Another different instance in the second DB so its internal state points to such DB
     260        b = UniqueForXModel(datetime1=now, date1=today, date2=today, udate=0, umonth=0, uyear=0, name='Bar')
     261        b.save(using=second_db)
     262        # Modify it to clash with the first instance
     263        b.udate = 1
     264        b.umonth = 20
     265        b.uyear = 3
     266        b.pk = None
     267        try:
     268            b.full_clean()
     269        except ValidationError, e:
     270            self.fail("Meta.unique_for_month validation shouldn't erroneosuly trigger when the identical model instances are on different databases. Got: %s" % e)
     271        else:
     272            b.save()
     273            self.assertEqual(UniqueForXModel.objects.using(first_db).count(), 1)
     274            self.assertEqual(UniqueForXModel.objects.using(second_db).count(), 2)
     275
     276    def _test_unique_for_year(self, first_db, second_db):
     277        today = datetime.date.today()
     278        now = datetime.datetime.now()
     279        # Create a model instance in the first DB
     280        UniqueForXModel(datetime1=now, date1=today, date2=today, udate=10, umonth=20, uyear=30, name='Foo').save(using=first_db)
     281        # Another different instance in the second DB so its internal state points to such DB
     282        b = UniqueForXModel(datetime1=now, date1=today, date2=today, udate=0, umonth=0, uyear=0, name='Bar')
     283        b.save(using=second_db)
     284        # Modify it to clash with the first instance
     285        b.udate = 1
     286        b.umonth = 2
     287        b.uyear = 30
     288        b.pk = None
     289        try:
     290            b.full_clean()
     291        except ValidationError, e:
     292            self.fail("Meta.unique_for_year validation shouldn't erroneosuly trigger when the identical model instances are on different databases. Got: %s" % e)
     293        else:
     294            b.save()
     295            self.assertEqual(UniqueForXModel.objects.using(first_db).count(), 1)
     296            self.assertEqual(UniqueForXModel.objects.using(second_db).count(), 2)
  • tests/modeltests/validation/tests.py

    diff -r 9d422208c684 tests/modeltests/validation/tests.py
    a b  
    77# Import other tests for this package.
    88from modeltests.validation.validators import TestModelsWithValidators
    99from modeltests.validation.test_unique import (GetUniqueCheckTests,
    10     PerformUniqueChecksTest)
     10    PerformUniqueChecksTest, UniqueMultiDbIsolationTests)
    1111from modeltests.validation.test_custom_messages import CustomMessagesTest
    1212
    1313
Back to Top