def test_raw_lobs(self): engine = testing_engine(options=dict(auto_convert_lobs=False)) metadata = MetaData() t = Table( "z_test", metadata, Column("id", Integer, primary_key=True), Column("data", Text), Column("bindata", Binary) ) t.create(engine) try: engine.execute(t.insert(), id=1, data="this is text", bindata="this is binary") row = engine.execute(t.select()).fetchone() eq_(row["data"].read(), "this is text") eq_(row["bindata"].read(), "this is binary") finally: t.drop(engine)
def test_raw_lobs(self): engine = testing_engine(options=dict(auto_convert_lobs=False)) metadata = MetaData() t = Table("z_test", metadata, Column('id', Integer, primary_key=True), Column('data', Text), Column('bindata', Binary)) t.create(engine) try: engine.execute(t.insert(), id=1, data='this is text', bindata='this is binary') row = engine.execute(t.select()).fetchone() eq_(row['data'].read(), 'this is text') eq_(row['bindata'].read(), 'this is binary') finally: t.drop(engine)
def test_twophase(self): # TODO: mock up a failure condition here # to ensure a rollback succeeds mapper(User, users) mapper(Address, addresses) engine2 = engines.testing_engine() sess = create_session(autocommit=True, autoflush=False, twophase=True) sess.bind_mapper(User, testing.db) sess.bind_mapper(Address, engine2) sess.begin() u1 = User(name='u1') a1 = Address(email_address='u1@e') sess.add_all((u1, a1)) sess.commit() sess.close() engine2.dispose() assert users.count().scalar() == 1 assert addresses.count().scalar() == 1
def _create_testing_engine(options, file_config): from testlib import engines, testing global db db = engines.testing_engine(db_url, db_opts) testing.db = db
def test_proxy(self): stmts = [] cursor_stmts = [] class MyProxy(ConnectionProxy): def execute(self, conn, execute, clauseelement, *multiparams, **params): stmts.append((str(clauseelement), params, multiparams)) return execute(clauseelement, *multiparams, **params) def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): cursor_stmts.append((statement, parameters, None)) return execute(cursor, statement, parameters, context) def assert_stmts(expected, received): for stmt, params, posn in expected: if not received: assert False while received: teststmt, testparams, testmultiparams = received.pop(0) teststmt = re.compile(r'[\n\t ]+', re.M).sub(' ', teststmt).strip() if teststmt.startswith(stmt) and (testparams == params or testparams == posn): break for engine in ( engines.testing_engine(options=dict(proxy=MyProxy())), engines.testing_engine( options=dict(proxy=MyProxy(), strategy='threadlocal'))): m = MetaData(engine) t1 = Table( 't1', m, Column('c1', Integer, primary_key=True), Column('c2', String(50), default=func.lower('Foo'), primary_key=True)) m.create_all() try: t1.insert().execute(c1=5, c2='some data') t1.insert().execute(c1=6) assert engine.execute("select * from t1").fetchall() == [ (5, 'some data'), (6, 'foo') ] finally: m.drop_all() engine.dispose() compiled = [("CREATE TABLE t1", {}, None), ("INSERT INTO t1 (c1, c2)", { 'c2': 'some data', 'c1': 5 }, None), ("INSERT INTO t1 (c1, c2)", { 'c1': 6 }, None), ("select * from t1", {}, None), ("DROP TABLE t1", {}, None)] if engine.dialect.preexecute_pk_sequences: cursor = [("CREATE TABLE t1", {}, None), ("INSERT INTO t1 (c1, c2)", { 'c2': 'some data', 'c1': 5 }, [5, 'some data']), ("SELECT lower", { 'lower_2': 'Foo' }, ['Foo']), ("INSERT INTO t1 (c1, c2)", { 'c2': 'foo', 'c1': 6 }, [6, 'foo']), ("select * from t1", {}, None), ("DROP TABLE t1", {}, None)] else: cursor = [ ("CREATE TABLE t1", {}, ()), ("INSERT INTO t1 (c1, c2)", { 'c2': 'some data', 'c1': 5 }, [5, 'some data']), ("INSERT INTO t1 (c1, c2)", { 'c1': 6, "lower_2": "Foo" }, [6, "Foo" ]), # bind param name 'lower_2' might be incorrect ("select * from t1", {}, ()), ("DROP TABLE t1", {}, ()) ] assert_stmts(compiled, stmts) assert_stmts(cursor, cursor_stmts)
def test_fetch_single_arraysize(self): eng = testing_engine(options={"arraysize": 1}) self.assertEquals(eng.execute(binary_table.select()).fetchall(), [(i, stream) for i in range(1, 11)])
def setUp(self): self.sql = self.accum() opts = config.db_opts.copy() opts['strategy'] = 'mock' opts['executor'] = self.sql self.engine = engines.testing_engine(options=opts)
def test_proxy(self): stmts = [] cursor_stmts = [] class MyProxy(ConnectionProxy): def execute(self, conn, execute, clauseelement, *multiparams, **params): stmts.append( (str(clauseelement), params,multiparams) ) return execute(clauseelement, *multiparams, **params) def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): cursor_stmts.append( (statement, parameters, None) ) return execute(cursor, statement, parameters, context) def assert_stmts(expected, received): for stmt, params, posn in expected: if not received: assert False while received: teststmt, testparams, testmultiparams = received.pop(0) teststmt = re.compile(r'[\n\t ]+', re.M).sub(' ', teststmt).strip() if teststmt.startswith(stmt) and (testparams==params or testparams==posn): break for engine in ( engines.testing_engine(options=dict(proxy=MyProxy())), engines.testing_engine(options=dict(proxy=MyProxy(), strategy='threadlocal')) ): m = MetaData(engine) t1 = Table('t1', m, Column('c1', Integer, primary_key=True), Column('c2', String(50), default=func.lower('Foo'), primary_key=True)) m.create_all() try: t1.insert().execute(c1=5, c2='some data') t1.insert().execute(c1=6) assert engine.execute("select * from t1").fetchall() == [(5, 'some data'), (6, 'foo')] finally: m.drop_all() engine.dispose() compiled = [ ("CREATE TABLE t1", {}, None), ("INSERT INTO t1 (c1, c2)", {'c2': 'some data', 'c1': 5}, None), ("INSERT INTO t1 (c1, c2)", {'c1': 6}, None), ("select * from t1", {}, None), ("DROP TABLE t1", {}, None) ] if engine.dialect.preexecute_pk_sequences: cursor = [ ("CREATE TABLE t1", {}, None), ("INSERT INTO t1 (c1, c2)", {'c2': 'some data', 'c1': 5}, [5, 'some data']), ("SELECT lower", {'lower_2':'Foo'}, ['Foo']), ("INSERT INTO t1 (c1, c2)", {'c2': 'foo', 'c1': 6}, [6, 'foo']), ("select * from t1", {}, None), ("DROP TABLE t1", {}, None) ] else: cursor = [ ("CREATE TABLE t1", {}, ()), ("INSERT INTO t1 (c1, c2)", {'c2': 'some data', 'c1': 5}, [5, 'some data']), ("INSERT INTO t1 (c1, c2)", {'c1': 6, "lower_2":"Foo"}, [6, "Foo"]), # bind param name 'lower_2' might be incorrect ("select * from t1", {}, ()), ("DROP TABLE t1", {}, ()) ] assert_stmts(compiled, stmts) assert_stmts(cursor, cursor_stmts)
def create_engine(self): return engines.testing_engine(options=dict(strategy='threadlocal'))
def test_fetch_single_arraysize(self): eng = testing_engine(options={'arraysize': 1}) self.assertEquals( eng.execute(binary_table.select()).fetchall(), [(i, stream) for i in range(1, 11)], )