diff options
Diffstat (limited to 'test/orm/relationships.py')
-rw-r--r-- | test/orm/relationships.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/test/orm/relationships.py b/test/orm/relationships.py new file mode 100644 index 000000000..4aac8a290 --- /dev/null +++ b/test/orm/relationships.py @@ -0,0 +1,105 @@ +import testbase +import unittest, sys, datetime + +db = testbase.db +#db.echo_uow=True + +from sqlalchemy import * + + +class RelationTest(testbase.PersistTest): + """this is essentially an extension of the "dependency.py" topological sort test. + in this test, a table is dependent on two other tables that are otherwise unrelated to each other. + the dependency sort must insure that this childmost table is below both parent tables in the outcome + (a bug existed where this was not always the case). + while the straight topological sort tests should expose this, since the sorting can be different due + to subtle differences in program execution, this test case was exposing the bug whereas the simpler tests + were not.""" + def setUpAll(self): + global tbl_a + global tbl_b + global tbl_c + global tbl_d + metadata = MetaData() + tbl_a = Table("tbl_a", metadata, + Column("id", Integer, primary_key=True), + Column("name", String), + ) + tbl_b = Table("tbl_b", metadata, + Column("id", Integer, primary_key=True), + Column("name", String), + ) + tbl_c = Table("tbl_c", metadata, + Column("id", Integer, primary_key=True), + Column("tbl_a_id", Integer, ForeignKey("tbl_a.id"), nullable=False), + Column("name", String), + ) + tbl_d = Table("tbl_d", metadata, + Column("id", Integer, primary_key=True), + Column("tbl_c_id", Integer, ForeignKey("tbl_c.id"), nullable=False), + Column("tbl_b_id", Integer, ForeignKey("tbl_b.id")), + Column("name", String), + ) + def setUp(self): + global session + session = create_session(bind_to=testbase.db) + conn = session.connect() + conn.create(tbl_a) + conn.create(tbl_b) + conn.create(tbl_c) + conn.create(tbl_d) + + class A(object): + pass + class B(object): + pass + class C(object): + pass + class D(object): + pass + + D.mapper = mapper(D, tbl_d) + C.mapper = mapper(C, tbl_c, properties=dict( + d_rows=relation(D, private=True, backref="c_row"), + )) + B.mapper = mapper(B, tbl_b) + A.mapper = mapper(A, tbl_a, properties=dict( + c_rows=relation(C, private=True, backref="a_row"), + )) + D.mapper.add_property("b_row", relation(B)) + + global a + global c + a = A(); a.name = "a1" + b = B(); b.name = "b1" + c = C(); c.name = "c1"; c.a_row = a + # we must have more than one d row or it won't fail + d1 = D(); d1.name = "d1"; d1.b_row = b; d1.c_row = c + d2 = D(); d2.name = "d2"; d2.b_row = b; d2.c_row = c + d3 = D(); d3.name = "d3"; d3.b_row = b; d3.c_row = c + session.save_or_update(a) + session.save_or_update(b) + + def tearDown(self): + conn = session.connect() + conn.drop(tbl_d) + conn.drop(tbl_c) + conn.drop(tbl_b) + conn.drop(tbl_a) + + def tearDownAll(self): + testbase.metadata.tables.clear() + + def testDeleteRootTable(self): + session.flush() + session.delete(a) # works as expected + session.flush() + + def testDeleteMiddleTable(self): + session.flush() + session.delete(c) # fails + session.flush() + + +if __name__ == "__main__": + testbase.main() |