MCPcopy
hub / github.com/django/django / test_add_field

Method test_add_field

tests/schema/tests.py:737–773  ·  view source on GitHub ↗

Tests adding fields to models

(self)

Source from the content-addressed store, hash-verified

735 self._test_m2m_db_constraint(InheritedManyToManyField)
736
737 def test_add_field(self):
738 """
739 Tests adding fields to models
740 """
741 # Create the table
742 with connection.schema_editor() as editor:
743 editor.create_model(Author)
744 # Ensure there's no age field
745 columns = self.column_classes(Author)
746 self.assertNotIn("age", columns)
747 # Add the new field
748 new_field = IntegerField(null=True)
749 new_field.set_attributes_from_name("age")
750 with (
751 CaptureQueriesContext(connection) as ctx,
752 connection.schema_editor() as editor,
753 ):
754 editor.add_field(Author, new_field)
755 drop_default_sql = editor.sql_alter_column_no_default % {
756 "column": editor.quote_name(new_field.name),
757 }
758 self.assertFalse(
759 any(drop_default_sql in query["sql"] for query in ctx.captured_queries)
760 )
761 # Table is not rebuilt.
762 self.assertIs(
763 any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries), False
764 )
765 self.assertIs(
766 any("DROP TABLE" in query["sql"] for query in ctx.captured_queries), False
767 )
768 columns = self.column_classes(Author)
769 self.assertEqual(
770 columns["age"][0],
771 connection.features.introspected_field_types["IntegerField"],
772 )
773 self.assertTrue(columns["age"][1][6])
774
775 def test_add_field_remove_field(self):
776 """

Callers

nothing calls this directly

Calls 8

column_classesMethod · 0.95
IntegerFieldClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
add_fieldMethod · 0.45
quote_nameMethod · 0.45

Tested by

no test coverage detected