MCPcopy
hub / github.com/django/django / test_add

Method test_add

tests/postgres_tests/test_operations.py:614–660  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

612 AddConstraintNotValid(model_name="pony", constraint=constraint)
613
614 def test_add(self):
615 table_name = f"{self.app_label}_pony"
616 constraint_name = "pony_pink_gte_check"
617 constraint = CheckConstraint(condition=Q(pink__gte=4), name=constraint_name)
618 operation = AddConstraintNotValid("Pony", constraint=constraint)
619 project_state, new_state = self.make_test_state(self.app_label, operation)
620 self.assertEqual(
621 operation.describe(),
622 f"Create not valid constraint {constraint_name} on model Pony",
623 )
624 self.assertEqual(
625 operation.formatted_description(),
626 f"+ Create not valid constraint {constraint_name} on model Pony",
627 )
628 self.assertEqual(
629 operation.migration_name_fragment,
630 f"pony_{constraint_name}_not_valid",
631 )
632 self.assertEqual(
633 len(new_state.models[self.app_label, "pony"].options["constraints"]),
634 1,
635 )
636 self.assertConstraintNotExists(table_name, constraint_name)
637 Pony = new_state.apps.get_model(self.app_label, "Pony")
638 self.assertEqual(len(Pony._meta.constraints), 1)
639 Pony.objects.create(pink=2, weight=1.0)
640 # Add constraint.
641 with connection.schema_editor(atomic=True) as editor:
642 operation.database_forwards(
643 self.app_label, editor, project_state, new_state
644 )
645 msg = f'check constraint "{constraint_name}"'
646 with self.assertRaisesMessage(IntegrityError, msg), transaction.atomic():
647 Pony.objects.create(pink=3, weight=1.0)
648 self.assertConstraintExists(table_name, constraint_name)
649 # Reversal.
650 with connection.schema_editor(atomic=True) as editor:
651 operation.database_backwards(
652 self.app_label, editor, project_state, new_state
653 )
654 self.assertConstraintNotExists(table_name, constraint_name)
655 Pony.objects.create(pink=3, weight=1.0)
656 # Deconstruction.
657 name, args, kwargs = operation.deconstruct()
658 self.assertEqual(name, "AddConstraintNotValid")
659 self.assertEqual(args, [])
660 self.assertEqual(kwargs, {"model_name": "Pony", "constraint": constraint})
661
662
663@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")

Callers

nothing calls this directly

Calls 15

describeMethod · 0.95
database_forwardsMethod · 0.95
CheckConstraintClass · 0.90
QClass · 0.90
make_test_stateMethod · 0.80
formatted_descriptionMethod · 0.80
schema_editorMethod · 0.80
assertRaisesMessageMethod · 0.80
get_modelMethod · 0.45

Tested by

no test coverage detected