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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
import testbase
from sqlalchemy.ext.activemapper import ActiveMapper, column, one_to_many, one_to_one, many_to_many, objectstore
from sqlalchemy import and_, or_, clear_mappers, backref, create_session, exceptions
from sqlalchemy import ForeignKey, String, Integer, DateTime, Table, Column
from datetime import datetime
import sqlalchemy
import sqlalchemy.ext.activemapper as activemapper
class testcase(testbase.PersistTest):
def setUpAll(self):
global Person, Preferences, Address
class Person(ActiveMapper):
class mapping:
__version_id_col__ = 'row_version'
full_name = column(String)
first_name = column(String)
middle_name = column(String)
last_name = column(String)
birth_date = column(DateTime)
ssn = column(String)
gender = column(String)
home_phone = column(String)
cell_phone = column(String)
work_phone = column(String)
row_version = column(Integer, default=0)
prefs_id = column(Integer, foreign_key=ForeignKey('preferences.id'))
addresses = one_to_many('Address', colname='person_id', backref='person', order_by=['state', 'city', 'postal_code'])
preferences = one_to_one('Preferences', colname='pref_id', backref='person')
def __str__(self):
s = '%s\n' % self.full_name
s += ' * birthdate: %s\n' % (self.birth_date or 'not provided')
s += ' * fave color: %s\n' % (self.preferences.favorite_color or 'Unknown')
s += ' * personality: %s\n' % (self.preferences.personality_type or 'Unknown')
for address in self.addresses:
s += ' * address: %s\n' % address.address_1
s += ' %s, %s %s\n' % (address.city, address.state, address.postal_code)
return s
class Preferences(ActiveMapper):
class mapping:
__table__ = 'preferences'
favorite_color = column(String)
personality_type = column(String)
class Address(ActiveMapper):
class mapping:
# note that in other objects, the 'id' primary key is
# automatically added -- if you specify a primary key,
# then ActiveMapper will not add an integer primary key
# for you.
id = column(Integer, primary_key=True)
type = column(String)
address_1 = column(String)
city = column(String)
state = column(String)
postal_code = column(String)
person_id = column(Integer, foreign_key=ForeignKey('person.id'))
activemapper.metadata.connect(testbase.db)
activemapper.create_tables()
def tearDownAll(self):
clear_mappers()
activemapper.drop_tables()
def tearDown(self):
for t in activemapper.metadata.table_iterator(reverse=True):
t.delete().execute()
def create_person_one(self):
# create a person
p1 = Person(
full_name='Jonathan LaCour',
birth_date=datetime(1979, 10, 12),
preferences=Preferences(
favorite_color='Green',
personality_type='ENTP'
),
addresses=[
Address(
address_1='123 Some Great Road.',
city='Atlanta',
state='GA',
postal_code='30338'
),
Address(
address_1='435 Franklin Road.',
city='Atlanta',
state='GA',
postal_code='30342'
)
]
)
return p1
def create_person_two(self):
p2 = Person(
full_name='Lacey LaCour',
addresses=[
Address(
address_1='123 Some Great Road.',
city='Atlanta',
state='GA',
postal_code='30338'
),
Address(
address_1='200 Main Street',
city='Roswell',
state='GA',
postal_code='30075'
)
]
)
# I don't like that I have to do this... and putting
# a "self.preferences = Preferences()" into the __init__
# of Person also doens't seem to fix this
p2.preferences = Preferences()
return p2
def test_create(self):
p1 = self.create_person_one()
objectstore.flush()
objectstore.clear()
results = Person.select()
self.assertEquals(len(results), 1)
person = results[0]
self.assertEquals(person.id, p1.id)
self.assertEquals(len(person.addresses), 2)
self.assertEquals(person.addresses[0].postal_code, '30338')
@testbase.unsupported('mysql')
def test_update(self):
p1 = self.create_person_one()
objectstore.flush()
objectstore.clear()
person = Person.select()[0]
person.gender = 'F'
objectstore.flush()
objectstore.clear()
self.assertEquals(person.row_version, 2)
person = Person.select()[0]
person.gender = 'M'
objectstore.flush()
objectstore.clear()
self.assertEquals(person.row_version, 3)
#TODO: check that a concurrent modification raises exception
p1 = Person.select()[0]
s1 = objectstore.session
s2 = create_session()
objectstore.context.current = s2
p2 = Person.select()[0]
p1.first_name = "jack"
p2.first_name = "ed"
objectstore.flush()
try:
objectstore.context.current = s1
objectstore.flush()
assert False
except exceptions.ConcurrentModificationError:
pass
def test_delete(self):
p1 = self.create_person_one()
objectstore.flush()
objectstore.clear()
results = Person.select()
self.assertEquals(len(results), 1)
results[0].delete()
objectstore.flush()
objectstore.clear()
results = Person.select()
self.assertEquals(len(results), 0)
def test_multiple(self):
p1 = self.create_person_one()
p2 = self.create_person_two()
objectstore.flush()
objectstore.clear()
# select and make sure we get back two results
people = Person.select()
self.assertEquals(len(people), 2)
# make sure that our backwards relationships work
self.assertEquals(people[0].addresses[0].person.id, p1.id)
self.assertEquals(people[1].addresses[0].person.id, p2.id)
# try a more complex select
results = Person.select(
or_(
and_(
Address.c.person_id == Person.c.id,
Address.c.postal_code.like('30075')
),
and_(
Person.c.prefs_id == Preferences.c.id,
Preferences.c.favorite_color == 'Green'
)
)
)
self.assertEquals(len(results), 2)
def test_oneway_backref(self):
# FIXME: I don't know why, but it seems that my backwards relationship
# on preferences still ends up being a list even though I pass
# in uselist=False...
# FIXED: the backref is a new PropertyLoader which needs its own "uselist".
# uses a function which I dont think existed when you first wrote ActiveMapper.
p1 = self.create_person_one()
self.assertEquals(p1.preferences.person, p1)
p1.delete()
objectstore.flush()
objectstore.clear()
def test_select_by(self):
# FIXME: either I don't understand select_by, or it doesn't work.
# FIXED (as good as we can for now): yup....everyone thinks it works that way....it only
# generates joins for keyword arguments, not ColumnClause args. would need a new layer of
# "MapperClause" objects to use properties in expressions. (MB)
p1 = self.create_person_one()
p2 = self.create_person_two()
objectstore.flush()
objectstore.clear()
results = Person.select(
Address.c.postal_code.like('30075') &
Person.join_to('addresses')
)
self.assertEquals(len(results), 1)
self.assertEquals(Person.count(), 2)
class testmanytomany(testbase.PersistTest):
def setUpAll(self):
global secondarytable, foo, baz
secondarytable = Table("secondarytable",
activemapper.metadata,
Column("foo_id", Integer, ForeignKey("foo.id"),primary_key=True),
Column("baz_id", Integer, ForeignKey("baz.id"),primary_key=True))
class foo(activemapper.ActiveMapper):
class mapping:
name = column(String(30))
# bazrel = many_to_many('baz', secondarytable, backref='foorel')
class baz(activemapper.ActiveMapper):
class mapping:
name = column(String(30))
foorel = many_to_many("foo", secondarytable, backref='bazrel')
activemapper.metadata.connect(testbase.db)
activemapper.create_tables()
# Create a couple of activemapper objects
def create_objects(self):
return foo(name='foo1'), baz(name='baz1')
def tearDownAll(self):
clear_mappers()
activemapper.drop_tables()
objectstore.clear()
def testbasic(self):
# Set up activemapper objects
foo1, baz1 = self.create_objects()
objectstore.flush()
objectstore.clear()
foo1 = foo.get_by(name='foo1')
baz1 = baz.get_by(name='baz1')
# Just checking ...
assert (foo1.name == 'foo1')
assert (baz1.name == 'baz1')
# Diagnostics ...
# import sys
# sys.stderr.write("\nbazrel missing from dir(foo1):\n%s\n" % dir(foo1))
# sys.stderr.write("\nbazrel in foo1 relations:\n%s\n" % foo1.relations)
# Optimistically based on activemapper one_to_many test, try to append
# baz1 to foo1.bazrel - (AttributeError: 'foo' object has no attribute 'bazrel')
print sqlalchemy.class_mapper(foo).props
print sqlalchemy.class_mapper(baz).props
foo1.bazrel.append(baz1)
assert (foo1.bazrel == [baz1])
class testselfreferential(testbase.PersistTest):
def setUpAll(self):
global TreeNode
class TreeNode(activemapper.ActiveMapper):
class mapping:
id = column(Integer, primary_key=True)
name = column(String(30))
parent_id = column(Integer, foreign_key=ForeignKey('treenode.id'))
children = one_to_many('TreeNode', colname='id', backref='parent')
activemapper.metadata.connect(testbase.db)
activemapper.create_tables()
def tearDownAll(self):
clear_mappers()
activemapper.drop_tables()
def testbasic(self):
t = TreeNode(name='node1')
t.children.append(TreeNode(name='node2'))
t.children.append(TreeNode(name='node3'))
objectstore.flush()
objectstore.clear()
t = TreeNode.get_by(name='node1')
assert (t.name == 'node1')
assert (t.children[0].name == 'node2')
assert (t.children[1].name == 'node3')
assert (t.children[1].parent is t)
objectstore.clear()
t = TreeNode.get_by(name='node3')
assert (t.parent is TreeNode.get_by(name='node1'))
if __name__ == '__main__':
testbase.main()
|