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
|
from test.lib.testing import assert_raises, assert_raises_message, eq_
import ConfigParser
import StringIO
import sqlalchemy.engine.url as url
from sqlalchemy import create_engine, engine_from_config, exc
from sqlalchemy.engine import _coerce_config
import sqlalchemy as tsa
from test.lib import TestBase
class ParseConnectTest(TestBase):
def test_rfc1738(self):
for text in (
'dbtype://username:password@hostspec:110//usr/db_file.db',
'dbtype://username:password@hostspec/database',
'dbtype://username:password@hostspec',
'dbtype://username:password@/database',
'dbtype://username@hostspec',
'dbtype://username:password@127.0.0.1:1521',
'dbtype://hostspec/database',
'dbtype://hostspec',
'dbtype://hostspec/?arg1=val1&arg2=val2',
'dbtype:///database',
'dbtype:///:memory:',
'dbtype:///foo/bar/im/a/file',
'dbtype:///E:/work/src/LEM/db/hello.db',
'dbtype:///E:/work/src/LEM/db/hello.db?foo=bar&hoho=lala',
'dbtype://',
'dbtype://username:password@/db',
'dbtype:////usr/local/mailman/lists/_xtest@example.com/memb'
'ers.db',
'dbtype://username:apples%2Foranges@hostspec/mydatabase',
):
u = url.make_url(text)
assert u.drivername == 'dbtype'
assert u.username == 'username' or u.username is None
assert u.password == 'password' or u.password \
== 'apples/oranges' or u.password is None
assert u.host == 'hostspec' or u.host == '127.0.0.1' \
or not u.host
assert str(u) == text
class DialectImportTest(TestBase):
def test_import_base_dialects(self):
# the globals() somehow makes it for the exec() + nose3.
for name in (
'mysql',
'firebird',
'postgresql',
'sqlite',
'oracle',
'mssql',
):
exec ('from sqlalchemy.dialects import %s\ndialect = '
'%s.dialect()' % (name, name), globals())
eq_(dialect.name, name)
class CreateEngineTest(TestBase):
"""test that create_engine arguments of different types get
propagated properly"""
def test_connect_query(self):
dbapi = MockDBAPI(foober='12', lala='18', fooz='somevalue')
e = \
create_engine('postgresql://scott:tiger@somehost/test?foobe'
'r=12&lala=18&fooz=somevalue', module=dbapi,
_initialize=False)
c = e.connect()
def test_kwargs(self):
dbapi = MockDBAPI(foober=12, lala=18, hoho={'this': 'dict'},
fooz='somevalue')
e = \
create_engine('postgresql://scott:tiger@somehost/test?fooz='
'somevalue', connect_args={'foober': 12,
'lala': 18, 'hoho': {'this': 'dict'}},
module=dbapi, _initialize=False)
c = e.connect()
def test_coerce_config(self):
raw = r"""
[prefixed]
sqlalchemy.url=postgresql://scott:tiger@somehost/test?fooz=somevalue
sqlalchemy.convert_unicode=0
sqlalchemy.echo=false
sqlalchemy.echo_pool=1
sqlalchemy.max_overflow=2
sqlalchemy.pool_recycle=50
sqlalchemy.pool_size=2
sqlalchemy.pool_threadlocal=1
sqlalchemy.pool_timeout=10
[plain]
url=postgresql://scott:tiger@somehost/test?fooz=somevalue
convert_unicode=0
echo=0
echo_pool=1
max_overflow=2
pool_recycle=50
pool_size=2
pool_threadlocal=1
pool_timeout=10
"""
ini = ConfigParser.ConfigParser()
ini.readfp(StringIO.StringIO(raw))
expected = {
'url': 'postgresql://scott:tiger@somehost/test?fooz=somevalue',
'convert_unicode': 0,
'echo': False,
'echo_pool': True,
'max_overflow': 2,
'pool_recycle': 50,
'pool_size': 2,
'pool_threadlocal': True,
'pool_timeout': 10,
}
prefixed = dict(ini.items('prefixed'))
self.assert_(tsa.engine._coerce_config(prefixed, 'sqlalchemy.')
== expected)
plain = dict(ini.items('plain'))
self.assert_(tsa.engine._coerce_config(plain, '') == expected)
def test_engine_from_config(self):
dbapi = mock_dbapi
config = \
{'sqlalchemy.url': 'postgresql://scott:tiger@somehost/test'\
'?fooz=somevalue', 'sqlalchemy.pool_recycle': '50',
'sqlalchemy.echo': 'true'}
e = engine_from_config(config, module=dbapi, _initialize=False)
assert e.pool._recycle == 50
assert e.url \
== url.make_url('postgresql://scott:tiger@somehost/test?foo'
'z=somevalue')
assert e.echo is True
for param, values in [
('convert_unicode', ('true', 'false', 'force')),
('echo', ('true', 'false', 'debug')),
('echo_pool', ('true', 'false', 'debug')),
('use_native_unicode', ('true', 'false')),
]:
for value in values:
config = {
'sqlalchemy.url': 'postgresql://scott:tiger@somehost/test',
'sqlalchemy.%s' % param : value
}
cfg = _coerce_config(config, 'sqlalchemy.')
assert cfg[param] == {'true':True, 'false':False}.get(value, value)
def test_custom(self):
dbapi = MockDBAPI(foober=12, lala=18, hoho={'this': 'dict'},
fooz='somevalue')
def connect():
return dbapi.connect(foober=12, lala=18, fooz='somevalue',
hoho={'this': 'dict'})
# start the postgresql dialect, but put our mock DBAPI as the
# module instead of psycopg
e = create_engine('postgresql://', creator=connect,
module=dbapi, _initialize=False)
c = e.connect()
def test_recycle(self):
dbapi = MockDBAPI(foober=12, lala=18, hoho={'this': 'dict'},
fooz='somevalue')
e = create_engine('postgresql://', pool_recycle=472,
module=dbapi, _initialize=False)
assert e.pool._recycle == 472
def test_badargs(self):
assert_raises(exc.ArgumentError, create_engine, 'foobar://',
module=mock_dbapi)
# bad arg
assert_raises(TypeError, create_engine, 'postgresql://',
use_ansi=True, module=mock_dbapi)
# bad arg
assert_raises(
TypeError,
create_engine,
'oracle://',
lala=5,
use_ansi=True,
module=mock_dbapi,
)
assert_raises(TypeError, create_engine, 'postgresql://',
lala=5, module=mock_dbapi)
assert_raises(TypeError, create_engine, 'sqlite://', lala=5,
module=mock_sqlite_dbapi)
assert_raises(TypeError, create_engine, 'mysql+mysqldb://',
use_unicode=True, module=mock_dbapi)
# sqlite uses SingletonThreadPool which doesnt have max_overflow
assert_raises(TypeError, create_engine, 'sqlite://',
max_overflow=5, module=mock_sqlite_dbapi)
try:
e = create_engine('sqlite://', connect_args={'use_unicode'
: True}, convert_unicode=True)
except ImportError:
# no sqlite
pass
else:
# raises DBAPIerror due to use_unicode not a sqlite arg
assert_raises(tsa.exc.DBAPIError, e.connect)
def test_urlattr(self):
"""test the url attribute on ``Engine``."""
e = create_engine('mysql://scott:tiger@localhost/test',
module=mock_dbapi, _initialize=False)
u = url.make_url('mysql://scott:tiger@localhost/test')
e2 = create_engine(u, module=mock_dbapi, _initialize=False)
assert e.url.drivername == e2.url.drivername == 'mysql'
assert e.url.username == e2.url.username == 'scott'
assert e2.url is u
def test_poolargs(self):
"""test that connection pool args make it thru"""
e = create_engine(
'postgresql://',
creator=None,
pool_recycle=50,
echo_pool=None,
module=mock_dbapi,
_initialize=False,
)
assert e.pool._recycle == 50
# these args work for QueuePool
e = create_engine(
'postgresql://',
max_overflow=8,
pool_timeout=60,
poolclass=tsa.pool.QueuePool,
module=mock_dbapi,
_initialize=False,
)
# but not SingletonThreadPool
assert_raises(
TypeError,
create_engine,
'sqlite://',
max_overflow=8,
pool_timeout=60,
poolclass=tsa.pool.SingletonThreadPool,
module=mock_sqlite_dbapi,
_initialize=False,
)
class MockDBAPI(object):
def __init__(self, **kwargs):
self.kwargs = kwargs
self.paramstyle = 'named'
def connect(self, *args, **kwargs):
for k in self.kwargs:
assert k in kwargs, 'key %s not present in dictionary' % k
assert kwargs[k] == self.kwargs[k], \
'value %s does not match %s' % (kwargs[k],
self.kwargs[k])
return MockConnection()
class MockConnection(object):
def get_server_info(self):
return '5.0'
def close(self):
pass
def cursor(self):
return MockCursor()
class MockCursor(object):
def close(self):
pass
mock_dbapi = MockDBAPI()
mock_sqlite_dbapi = msd = MockDBAPI()
msd.version_info = msd.sqlite_version_info = 99, 9, 9
msd.sqlite_version = '99.9.9'
|