1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import testenv; testenv.configure_for_tests()
from sqlalchemy import *
from sqlalchemy import exceptions, util
from sqlalchemy.orm import *
from testlib import *
from testlib import fixtures
class ABCTest(ORMTest):
def define_tables(self, metadata):
global a, b, c
a = Table('a', metadata,
Column('id', Integer, primary_key=True),
Column('adata', String(30)),
Column('type', String(30)),
)
b = Table('b', metadata,
Column('id', Integer, ForeignKey('a.id'), primary_key=True),
Column('bdata', String(30)))
c = Table('c', metadata,
Column('id', Integer, ForeignKey('b.id'), primary_key=True),
Column('cdata', String(30)))
def make_test(fetchtype):
def test_roundtrip(self):
class A(fixtures.Base):pass
class B(A):pass
class C(B):pass
if fetchtype == 'union':
abc = a.outerjoin(b).outerjoin(c)
bc = a.join(b).outerjoin(c)
else:
abc = bc = None
mapper(A, a, select_table=abc, polymorphic_on=a.c.type, polymorphic_identity='a', polymorphic_fetch=fetchtype)
mapper(B, b, select_table=bc, inherits=A, polymorphic_identity='b', polymorphic_fetch=fetchtype)
mapper(C, c, inherits=B, polymorphic_identity='c')
a1 = A(adata='a1')
b1 = B(bdata='b1', adata='b1')
b2 = B(bdata='b2', adata='b2')
b3 = B(bdata='b3', adata='b3')
c1 = C(cdata='c1', bdata='c1', adata='c1')
c2 = C(cdata='c2', bdata='c2', adata='c2')
c3 = C(cdata='c2', bdata='c2', adata='c2')
sess = create_session()
for x in (a1, b1, b2, b3, c1, c2, c3):
sess.save(x)
sess.flush()
sess.clear()
#for obj in sess.query(A).all():
# print obj
assert [
A(adata='a1'),
B(bdata='b1', adata='b1'),
B(bdata='b2', adata='b2'),
B(bdata='b3', adata='b3'),
C(cdata='c1', bdata='c1', adata='c1'),
C(cdata='c2', bdata='c2', adata='c2'),
C(cdata='c2', bdata='c2', adata='c2'),
] == sess.query(A).all()
assert [
B(bdata='b1', adata='b1'),
B(bdata='b2', adata='b2'),
B(bdata='b3', adata='b3'),
C(cdata='c1', bdata='c1', adata='c1'),
C(cdata='c2', bdata='c2', adata='c2'),
C(cdata='c2', bdata='c2', adata='c2'),
] == sess.query(B).all()
assert [
C(cdata='c1', bdata='c1', adata='c1'),
C(cdata='c2', bdata='c2', adata='c2'),
C(cdata='c2', bdata='c2', adata='c2'),
] == sess.query(C).all()
test_roundtrip.__name__ = 'test_%s' % fetchtype
return test_roundtrip
test_union = make_test('union')
test_select = make_test('select')
test_deferred = make_test('deferred')
if __name__ == '__main__':
testenv.main()
|