| 152 | |
| 153 | class MultiDbUniqueTests(TestCase): |
| 154 | multi_db = True |
| 155 | |
| 156 | def test_unique_checks_multi_db_isolation_unique(self): |
| 157 | UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3).save(using='default') |
| 158 | m = UniqueFieldsModel(unique_charfield='Hello world', unique_integerfield=42, non_unique_field=3) |
| 159 | m._state.db = 'other' |
| 160 | try: |
| 161 | m.full_clean() |
| 162 | except ValidationError: |
| 163 | self.fail("unique field validation shouldn't erroneosuly trigger when the identical model instances are on different databases.") |
| 164 | else: |
| 165 | m.save() |
| 166 | self.assertEqual(UniqueFieldsModel.objects.using('default').count(), 1) |
| 167 | self.assertEqual(UniqueFieldsModel.objects.using('other').count(), 1) |
| 168 | |
| 169 | def test_unique_checks_multi_db_isolation_unique_together(self): |
| 170 | UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org').save(using='default') |
| 171 | m = UniqueTogetherModel(cfield='Hello world', ifield=42, efield='user@example.org') |
| 172 | m._state.db = 'other' |
| 173 | try: |
| 174 | m.full_clean() |
| 175 | except ValidationError: |
| 176 | self.fail("Meta.unique_together validation shouldn't erroneosuly trigger when the identical model instances are on different databases.") |
| 177 | else: |
| 178 | m.save() |
| 179 | self.assertEqual(UniqueTogetherModel.objects.using('default').count(), 1) |
| 180 | self.assertEqual(UniqueTogetherModel.objects.using('other').count(), 1) |
| 181 | |
| 182 | def test_unique_checks_multi_db_isolation_unique_for_x(self): |
| 183 | today = datetime.date.today() |
| 184 | now = datetime.datetime.now() |
| 185 | UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo').save(using='default') |
| 186 | m = UniqueForDateModel(start_date=today, end_date=now, count=314, order=21, name='Foo') |
| 187 | m._state.db = 'other' |
| 188 | try: |
| 189 | m.full_clean() |
| 190 | except ValidationError: |
| 191 | self.fail("Meta.unique_for_* validation shouldn't erroneosuly trigger when the identical model instances are on different databases.") |
| 192 | else: |
| 193 | m.save() |
| 194 | self.assertEqual(UniqueForDateModel.objects.using('default').count(), 1) |
| 195 | self.assertEqual(UniqueForDateModel.objects.using('other').count(), 1) |