Refs #11964 -- Removed raw SQL from and cleaned up constraint operation tests.

This commit is contained in:
Simon Charette 2018-10-20 13:19:44 -04:00 committed by Tim Graham
parent 58d1e9aa8a
commit 32da3cfdf9
1 changed files with 22 additions and 51 deletions

View File

@ -1778,79 +1778,50 @@ class OperationTests(OperationTestBase):
@skipUnlessDBFeature('supports_table_check_constraints') @skipUnlessDBFeature('supports_table_check_constraints')
def test_add_constraint(self): def test_add_constraint(self):
"""Test the AddConstraint operation.""" project_state = self.set_up_test_model("test_addconstraint")
project_state = self.set_up_test_model('test_addconstraint') gt_check = models.Q(pink__gt=2)
gt_constraint = models.CheckConstraint(check=gt_check, name="test_constraint_pony_pink_gt_2")
where = models.Q(pink__gt=2) gt_operation = migrations.AddConstraint("Pony", gt_constraint)
check_constraint = models.CheckConstraint(check=where, name='test_constraint_pony_pink_gt_2') self.assertEqual(gt_operation.describe(), "Create constraint test_constraint_pony_pink_gt_2 on model Pony")
operation = migrations.AddConstraint('Pony', check_constraint) # Test the state alteration
self.assertEqual(operation.describe(), 'Create constraint test_constraint_pony_pink_gt_2 on model Pony')
new_state = project_state.clone() new_state = project_state.clone()
operation.state_forwards('test_addconstraint', new_state) gt_operation.state_forwards("test_addconstraint", new_state)
self.assertEqual(len(new_state.models['test_addconstraint', 'pony'].options['constraints']), 1) self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 1)
Pony = new_state.apps.get_model("test_addconstraint", "Pony")
# Test database alteration # Test the database alteration
with connection.cursor() as cursor:
with atomic():
cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
cursor.execute("DELETE FROM test_addconstraint_pony")
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_forwards("test_addconstraint", editor, project_state, new_state) gt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
with self.assertRaises(IntegrityError), transaction.atomic():
with connection.cursor() as cursor: Pony.objects.create(pink=1, weight=1.0)
with self.assertRaises(IntegrityError):
cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
# Test reversal # Test reversal
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_backwards("test_addconstraint", editor, new_state, project_state) gt_operation.database_backwards("test_addconstraint", editor, new_state, project_state)
Pony.objects.create(pink=1, weight=1.0)
with connection.cursor() as cursor:
with atomic():
cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
cursor.execute("DELETE FROM test_addconstraint_pony")
# Test deconstruction # Test deconstruction
definition = operation.deconstruct() definition = gt_operation.deconstruct()
self.assertEqual(definition[0], "AddConstraint") self.assertEqual(definition[0], "AddConstraint")
self.assertEqual(definition[1], []) self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': check_constraint}) self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': gt_constraint})
@skipUnlessDBFeature('supports_table_check_constraints') @skipUnlessDBFeature('supports_table_check_constraints')
def test_remove_constraint(self): def test_remove_constraint(self):
"""Test the RemoveConstraint operation."""
project_state = self.set_up_test_model("test_removeconstraint", check_constraint=True) project_state = self.set_up_test_model("test_removeconstraint", check_constraint=True)
self.assertTableExists("test_removeconstraint_pony")
operation = migrations.RemoveConstraint("Pony", "pony_test_constraint") operation = migrations.RemoveConstraint("Pony", "pony_test_constraint")
self.assertEqual(operation.describe(), "Remove constraint pony_test_constraint from model Pony") self.assertEqual(operation.describe(), "Remove constraint pony_test_constraint from model Pony")
# Test state alteration
new_state = project_state.clone() new_state = project_state.clone()
operation.state_forwards("test_removeconstraint", new_state) operation.state_forwards("test_removeconstraint", new_state)
# Test state alteration
self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 0) self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 0)
Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
with connection.cursor() as cursor:
with self.assertRaises(IntegrityError):
cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
# Test database alteration # Test database alteration
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_forwards("test_removeconstraint", editor, project_state, new_state) operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
Pony.objects.create(pink=1, weight=1.0).delete()
with connection.cursor() as cursor:
with atomic():
cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
cursor.execute("DELETE FROM test_removeconstraint_pony")
# Test reversal # Test reversal
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_backwards("test_removeconstraint", editor, new_state, project_state) operation.database_backwards("test_removeconstraint", editor, new_state, project_state)
with self.assertRaises(IntegrityError), transaction.atomic():
with connection.cursor() as cursor: Pony.objects.create(pink=1, weight=1.0)
with self.assertRaises(IntegrityError):
cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
# Test deconstruction # Test deconstruction
definition = operation.deconstruct() definition = operation.deconstruct()
self.assertEqual(definition[0], "RemoveConstraint") self.assertEqual(definition[0], "RemoveConstraint")