MCPcopy
hub / github.com/django/django / test_alter_field_fk_to_o2o

Method test_alter_field_fk_to_o2o

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

Source from the content-addressed store, hash-verified

1938 self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
1939
1940 def test_alter_field_fk_to_o2o(self):
1941 with connection.schema_editor() as editor:
1942 editor.create_model(Author)
1943 editor.create_model(Book)
1944 expected_fks = (
1945 1
1946 if connection.features.supports_foreign_keys
1947 and connection.features.can_introspect_foreign_keys
1948 else 0
1949 )
1950 expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
1951
1952 # Check the index is right to begin with.
1953 counts = self.get_constraints_count(
1954 Book._meta.db_table,
1955 Book._meta.get_field("author").column,
1956 (Author._meta.db_table, Author._meta.pk.column),
1957 )
1958 self.assertEqual(
1959 counts,
1960 {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
1961 )
1962
1963 old_field = Book._meta.get_field("author")
1964 new_field = OneToOneField(Author, CASCADE)
1965 new_field.set_attributes_from_name("author")
1966 with connection.schema_editor() as editor:
1967 editor.alter_field(Book, old_field, new_field)
1968
1969 counts = self.get_constraints_count(
1970 Book._meta.db_table,
1971 Book._meta.get_field("author").column,
1972 (Author._meta.db_table, Author._meta.pk.column),
1973 )
1974 # The index on ForeignKey is replaced with a unique constraint for
1975 # OneToOneField.
1976 self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
1977
1978 def test_autofield_to_o2o(self):
1979 with connection.schema_editor() as editor:

Callers

nothing calls this directly

Calls 7

get_constraints_countMethod · 0.95
OneToOneFieldClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
get_fieldMethod · 0.45
alter_fieldMethod · 0.45

Tested by

no test coverage detected