305
317
assert eng.dialect.returns_unicode_strings in (True, False)
320
class ConvenienceExecuteTest(fixtures.TablesTest):
322
def define_tables(cls, metadata):
323
cls.table = Table('exec_test', metadata,
324
Column('a', Integer),
325
Column('b', Integer),
329
def _trans_fn(self, is_transaction=False):
330
def go(conn, x, value=None):
332
conn = conn.connection
333
conn.execute(self.table.insert().values(a=x, b=value))
336
def _trans_rollback_fn(self, is_transaction=False):
337
def go(conn, x, value=None):
339
conn = conn.connection
340
conn.execute(self.table.insert().values(a=x, b=value))
341
raise Exception("breakage")
344
def _assert_no_data(self):
346
testing.db.scalar(self.table.count()), 0
349
def _assert_fn(self, x, value=None):
351
testing.db.execute(self.table.select()).fetchall(),
355
def test_transaction_engine_ctx_commit(self):
356
fn = self._trans_fn()
357
ctx = testing.db.begin()
358
testing.run_as_contextmanager(ctx, fn, 5, value=8)
359
self._assert_fn(5, value=8)
361
def test_transaction_engine_ctx_rollback(self):
362
fn = self._trans_rollback_fn()
363
ctx = testing.db.begin()
364
assert_raises_message(
367
testing.run_as_contextmanager, ctx, fn, 5, value=8
369
self._assert_no_data()
371
def test_transaction_tlocal_engine_ctx_commit(self):
372
fn = self._trans_fn()
373
engine = engines.testing_engine(options=dict(
374
strategy='threadlocal',
375
pool=testing.db.pool))
377
testing.run_as_contextmanager(ctx, fn, 5, value=8)
378
self._assert_fn(5, value=8)
380
def test_transaction_tlocal_engine_ctx_rollback(self):
381
fn = self._trans_rollback_fn()
382
engine = engines.testing_engine(options=dict(
383
strategy='threadlocal',
384
pool=testing.db.pool))
386
assert_raises_message(
389
testing.run_as_contextmanager, ctx, fn, 5, value=8
391
self._assert_no_data()
393
def test_transaction_connection_ctx_commit(self):
394
fn = self._trans_fn(True)
395
conn = testing.db.connect()
397
testing.run_as_contextmanager(ctx, fn, 5, value=8)
398
self._assert_fn(5, value=8)
400
def test_transaction_connection_ctx_rollback(self):
401
fn = self._trans_rollback_fn(True)
402
conn = testing.db.connect()
404
assert_raises_message(
407
testing.run_as_contextmanager, ctx, fn, 5, value=8
409
self._assert_no_data()
411
def test_connection_as_ctx(self):
412
fn = self._trans_fn()
413
ctx = testing.db.connect()
414
testing.run_as_contextmanager(ctx, fn, 5, value=8)
416
self._assert_fn(5, value=8)
418
@testing.fails_on('mysql+oursql', "oursql bug ? getting wrong rowcount")
419
def test_connect_as_ctx_noautocommit(self):
420
fn = self._trans_fn()
421
self._assert_no_data()
422
ctx = testing.db.connect().execution_options(autocommit=False)
423
testing.run_as_contextmanager(ctx, fn, 5, value=8)
425
self._assert_no_data()
427
def test_transaction_engine_fn_commit(self):
428
fn = self._trans_fn()
429
testing.db.transaction(fn, 5, value=8)
430
self._assert_fn(5, value=8)
432
def test_transaction_engine_fn_rollback(self):
433
fn = self._trans_rollback_fn()
434
assert_raises_message(
437
testing.db.transaction, fn, 5, value=8
439
self._assert_no_data()
441
def test_transaction_connection_fn_commit(self):
442
fn = self._trans_fn()
443
conn = testing.db.connect()
444
conn.transaction(fn, 5, value=8)
445
self._assert_fn(5, value=8)
447
def test_transaction_connection_fn_rollback(self):
448
fn = self._trans_rollback_fn()
449
conn = testing.db.connect()
452
conn.transaction, fn, 5, value=8
454
self._assert_no_data()
308
456
class CompiledCacheTest(fixtures.TestBase):
310
458
def setup_class(cls):