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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
from sqlalchemy import *
import testbase
import string
import sqlalchemy.attributes as attr
class Principal( object ):
pass
class User( Principal ):
pass
class Group( Principal ):
pass
class InheritTest(testbase.AssertMixin):
def setUpAll(self):
global principals
global users
global groups
global user_group_map
principals = Table(
'principals',
testbase.db,
Column('principal_id', Integer, Sequence('principal_id_seq', optional=False), primary_key=True),
Column('name', String(50), nullable=False),
)
users = Table(
'prin_users',
testbase.db,
Column('principal_id', Integer, ForeignKey('principals.principal_id'), primary_key=True),
Column('password', String(50), nullable=False),
Column('email', String(50), nullable=False),
Column('login_id', String(50), nullable=False),
)
groups = Table(
'prin_groups',
testbase.db,
Column( 'principal_id', Integer, ForeignKey('principals.principal_id'), primary_key=True),
)
user_group_map = Table(
'prin_user_group_map',
testbase.db,
Column('user_id', Integer, ForeignKey( "prin_users.principal_id"), primary_key=True ),
Column('group_id', Integer, ForeignKey( "prin_groups.principal_id"), primary_key=True ),
#Column('user_id', Integer, ForeignKey( "prin_users.principal_id"), ),
#Column('group_id', Integer, ForeignKey( "prin_groups.principal_id"), ),
)
principals.create()
users.create()
groups.create()
user_group_map.create()
def tearDownAll(self):
user_group_map.drop()
groups.drop()
users.drop()
principals.drop()
def setUp(self):
objectstore.clear()
clear_mappers()
def testbasic(self):
assign_mapper( Principal, principals )
assign_mapper(
User,
users,
inherits=Principal.mapper
)
assign_mapper(
Group,
groups,
inherits=Principal.mapper,
properties=dict( users = relation(User.mapper, user_group_map, lazy=True, backref="groups") )
)
g = Group(name="group1")
g.users.append(User(name="user1", password="pw", email="foo@bar.com", login_id="lg1"))
objectstore.commit()
class InheritTest2(testbase.AssertMixin):
def setUpAll(self):
engine = testbase.db
global foo, bar, foo_bar
foo = Table('foo', engine,
Column('id', Integer, primary_key=True),
Column('data', String(20)),
).create()
bar = Table('bar', engine,
Column('bid', Integer, ForeignKey('foo.id'), primary_key=True),
#Column('fid', Integer, ForeignKey('foo.id'), )
).create()
foo_bar = Table('foo_bar', engine,
Column('foo_id', Integer, ForeignKey('foo.id')),
Column('bar_id', Integer, ForeignKey('bar.bid'))).create()
def tearDownAll(self):
foo_bar.drop()
bar.drop()
foo.drop()
def testbasic(self):
class Foo(object):
def __init__(self, data=None):
self.data = data
def __str__(self):
return "Foo(%s)" % self.data
def __repr__(self):
return str(self)
Foo.mapper = mapper(Foo, foo)
class Bar(Foo):
def __str__(self):
return "Bar(%s)" % self.data
Bar.mapper = mapper(Bar, bar, inherits=Foo.mapper, properties = {
# the old way, you needed to explicitly set up a compound
# column like this. but now the mapper uses SyncRules to match up
# the parent/child inherited columns
#'id':[bar.c.bid, foo.c.id]
})
Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar, primaryjoin=bar.c.bid==foo_bar.c.bar_id, secondaryjoin=foo_bar.c.foo_id==foo.c.id, lazy=False))
#Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar, lazy=False))
b = Bar('barfoo')
objectstore.commit()
b.foos.append(Foo('subfoo1'))
b.foos.append(Foo('subfoo2'))
objectstore.commit()
objectstore.clear()
l =b.mapper.select()
print l[0]
print l[0].foos
self.assert_result(l, Bar,
# {'id':1, 'data':'barfoo', 'bid':1, 'foos':(Foo, [{'id':2,'data':'subfoo1'}, {'id':3,'data':'subfoo2'}])},
{'id':1, 'data':'barfoo', 'foos':(Foo, [{'id':2,'data':'subfoo1'}, {'id':3,'data':'subfoo2'}])},
)
if __name__ == "__main__":
testbase.main()
|