summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2012-09-07 15:40:59 -0400
committerAndrew Godwin <andrew@aeracode.org>2012-09-07 15:40:59 -0400
commitca9c3cd39fade827cced1b5198dd37bb80c208b0 (patch)
tree8a0a7d7c048d33798efa5baed8b51140a774bc90 /tests
parent375178fc19c1170fe046ad26befeba02fc19548c (diff)
Add check constraint support - needed a few Field changes
Diffstat (limited to 'tests')
-rw-r--r--tests/modeltests/schema/models.py1
-rw-r--r--tests/modeltests/schema/tests.py50
2 files changed, 51 insertions, 0 deletions
diff --git a/tests/modeltests/schema/models.py b/tests/modeltests/schema/models.py
index 76a8cf3687..f3d6d09e2e 100644
--- a/tests/modeltests/schema/models.py
+++ b/tests/modeltests/schema/models.py
@@ -7,6 +7,7 @@ from django.db import models
class Author(models.Model):
name = models.CharField(max_length=255)
+ height = models.PositiveIntegerField(null=True, blank=True)
class Meta:
managed = False
diff --git a/tests/modeltests/schema/tests.py b/tests/modeltests/schema/tests.py
index b3fc5d1c80..7d8602eff7 100644
--- a/tests/modeltests/schema/tests.py
+++ b/tests/modeltests/schema/tests.py
@@ -347,6 +347,56 @@ class SchemaTests(TestCase):
else:
self.fail("No FK constraint for tag_id found")
+ @skipUnless(connection.features.supports_check_constraints, "No check constraints")
+ def test_check_constraints(self):
+ """
+ Tests creating/deleting CHECK constraints
+ """
+ # Create the tables
+ editor = connection.schema_editor()
+ editor.start()
+ editor.create_model(Author)
+ editor.commit()
+ # Ensure the constraint exists
+ constraints = connection.introspection.get_constraints(connection.cursor(), Author._meta.db_table)
+ for name, details in constraints.items():
+ if details['columns'] == set(["height"]) and details['check']:
+ break
+ else:
+ self.fail("No check constraint for height found")
+ # Alter the column to remove it
+ new_field = IntegerField(null=True, blank=True)
+ new_field.set_attributes_from_name("height")
+ editor = connection.schema_editor()
+ editor.start()
+ editor.alter_field(
+ Author,
+ Author._meta.get_field_by_name("height")[0],
+ new_field,
+ strict = True,
+ )
+ editor.commit()
+ constraints = connection.introspection.get_constraints(connection.cursor(), Author._meta.db_table)
+ for name, details in constraints.items():
+ if details['columns'] == set(["height"]) and details['check']:
+ self.fail("Check constraint for height found")
+ # Alter the column to re-add it
+ editor = connection.schema_editor()
+ editor.start()
+ editor.alter_field(
+ Author,
+ new_field,
+ Author._meta.get_field_by_name("height")[0],
+ strict = True,
+ )
+ editor.commit()
+ constraints = connection.introspection.get_constraints(connection.cursor(), Author._meta.db_table)
+ for name, details in constraints.items():
+ if details['columns'] == set(["height"]) and details['check']:
+ break
+ else:
+ self.fail("No check constraint for height found")
+
def test_unique(self):
"""
Tests removing and adding unique constraints to a single column.