From a65547c04af52087f78e9bbaa79bdae3807e5c64 Mon Sep 17 00:00:00 2001
From: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Date: Wed, 13 Apr 2022 11:52:40 +0200
Subject: [PATCH] Fixed tests on databases that don't support introspecting
 foreign keys.

---
 tests/migrations/test_base.py |  2 ++
 tests/schema/tests.py         | 44 +++++++++++++++++++++++++++--------
 2 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/tests/migrations/test_base.py b/tests/migrations/test_base.py
index 955bfa99d0..819464451a 100644
--- a/tests/migrations/test_base.py
+++ b/tests/migrations/test_base.py
@@ -125,6 +125,8 @@ class MigrationTestBase(TransactionTestCase):
             )
 
     def assertFKExists(self, table, columns, to, value=True, using="default"):
+        if not connections[using].features.can_introspect_foreign_keys:
+            return
         with connections[using].cursor() as cursor:
             self.assertEqual(
                 value,
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index d56e820251..c758db8bc8 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -283,6 +283,8 @@ class SchemaTests(TransactionTestCase):
         Fail if the FK constraint on `model.Meta.db_table`.`column` to
         `expected_fk_table`.id doesn't exist.
         """
+        if not connection.features.can_introspect_foreign_keys:
+            return
         constraints = self.get_constraints(model._meta.db_table)
         constraint_fk = None
         for details in constraints.values():
@@ -292,6 +294,8 @@ class SchemaTests(TransactionTestCase):
         self.assertEqual(constraint_fk, (expected_fk_table, field))
 
     def assertForeignKeyNotExists(self, model, column, expected_fk_table):
+        if not connection.features.can_introspect_foreign_keys:
+            return
         with self.assertRaises(AssertionError):
             self.assertForeignKeyExists(model, column, expected_fk_table)
 
@@ -487,7 +491,7 @@ class SchemaTests(TransactionTestCase):
             editor.create_model(AuthorRef)
         self.assertForeignKeyExists(AuthorRef, "author_id", "schema_author")
 
-    @skipUnlessDBFeature("supports_foreign_keys")
+    @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
     def test_fk_db_constraint(self):
         "The db_constraint parameter is respected"
         # Create the table
@@ -1298,7 +1302,7 @@ class SchemaTests(TransactionTestCase):
             editor.alter_field(LocalBook, old_field, new_field, strict=True)
         self.assertForeignKeyExists(LocalBook, "author_id", "schema_author")
 
-    @skipUnlessDBFeature("supports_foreign_keys")
+    @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
     def test_alter_o2o_to_fk(self):
         """
         #24163 - Tests altering of OneToOneField to ForeignKey
@@ -1344,7 +1348,7 @@ class SchemaTests(TransactionTestCase):
         )
         self.assertForeignKeyExists(Book, "author_id", "schema_author")
 
-    @skipUnlessDBFeature("supports_foreign_keys")
+    @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
     def test_alter_fk_to_o2o(self):
         """
         #24163 - Tests altering of ForeignKey to OneToOneField
@@ -1394,7 +1398,12 @@ class SchemaTests(TransactionTestCase):
         with connection.schema_editor() as editor:
             editor.create_model(Author)
             editor.create_model(Book)
-        expected_fks = 1 if connection.features.supports_foreign_keys else 0
+        expected_fks = (
+            1
+            if connection.features.supports_foreign_keys
+            and connection.features.can_introspect_foreign_keys
+            else 0
+        )
         expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
 
         # Check the index is right to begin with.
@@ -1412,7 +1421,7 @@ class SchemaTests(TransactionTestCase):
         new_field = OneToOneField(Author, CASCADE)
         new_field.set_attributes_from_name("author")
         with connection.schema_editor() as editor:
-            editor.alter_field(Book, old_field, new_field, strict=True)
+            editor.alter_field(Book, old_field, new_field)
 
         counts = self.get_constraints_count(
             Book._meta.db_table,
@@ -1427,7 +1436,12 @@ class SchemaTests(TransactionTestCase):
         with connection.schema_editor() as editor:
             editor.create_model(Author)
             editor.create_model(Book)
-        expected_fks = 1 if connection.features.supports_foreign_keys else 0
+        expected_fks = (
+            1
+            if connection.features.supports_foreign_keys
+            and connection.features.can_introspect_foreign_keys
+            else 0
+        )
         expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
 
         # Check the index is right to begin with.
@@ -1463,7 +1477,12 @@ class SchemaTests(TransactionTestCase):
         with connection.schema_editor() as editor:
             editor.create_model(Author)
             editor.create_model(BookWithO2O)
-        expected_fks = 1 if connection.features.supports_foreign_keys else 0
+        expected_fks = (
+            1
+            if connection.features.supports_foreign_keys
+            and connection.features.can_introspect_foreign_keys
+            else 0
+        )
 
         # Check the unique constraint is right to begin with.
         counts = self.get_constraints_count(
@@ -1477,7 +1496,7 @@ class SchemaTests(TransactionTestCase):
         new_field = ForeignKey(Author, CASCADE)
         new_field.set_attributes_from_name("author")
         with connection.schema_editor() as editor:
-            editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
+            editor.alter_field(BookWithO2O, old_field, new_field)
 
         counts = self.get_constraints_count(
             BookWithO2O._meta.db_table,
@@ -1492,7 +1511,12 @@ class SchemaTests(TransactionTestCase):
         with connection.schema_editor() as editor:
             editor.create_model(Author)
             editor.create_model(BookWithO2O)
-        expected_fks = 1 if connection.features.supports_foreign_keys else 0
+        expected_fks = (
+            1
+            if connection.features.supports_foreign_keys
+            and connection.features.can_introspect_foreign_keys
+            else 0
+        )
 
         # Check the unique constraint is right to begin with.
         counts = self.get_constraints_count(
@@ -3752,7 +3776,7 @@ class SchemaTests(TransactionTestCase):
                 expected_constraint_name, self.get_constraints(model._meta.db_table)
             )
 
-            if editor.sql_create_fk:
+            if editor.sql_create_fk and connection.features.can_introspect_foreign_keys:
                 constraint_name = "CamelCaseFKConstraint"
                 expected_constraint_name = identifier_converter(constraint_name)
                 editor.execute(