MCPcopy
hub / github.com/django/django / test_composite_func_unique_constraint

Method test_composite_func_unique_constraint

tests/schema/tests.py:3593–3620  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3591
3592 @skipUnlessDBFeature("supports_expression_indexes")
3593 def test_composite_func_unique_constraint(self):
3594 with connection.schema_editor() as editor:
3595 editor.create_model(Author)
3596 editor.create_model(BookWithSlug)
3597 constraint = UniqueConstraint(
3598 Upper("title"),
3599 Lower("slug"),
3600 name="func_upper_lower_unq",
3601 )
3602 # Add constraint.
3603 with connection.schema_editor() as editor:
3604 editor.add_constraint(BookWithSlug, constraint)
3605 sql = constraint.create_sql(BookWithSlug, editor)
3606 table = BookWithSlug._meta.db_table
3607 constraints = self.get_constraints(table)
3608 self.assertIn(constraint.name, constraints)
3609 self.assertIs(constraints[constraint.name]["unique"], True)
3610 # SQL contains database functions.
3611 self.assertIs(sql.references_column(table, "title"), True)
3612 self.assertIs(sql.references_column(table, "slug"), True)
3613 sql = str(sql)
3614 self.assertIn("UPPER(%s)" % editor.quote_name("title"), sql)
3615 self.assertIn("LOWER(%s)" % editor.quote_name("slug"), sql)
3616 self.assertLess(sql.index("UPPER"), sql.index("LOWER"))
3617 # Remove constraint.
3618 with connection.schema_editor() as editor:
3619 editor.remove_constraint(BookWithSlug, constraint)
3620 self.assertNotIn(constraint.name, self.get_constraints(table))
3621
3622 @skipUnlessDBFeature("supports_expression_indexes")
3623 def test_unique_constraint_field_and_expression(self):

Callers

nothing calls this directly

Calls 12

create_sqlMethod · 0.95
get_constraintsMethod · 0.95
UniqueConstraintClass · 0.90
UpperClass · 0.90
LowerClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
add_constraintMethod · 0.45
references_columnMethod · 0.45
quote_nameMethod · 0.45
indexMethod · 0.45
remove_constraintMethod · 0.45

Tested by

no test coverage detected