MCPcopy
hub / github.com/django/django / test_alter_fk_to_o2o

Method test_alter_fk_to_o2o

tests/schema/tests.py:1895–1938  ·  view source on GitHub ↗

#24163 - Tests altering of ForeignKey to OneToOneField

(self)

Source from the content-addressed store, hash-verified

1893
1894 @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
1895 def test_alter_fk_to_o2o(self):
1896 """
1897 #24163 - Tests altering of ForeignKey to OneToOneField
1898 """
1899 # Create the table
1900 with connection.schema_editor() as editor:
1901 editor.create_model(Author)
1902 editor.create_model(Book)
1903 # Ensure the field is right to begin with
1904 columns = self.column_classes(Book)
1905 self.assertEqual(
1906 columns["author_id"][0],
1907 connection.features.introspected_field_types["BigIntegerField"],
1908 )
1909 # Ensure the field is not unique
1910 author = Author.objects.create(name="Joe")
1911 Book.objects.create(
1912 author=author, title="Django 1", pub_date=datetime.datetime.now()
1913 )
1914 Book.objects.create(
1915 author=author, title="Django 2", pub_date=datetime.datetime.now()
1916 )
1917 Book.objects.all().delete()
1918 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1919 # Alter the ForeignKey to OneToOneField
1920 old_field = Book._meta.get_field("author")
1921 new_field = OneToOneField(Author, CASCADE)
1922 new_field.set_attributes_from_name("author")
1923 with connection.schema_editor() as editor:
1924 editor.alter_field(Book, old_field, new_field, strict=True)
1925 columns = self.column_classes(BookWithO2O)
1926 self.assertEqual(
1927 columns["author_id"][0],
1928 connection.features.introspected_field_types["BigIntegerField"],
1929 )
1930 # Ensure the field is unique now
1931 BookWithO2O.objects.create(
1932 author=author, title="Django 1", pub_date=datetime.datetime.now()
1933 )
1934 with self.assertRaises(IntegrityError):
1935 BookWithO2O.objects.create(
1936 author=author, title="Django 2", pub_date=datetime.datetime.now()
1937 )
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:

Callers

nothing calls this directly

Calls 12

column_classesMethod · 0.95
OneToOneFieldClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
createMethod · 0.45
nowMethod · 0.45
deleteMethod · 0.45
allMethod · 0.45
get_fieldMethod · 0.45
alter_fieldMethod · 0.45

Tested by

no test coverage detected