diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/constraints/tests.py | 15 | ||||
| -rw-r--r-- | tests/schema/tests.py | 28 |
2 files changed, 28 insertions, 15 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py index 28a5c4ba34..b144c24a28 100644 --- a/tests/constraints/tests.py +++ b/tests/constraints/tests.py @@ -1,10 +1,15 @@ -from django.db import IntegrityError, models +from django.db import IntegrityError, connection, models from django.db.models.constraints import BaseConstraint from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature from .models import Product +def get_constraints(table): + with connection.cursor() as cursor: + return connection.introspection.get_constraints(cursor, table) + + class BaseConstraintTests(SimpleTestCase): def test_constraint_sql(self): c = BaseConstraint('name') @@ -37,3 +42,11 @@ class CheckConstraintTests(TestCase): Product.objects.create(name='Valid', price=10, discounted_price=5) with self.assertRaises(IntegrityError): Product.objects.create(name='Invalid', price=10, discounted_price=20) + + @skipUnlessDBFeature('supports_table_check_constraints') + def test_name(self): + constraints = get_constraints(Product._meta.db_table) + expected_name = 'price_gt_discounted_price' + if connection.features.uppercases_column_names: + expected_name = expected_name.upper() + self.assertIn(expected_name, constraints) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 7f170c863e..6f2b6df765 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -2145,29 +2145,29 @@ class SchemaTests(TransactionTestCase): self.assertNotIn(constraint_name, self.get_constraints(model._meta.db_table)) constraint_name = "CamelCaseUniqConstraint" - editor.execute( - editor.sql_create_unique % { - "table": editor.quote_name(table), - "name": editor.quote_name(constraint_name), - "columns": editor.quote_name(field.column), - } - ) + editor.execute(editor._create_unique_sql(model, [field.column], constraint_name)) if connection.features.uppercases_column_names: constraint_name = constraint_name.upper() self.assertIn(constraint_name, self.get_constraints(model._meta.db_table)) editor.alter_field(model, get_field(unique=True), field, strict=True) self.assertNotIn(constraint_name, self.get_constraints(model._meta.db_table)) - if editor.sql_create_fk: + if editor.sql_foreign_key_constraint: constraint_name = "CamelCaseFKConstraint" + fk_sql = editor.sql_foreign_key_constraint % { + "column": editor.quote_name(column), + "to_table": editor.quote_name(table), + "to_column": editor.quote_name(model._meta.auto_field.column), + "deferrable": connection.ops.deferrable_sql(), + } + constraint_sql = editor.sql_constraint % { + "name": editor.quote_name(constraint_name), + "constraint": fk_sql, + } editor.execute( - editor.sql_create_fk % { + editor.sql_create_constraint % { "table": editor.quote_name(table), - "name": editor.quote_name(constraint_name), - "column": editor.quote_name(column), - "to_table": editor.quote_name(table), - "to_column": editor.quote_name(model._meta.auto_field.column), - "deferrable": connection.ops.deferrable_sql(), + "constraint": constraint_sql, } ) if connection.features.uppercases_column_names: |
