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
|
from sqlalchemy import *
from sqlalchemy.ext.proxy import ProxyEngine
from testbase import PersistTest
import testbase
import os
#
# Define an engine, table and mapper at the module level, to show that the
# table and mapper can be used with different real engines in multiple threads
#
module_engine = ProxyEngine(echo=testbase.echo)
users = Table('users', module_engine,
Column('user_id', Integer, primary_key=True),
Column('user_name', String(16)),
Column('password', String(20))
)
class User(object):
pass
class ConstructTest(PersistTest):
"""tests that we can build SQL constructs without engine-specific parameters, particulary
oid_column, being needed, as the proxy engine is usually not connected yet."""
def test_join(self):
engine = ProxyEngine()
t = Table('table1', engine,
Column('col1', Integer, primary_key=True))
t2 = Table('table2', engine,
Column('col2', Integer, ForeignKey('table1.col1')))
j = join(t, t2)
class ProxyEngineTest1(PersistTest):
def setUp(self):
clear_mappers()
objectstore.clear()
def test_engine_connect(self):
# connect to a real engine
module_engine.connect(testbase.db_uri)
users.create()
assign_mapper(User, users)
try:
trans = objectstore.begin()
user = User()
user.user_name='fred'
user.password='*'
trans.commit()
# select
sqluser = User.select_by(user_name='fred')[0]
assert sqluser.user_name == 'fred'
# modify
sqluser.user_name = 'fred jones'
# commit - saves everything that changed
objectstore.commit()
allusers = [ user.user_name for user in User.select() ]
assert allusers == [ 'fred jones' ]
finally:
users.drop()
class ThreadProxyTest(PersistTest):
def setUp(self):
assign_mapper(User, users)
def tearDown(self):
clear_mappers()
def tearDownAll(self):
pass
os.remove('threadtesta.db')
os.remove('threadtestb.db')
def test_multi_thread(self):
from threading import Thread
from Queue import Queue
# start 2 threads with different connection params
# and perform simultaneous operations, showing that the
# 2 threads don't share a connection
qa = Queue()
qb = Queue()
def run(db_uri, uname, queue):
def test():
try:
module_engine.connect(db_uri)
users.create()
try:
trans = objectstore.begin()
all = User.select()[:]
assert all == []
u = User()
u.user_name = uname
u.password = 'whatever'
trans.commit()
names = [ us.user_name for us in User.select() ]
assert names == [ uname ]
finally:
users.drop()
module_engine.dispose()
except Exception, e:
import traceback
traceback.print_exc()
queue.put(e)
else:
queue.put(False)
return test
# NOTE: I'm not sure how to give the test runner the option to
# override these uris, or how to safely clear them after test runs
a = Thread(target=run('sqlite://filename=threadtesta.db', 'jim', qa))
b = Thread(target=run('sqlite://filename=threadtestb.db', 'joe', qb))
a.start()
b.start()
# block and wait for the threads to push their results
res = qa.get(True)
if res != False:
raise res
res = qb.get(True)
if res != False:
raise res
class ProxyEngineTest2(PersistTest):
def setUp(self):
clear_mappers()
objectstore.clear()
def test_table_singleton_a(self):
"""set up for table singleton check
"""
#
# For this 'test', create a proxy engine instance, connect it
# to a real engine, and make it do some work
#
engine = ProxyEngine()
cats = Table('cats', engine,
Column('cat_id', Integer, primary_key=True),
Column('cat_name', String))
engine.connect(testbase.db_uri)
cats.create()
cats.drop()
ProxyEngineTest2.cats_table_a = cats
assert isinstance(cats, Table)
def test_table_singleton_b(self):
"""check that a table on a 2nd proxy engine instance gets 2nd table
instance
"""
#
# Now create a new proxy engine instance and attach the same
# table as the first test. This should result in 2 table instances,
# since different proxy engine instances can't attach to the
# same table instance
#
engine = ProxyEngine()
cats = Table('cats', engine,
Column('cat_id', Integer, primary_key=True),
Column('cat_name', String))
assert id(cats) != id(ProxyEngineTest2.cats_table_a)
# the real test -- if we're still using the old engine reference,
# this will fail because the old reference's local storage will
# not have the default attributes
engine.connect(testbase.db_uri)
cats.create()
cats.drop()
def test_type_engine_caching(self):
from sqlalchemy.engine import SQLEngine
import sqlalchemy.types as sqltypes
class EngineA(SQLEngine):
def __init__(self):
pass
def hash_key(self):
return 'a'
def type_descriptor(self, typeobj):
if typeobj == types.Integer:
return TypeEngineX2()
else:
return TypeEngineSTR()
class EngineB(SQLEngine):
def __init__(self):
pass
def hash_key(self):
return 'b'
def type_descriptor(self, typeobj):
return TypeEngineMonkey()
class TypeEngineX2(sqltypes.TypeEngine):
def convert_bind_param(self, value, engine):
return value * 2
class TypeEngineSTR(sqltypes.TypeEngine):
def convert_bind_param(self, value, engine):
return repr(str(value))
class TypeEngineMonkey(sqltypes.TypeEngine):
def convert_bind_param(self, value, engine):
return 'monkey'
engine = ProxyEngine()
engine.storage.engine = EngineA()
a = engine.type_descriptor(sqltypes.Integer)
assert a.convert_bind_param(12, engine) == 24
assert a.convert_bind_param([1,2,3], engine) == [1, 2, 3, 1, 2, 3]
a2 = engine.type_descriptor(sqltypes.String)
assert a2.convert_bind_param(12, engine) == "'12'"
assert a2.convert_bind_param([1,2,3], engine) == "'[1, 2, 3]'"
engine.storage.engine = EngineB()
b = engine.type_descriptor(sqltypes.Integer)
assert b.convert_bind_param(12, engine) == 'monkey'
assert b.convert_bind_param([1,2,3], engine) == 'monkey'
def test_type_engine_autoincrement(self):
engine = ProxyEngine()
dogs = Table('dogs', engine,
Column('dog_id', Integer, primary_key=True),
Column('breed', String),
Column('name', String))
class Dog(object):
pass
assign_mapper(Dog, dogs)
engine.connect(testbase.db_uri)
dogs.create()
try:
spot = Dog()
spot.breed = 'beagle'
spot.name = 'Spot'
rover = Dog()
rover.breed = 'spaniel'
rover.name = 'Rover'
objectstore.commit()
assert spot.dog_id > 0, "Spot did not get an id"
assert rover.dog_id != spot.dog_id
finally:
dogs.drop()
def test_type_proxy_schema_gen(self):
from sqlalchemy.databases.postgres import PGSchemaGenerator
engine = ProxyEngine()
lizards = Table('lizards', engine,
Column('id', Integer, primary_key=True),
Column('name', String))
# this doesn't really CONNECT to pg, just establishes pg as the
# actual engine so that we can determine that it gets the right
# answer
engine.connect('postgres://database=test&port=5432&host=127.0.0.1&user=scott&password=tiger')
sg = PGSchemaGenerator(engine)
id_spec = sg.get_column_specification(lizards.c.id)
assert id_spec == 'id SERIAL NOT NULL PRIMARY KEY'
if __name__ == "__main__":
testbase.main()
|