diff options
Diffstat (limited to 'tests/schema/tests.py')
| -rw-r--r-- | tests/schema/tests.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 688a9f1fcf..5c20155387 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -3318,6 +3318,43 @@ class SchemaTests(TransactionTestCase): with self.assertRaises(DatabaseError): editor.add_constraint(Author, constraint) + @skipUnlessDBFeature("supports_nulls_distinct_unique_constraints") + def test_unique_constraint_nulls_distinct(self): + with connection.schema_editor() as editor: + editor.create_model(Author) + nulls_distinct = UniqueConstraint( + F("height"), name="distinct_height", nulls_distinct=True + ) + nulls_not_distinct = UniqueConstraint( + F("weight"), name="not_distinct_weight", nulls_distinct=False + ) + with connection.schema_editor() as editor: + editor.add_constraint(Author, nulls_distinct) + editor.add_constraint(Author, nulls_not_distinct) + Author.objects.create(name="", height=None, weight=None) + Author.objects.create(name="", height=None, weight=1) + with self.assertRaises(IntegrityError): + Author.objects.create(name="", height=1, weight=None) + with connection.schema_editor() as editor: + editor.remove_constraint(Author, nulls_distinct) + editor.remove_constraint(Author, nulls_not_distinct) + constraints = self.get_constraints(Author._meta.db_table) + self.assertNotIn(nulls_distinct.name, constraints) + self.assertNotIn(nulls_not_distinct.name, constraints) + + @skipIfDBFeature("supports_nulls_distinct_unique_constraints") + def test_unique_constraint_nulls_distinct_unsupported(self): + # UniqueConstraint is ignored on databases that don't support + # NULLS [NOT] DISTINCT. + with connection.schema_editor() as editor: + editor.create_model(Author) + constraint = UniqueConstraint( + F("name"), name="func_name_uq", nulls_distinct=True + ) + with connection.schema_editor() as editor, self.assertNumQueries(0): + self.assertIsNone(editor.add_constraint(Author, constraint)) + self.assertIsNone(editor.remove_constraint(Author, constraint)) + @ignore_warnings(category=RemovedInDjango51Warning) def test_index_together(self): """ |
