summaryrefslogtreecommitdiff
path: root/test/sql
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2020-05-09 13:57:21 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2020-05-09 13:57:21 +0000
commit7f084b7bd4db6bf91cde7f58fbcc55c6ef14f7af (patch)
tree2ef4a8a615a0f96ea65db5481513db6ea30028b4 /test/sql
parentaca8a88976a08fbc51b7be118a5d83b102bcb89b (diff)
parent8782469b789585d3f0c3a642f0bb9519816f6b11 (diff)
downloadsqlalchemy-7f084b7bd4db6bf91cde7f58fbcc55c6ef14f7af.tar.gz
Merge "Warn when sorted_tables is not actually sorting"
Diffstat (limited to 'test/sql')
-rw-r--r--test/sql/test_metadata.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py
index 36f308073..07f5b80db 100644
--- a/test/sql/test_metadata.py
+++ b/test/sql/test_metadata.py
@@ -611,6 +611,93 @@ class MetaDataTest(fixtures.TestBase, ComparesTables):
a.add_is_dependent_on(b)
eq_(meta.sorted_tables, [b, c, d, a, e])
+ def test_fks_deterministic_order(self):
+ meta = MetaData()
+ a = Table("a", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ b = Table("b", meta, Column("foo", Integer))
+ c = Table("c", meta, Column("foo", Integer))
+ d = Table("d", meta, Column("foo", Integer))
+ e = Table("e", meta, Column("foo", Integer, ForeignKey("c.foo")))
+
+ eq_(meta.sorted_tables, [b, c, d, a, e])
+
+ def test_cycles_fks_warning_one(self):
+ meta = MetaData()
+ a = Table("a", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ b = Table("b", meta, Column("foo", Integer, ForeignKey("d.foo")))
+ c = Table("c", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ d = Table("d", meta, Column("foo", Integer, ForeignKey("c.foo")))
+ e = Table("e", meta, Column("foo", Integer))
+
+ with testing.expect_warnings(
+ "Cannot correctly sort tables; there are unresolvable cycles "
+ 'between tables "b, c, d", which is usually caused by mutually '
+ "dependent foreign key constraints. "
+ "Foreign key constraints involving these tables will not be "
+ "considered"
+ ):
+ eq_(meta.sorted_tables, [b, c, d, e, a])
+
+ def test_cycles_fks_warning_two(self):
+ meta = MetaData()
+ a = Table("a", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ b = Table("b", meta, Column("foo", Integer, ForeignKey("a.foo")))
+ c = Table("c", meta, Column("foo", Integer, ForeignKey("e.foo")))
+ d = Table("d", meta, Column("foo", Integer))
+ e = Table("e", meta, Column("foo", Integer, ForeignKey("d.foo")))
+
+ with testing.expect_warnings(
+ "Cannot correctly sort tables; there are unresolvable cycles "
+ 'between tables "a, b", which is usually caused by mutually '
+ "dependent foreign key constraints. "
+ "Foreign key constraints involving these tables will not be "
+ "considered"
+ ):
+ eq_(meta.sorted_tables, [a, b, d, e, c])
+
+ def test_cycles_fks_fks_delivered_separately(self):
+ meta = MetaData()
+ a = Table("a", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ b = Table("b", meta, Column("foo", Integer, ForeignKey("a.foo")))
+ c = Table("c", meta, Column("foo", Integer, ForeignKey("e.foo")))
+ d = Table("d", meta, Column("foo", Integer))
+ e = Table("e", meta, Column("foo", Integer, ForeignKey("d.foo")))
+
+ results = schema.sort_tables_and_constraints(
+ sorted(meta.tables.values(), key=lambda t: t.key)
+ )
+ results[-1] = (None, set(results[-1][-1]))
+ eq_(
+ results,
+ [
+ (a, set()),
+ (b, set()),
+ (d, {fk.constraint for fk in d.foreign_keys}),
+ (e, {fk.constraint for fk in e.foreign_keys}),
+ (c, {fk.constraint for fk in c.foreign_keys}),
+ (
+ None,
+ {fk.constraint for fk in a.foreign_keys}.union(
+ fk.constraint for fk in b.foreign_keys
+ ),
+ ),
+ ],
+ )
+
+ def test_cycles_fks_usealter(self):
+ meta = MetaData()
+ a = Table("a", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ b = Table(
+ "b",
+ meta,
+ Column("foo", Integer, ForeignKey("d.foo", use_alter=True)),
+ )
+ c = Table("c", meta, Column("foo", Integer, ForeignKey("b.foo")))
+ d = Table("d", meta, Column("foo", Integer, ForeignKey("c.foo")))
+ e = Table("e", meta, Column("foo", Integer))
+
+ eq_(meta.sorted_tables, [b, e, a, c, d])
+
def test_nonexistent(self):
assert_raises(
tsa.exc.NoSuchTableError,