Ticket #15130: 15130-tests_validate_unique.diff

File 15130-tests_validate_unique.diff, 2.6 KB (added by Tetsuya Morimoto, 14 years ago)

Tests (as an additon to the Django test suite) that occur ValidationError

  • tests.py

     
    77# Import other tests for this package.
    88from modeltests.validation.validators import TestModelsWithValidators
    99from modeltests.validation.test_unique import (GetUniqueCheckTests,
    10     PerformUniqueChecksTest)
     10    PerformUniqueChecksTest, MultiDbUniqueTests)
    1111from modeltests.validation.test_custom_messages import CustomMessagesTest
    1212
    1313
  • test_unique.py

     
    149149            self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
    150150        except:
    151151            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
     152
     153class MultiDbUniqueTests(TestCase):
     154    multi_db = True
     155
     156    def test_unique_checks_multi_db_with_uniqfieldsmodel(self):
     157        UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3).save(using='default')
     158        u = UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3)
     159        u._state.db = 'other'
     160        self.assertEqual(u.validate_unique(), None)
     161        u.save()
     162        self.assertEqual(UniqueFieldsModel.objects.using('default').count(), 1)
     163        self.assertEqual(UniqueFieldsModel.objects.using('other').count(), 1)
     164 
     165    def test_unique_checks_multi_db_with_uniquetogethermodel(self):
     166        UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org').save(using='default')
     167        u = UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org')
     168        u._state.db = 'other'
     169        self.assertEqual(u.validate_unique(), None)
     170        u.save()
     171        self.assertEqual(UniqueTogetherModel.objects.using('default').count(), 1)
     172        self.assertEqual(UniqueTogetherModel.objects.using('other').count(), 1)
     173 
     174    def test_unique_checks_multi_db_with_uniquefordatemodel(self):
     175        today = datetime.date.today()
     176        now = datetime.datetime.now()
     177        UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo').save(using='default')
     178        u = UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo')
     179        u._state.db = 'other'
     180        self.assertEqual(u.validate_unique(), None)
     181        u.save()
     182        self.assertEqual(UniqueForDateModel.objects.using('default').count(), 1)
     183        self.assertEqual(UniqueForDateModel.objects.using('other').count(), 1)
Back to Top