summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-07-25 13:44:10 +0000
committerGerrit Code Review <review@openstack.org>2014-07-25 13:44:10 +0000
commit50b8d1e8a1fbad5fa8f423229b993fb088282a15 (patch)
tree690a7fb14442a1bb804e887dae06ec00b3420027 /tests
parent8dbba33f00c928e5349af967a28616044e1d788d (diff)
parenta9e3af2ebf9de4f771513283766d98c5f13dec24 (diff)
downloadoslo-db-50b8d1e8a1fbad5fa8f423229b993fb088282a15.tar.gz
Merge "Raise DBReferenceError on foreign key violation"
Diffstat (limited to 'tests')
-rw-r--r--tests/sqlalchemy/test_exc_filters.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/sqlalchemy/test_exc_filters.py b/tests/sqlalchemy/test_exc_filters.py
index 1700bd9..1955c8a 100644
--- a/tests/sqlalchemy/test_exc_filters.py
+++ b/tests/sqlalchemy/test_exc_filters.py
@@ -181,6 +181,89 @@ class TestFallthroughsAndNonDBAPI(TestsExceptionFilter):
self.assertEqual("mysqldb has an attribute error", matched.message)
+class TestRaiseReferenceError(TestsExceptionFilter):
+ def test_postgresql(self):
+ e = self._run_test(
+ "postgresql",
+ "INSERT SOMETHING",
+ self.IntegrityError(
+ "insert or update on table "
+ "\"resource_entity\" "
+ "violates foreign key constraint "
+ "\"resource_entity_entity_id_fkey\"\n"
+ "DETAIL: Key "
+ "(entity_id)=(74b5da71-5a9c-4f89-a8e9-4a2d856e6c29) "
+ "is not present in table \"entity\".\n"
+ "'INSERT INTO resource_entity (resource_id, entity_id, name) "
+ "VALUES (%(resource_id)s, "
+ "%(entity_id)s, %(name)s)' "
+ "{'entity_id': '74b5da71-5a9c-4f89-a8e9-4a2d856e6c29', "
+ "'name': u'foo', "
+ "'resource_id': 'ffb12cb4-d955-4d96-a315-5f48ea161eef'}"),
+ exception.DBReferenceError,
+ )
+ self.assertEqual("resource_entity", e.table)
+ self.assertEqual("resource_entity_entity_id_fkey", e.constraint)
+ self.assertEqual("entity_id", e.key)
+ self.assertEqual("entity", e.key_table)
+ self.assertEqual(
+ "(IntegrityError) insert or update on table "
+ "\"resource_entity\" violates foreign key constraint "
+ "\"resource_entity_entity_id_fkey\"\n"
+ "DETAIL: Key (entity_id)=(74b5da71-5a9c-4f89-a8e9-4a2d856e6c29) "
+ "is not present in table \"entity\".\n"
+ "'INSERT INTO resource_entity (resource_id, entity_id, name) "
+ "VALUES (%(resource_id)s, %(entity_id)s, %(name)s)' "
+ "{'entity_id': '74b5da71-5a9c-4f89-a8e9-4a2d856e6c29', "
+ "'name': u'foo', "
+ "'resource_id': 'ffb12cb4-d955-4d96-a315-5f48ea161eef'} "
+ "'INSERT SOMETHING' ()",
+ str(e))
+
+ def test_mysql(self):
+ e = self._run_test(
+ "mysql",
+ "INSERT SOMETHING",
+ self.IntegrityError(
+ "Cannot add or update a child row: "
+ "a foreign key constraint fails "
+ "(resource_entity, CONSTRAINT resource_entity_entity_id_fkey "
+ "FOREIGN KEY (entity_id) "
+ "REFERENCES entity (entity_id))"
+ ),
+ exception.DBReferenceError,
+ )
+ self.assertEqual("resource_entity", e.table)
+ self.assertEqual("resource_entity_entity_id_fkey", e.constraint)
+ self.assertEqual("entity_id", e.key)
+ self.assertEqual("entity", e.key_table)
+ self.assertEqual(
+ "(IntegrityError) Cannot add or update a child row: "
+ "a foreign key constraint fails "
+ "(resource_entity, CONSTRAINT resource_entity_entity_id_fkey "
+ "FOREIGN KEY (entity_id) REFERENCES entity (entity_id)) "
+ "'INSERT SOMETHING' ()",
+ str(e))
+
+ def test_sqlite(self):
+ e = self._run_test(
+ "sqlite",
+ "INSERT SOMETHING",
+ self.IntegrityError(
+ "SQL error: foreign key constraint failed"
+ ),
+ exception.DBReferenceError,
+ )
+ self.assertIsNone(e.table)
+ self.assertIsNone(e.constraint)
+ self.assertIsNone(e.key)
+ self.assertIsNone(e.key_table)
+ self.assertEqual(
+ "(IntegrityError) SQL error: foreign key "
+ "constraint failed 'INSERT SOMETHING' ()",
+ str(e))
+
+
class TestDuplicate(TestsExceptionFilter):
def _run_dupe_constraint_test(self, dialect_name, message,