def test_cannot_insert_duplicate_row(self): """Inserting a duplicate rows shouldn't work.""" self.model_class.objects.create(f1='a', f2='b') many_to_many_set(self.ut, 'field_defs', [self.f1, self.f2]) with captured_stderr(): with self.assertRaises(IntegrityError): with transaction.atomic(): self.model_class.objects.create(f1='a', f2='b')
def test_clear_removes_unique(self): """ Removing a unique constraint should relax duplicate row validation """ self.model_class.objects.create(f1='a', f2='b') many_to_many_set(self.ut, 'field_defs', [self.f1, self.f2]) self.ut.field_defs.clear() self.model_class.objects.create(f1='a', f2='b')
def test_cannot_create_unique(self): """Creating a unique key on a table with duplicate rows shouldn't work""" self.model_class.objects.create(f1='a', f2='b') self.model_class.objects.create(f1='a', f2='b') with captured_stderr(): with self.assertRaises(IntegrityError): with transaction.atomic(): many_to_many_set(self.ut, 'field_defs', [self.f1, self.f2])
def test_db_column(self): """Make sure a unique index creation works correctly when using a custom `db_column`. This is needed for unique FK's columns.""" self.f2.db_column = 'f2_column' self.f2.save() many_to_many_set(self.ut, 'field_defs', [self.f1, self.f2]) self.f2.db_column = 'f2' self.f2.save() self.ut.delete()
def test_cannot_remove_unique(self): """Removing a unique constraint that cause duplicate rows shouldn't work.""" many_to_many_set(self.ut, 'field_defs', [self.f1, self.f2]) self.model_class.objects.create(f1='a', f2='b') self.model_class.objects.create(f1='a', f2='c') with captured_stderr(): with self.assertRaises(IntegrityError): with transaction.atomic(): self.ut.field_defs.remove(self.f2)
def test_clean(self): """Make sure we can't create a unique key with two fields of two different models""" other_model_def = ModelDefinition.objects.create( app_label='mutant', object_name='OtherModel') with self.assertChecksumChange(other_model_def): f2 = CharFieldDefinition.objects.create(model_def=other_model_def, name='f2', max_length=25) many_to_many_set(self.ut, 'field_defs', [self.f1, f2]) self.assertRaises(ValidationError, self.ut.clean)
def test_clean(self): """Make sure we can't create a unique key with two fields of two different models""" other_model_def = ModelDefinition.objects.create( app_label='mutant', object_name='OtherModel' ) with self.assertChecksumChange(other_model_def): f2 = CharFieldDefinition.objects.create( model_def=other_model_def, name='f2', max_length=25 ) many_to_many_set(self.ut, 'field_defs', [self.f1, f2]) self.assertRaises(ValidationError, self.ut.clean)