[Sqlalchemy-commits] [1429] sqlalchemy/branches/schema/test: - Fixed ProxyEngine tests (use MetaData
Brought to you by:
zzzeek
From: <co...@sq...> - 2006-05-08 22:10:17
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1429] sqlalchemy/branches/schema/test: - Fixed ProxyEngine tests (use MetaData, new Session API, etc).</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1429</dd> <dt>Author</dt> <dd>niemeyer</dd> <dt>Date</dt> <dd>2006-05-08 17:10:00 -0500 (Mon, 08 May 2006)</dd> </dl> <h3>Log Message</h3> <pre>- Fixed ProxyEngine tests (use MetaData, new Session API, etc). - Removed unneeded imports in sqlalchemy.ext.proxy. - Removed execute_compiled() and compiler() methods from BaseProxyEngine. There's no need for them to be special cased.</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemalibsqlalchemyextproxypy">sqlalchemy/branches/schema/lib/sqlalchemy/ext/proxy.py</a></li> <li><a href="#sqlalchemybranchesschematestproxy_enginepy">sqlalchemy/branches/schema/test/proxy_engine.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemalibsqlalchemyextproxypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/ext/proxy.py (1428 => 1429)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/ext/proxy.py 2006-05-08 19:00:51 UTC (rev 1428) +++ sqlalchemy/branches/schema/lib/sqlalchemy/ext/proxy.py 2006-05-08 22:10:00 UTC (rev 1429) </span><span class="lines">@@ -5,15 +5,10 @@ </span><span class="cx"> </span><span class="cx"> from sqlalchemy import sql </span><span class="cx"> from sqlalchemy.engine import create_engine </span><del>-from sqlalchemy.types import TypeEngine -import sqlalchemy.schema as schema -import thread, weakref </del><span class="cx"> </span><span class="cx"> </span><span class="cx"> class BaseProxyEngine(sql.Engine): </span><del>- ''' - Basis for all proxy engines - ''' </del><ins>+ """Basis for all proxy engines.""" </ins><span class="cx"> </span><span class="cx"> def get_engine(self): </span><span class="cx"> raise NotImplementedError </span><span class="lines">@@ -23,28 +18,18 @@ </span><span class="cx"> </span><span class="cx"> engine = property(lambda s:s.get_engine(), lambda s,e:s.set_engine(e)) </span><span class="cx"> </span><del>- def execute_compiled(self, *args, **kwargs): - return self.get_engine().execute_compiled(*args, **kwargs) - def compiler(self, *args, **kwargs): - return self.get_engine().compiler(*args, **kwargs) - - def hash_key(self): - return "%s(%s)" % (self.__class__.__name__, id(self)) - </del><span class="cx"> def __getattr__(self, attr): </span><span class="cx"> # call get_engine() to give subclasses a chance to change </span><span class="cx"> # connection establishment behavior </span><del>- e= self.get_engine() </del><ins>+ e = self.get_engine() </ins><span class="cx"> if e is not None: </span><span class="cx"> return getattr(e, attr) </span><del>- raise AttributeError('No connection established in ProxyEngine: ' - ' no access to %s' % attr) </del><ins>+ raise AttributeError("No connection established in ProxyEngine: " + " no access to %s" % attr) </ins><span class="cx"> </span><span class="cx"> </span><span class="cx"> class AutoConnectEngine(BaseProxyEngine): </span><del>- ''' - An SQLEngine proxy that automatically connects when necessary. - ''' </del><ins>+ """An SQLEngine proxy that automatically connects when necessary.""" </ins><span class="cx"> </span><span class="cx"> def __init__(self, dburi, **kwargs): </span><span class="cx"> BaseProxyEngine.__init__(self) </span><span class="lines">@@ -62,13 +47,11 @@ </span><span class="cx"> return self._engine </span><span class="cx"> </span><span class="cx"> </span><del>- </del><span class="cx"> class ProxyEngine(BaseProxyEngine): </span><ins>+ """Engine proxy for lazy and late initialization. + + This engine will delegate access to a real engine set with connect(). </ins><span class="cx"> """ </span><del>- SQLEngine proxy. Supports lazy and late initialization by - delegating to a real engine (set with connect()), and using proxy - classes for TypeEngine. - """ </del><span class="cx"> </span><span class="cx"> def __init__(self, **kwargs): </span><span class="cx"> BaseProxyEngine.__init__(self) </span><span class="lines">@@ -79,8 +62,8 @@ </span><span class="cx"> self.kwargs = kwargs </span><span class="cx"> </span><span class="cx"> def connect(self, *args, **kwargs): </span><del>- """Establish connection to a real engine. - """ </del><ins>+ """Establish connection to a real engine.""" + </ins><span class="cx"> kwargs.update(self.kwargs) </span><span class="cx"> if not kwargs: </span><span class="cx"> key = repr(args) </span><span class="lines">@@ -100,7 +83,7 @@ </span><span class="cx"> </span><span class="cx"> def get_engine(self): </span><span class="cx"> if self.storage.engine is None: </span><del>- raise AttributeError('No connection established') </del><ins>+ raise AttributeError("No connection established") </ins><span class="cx"> return self.storage.engine </span><span class="cx"> </span><span class="cx"> def set_engine(self, engine): </span></span></pre></div> <a id="sqlalchemybranchesschematestproxy_enginepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/test/proxy_engine.py (1428 => 1429)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/test/proxy_engine.py 2006-05-08 19:00:51 UTC (rev 1428) +++ sqlalchemy/branches/schema/test/proxy_engine.py 2006-05-08 22:10:00 UTC (rev 1429) </span><span class="lines">@@ -1,3 +1,5 @@ </span><ins>+import os + </ins><span class="cx"> from sqlalchemy import * </span><span class="cx"> from sqlalchemy.ext.proxy import ProxyEngine </span><span class="cx"> </span><span class="lines">@@ -3,5 +5,4 @@ </span><span class="cx"> from testbase import PersistTest </span><span class="cx"> import testbase </span><del>-import os </del><span class="cx"> </span><span class="cx"> # </span><span class="lines">@@ -12,7 +13,9 @@ </span><span class="cx"> </span><span class="cx"> </span><span class="cx"> module_engine = ProxyEngine(echo=testbase.echo) </span><del>-users = Table('users', module_engine, </del><ins>+module_metadata = MetaData() + +users = Table('users', module_metadata, </ins><span class="cx"> Column('user_id', Integer, primary_key=True), </span><span class="cx"> Column('user_name', String(16)), </span><span class="cx"> Column('password', String(20)) </span><span class="lines">@@ -21,10 +24,13 @@ </span><span class="cx"> class User(object): </span><span class="cx"> pass </span><span class="cx"> </span><ins>+User.mapper = mapper(User, users) </ins><span class="cx"> </span><ins>+ </ins><span class="cx"> class ConstructTest(PersistTest): </span><span class="cx"> """tests that we can build SQL constructs without engine-specific parameters, particulary </span><span class="cx"> oid_column, being needed, as the proxy engine is usually not connected yet.""" </span><ins>+ </ins><span class="cx"> def test_join(self): </span><span class="cx"> engine = ProxyEngine() </span><span class="cx"> t = Table('table1', engine, </span><span class="lines">@@ -33,47 +39,46 @@ </span><span class="cx"> Column('col2', Integer, ForeignKey('table1.col1'))) </span><span class="cx"> j = join(t, t2) </span><span class="cx"> </span><ins>+ </ins><span class="cx"> class ProxyEngineTest1(PersistTest): </span><span class="cx"> </span><del>- def setUp(self): - clear_mappers() - objectstore.clear() - </del><span class="cx"> def test_engine_connect(self): </span><span class="cx"> # connect to a real engine </span><span class="cx"> module_engine.connect(testbase.db_uri) </span><del>- users.create() - assign_mapper(User, users) </del><ins>+ module_metadata.create_all(module_engine) + + session = create_session(bind_to=module_engine) </ins><span class="cx"> try: </span><del>- trans = objectstore.begin() </del><span class="cx"> </span><span class="cx"> user = User() </span><span class="cx"> user.user_name='fred' </span><span class="cx"> user.password='*' </span><del>- trans.commit() </del><span class="cx"> </span><ins>+ session.save(user) + session.flush() + + query = session.query(User) + </ins><span class="cx"> # select </span><del>- sqluser = User.select_by(user_name='fred')[0] </del><ins>+ sqluser = query.select_by(user_name='fred')[0] </ins><span class="cx"> assert sqluser.user_name == 'fred' </span><span class="cx"> </span><span class="cx"> # modify </span><span class="cx"> sqluser.user_name = 'fred jones' </span><span class="cx"> </span><del>- # commit - saves everything that changed - objectstore.commit() </del><ins>+ # flush - saves everything that changed + session.flush() </ins><span class="cx"> </span><del>- allusers = [ user.user_name for user in User.select() ] - assert allusers == [ 'fred jones' ] </del><ins>+ allusers = [ user.user_name for user in query.select() ] + assert allusers == ['fred jones'] + </ins><span class="cx"> finally: </span><del>- users.drop() </del><ins>+ module_metadata.drop_all(module_engine) </ins><span class="cx"> </span><ins>+ </ins><span class="cx"> class ThreadProxyTest(PersistTest): </span><del>- def setUp(self): - assign_mapper(User, users) - def tearDown(self): - clear_mappers() </del><ins>+ </ins><span class="cx"> def tearDownAll(self): </span><del>- pass </del><span class="cx"> os.remove('threadtesta.db') </span><span class="cx"> os.remove('threadtestb.db') </span><span class="cx"> </span><span class="lines">@@ -92,23 +97,26 @@ </span><span class="cx"> </span><span class="cx"> try: </span><span class="cx"> module_engine.connect(db_uri) </span><del>- users.create() </del><ins>+ module_metadata.create_all(module_engine) </ins><span class="cx"> try: </span><del>- trans = objectstore.begin() </del><ins>+ session = create_session(bind_to=module_engine) </ins><span class="cx"> </span><del>- all = User.select()[:] </del><ins>+ query = session.query(User) + + all = list(query.select()) </ins><span class="cx"> assert all == [] </span><span class="cx"> </span><span class="cx"> u = User() </span><span class="cx"> u.user_name = uname </span><span class="cx"> u.password = 'whatever' </span><del>- trans.commit() </del><span class="cx"> </span><del>- names = [ us.user_name for us in User.select() ] - assert names == [ uname ] </del><ins>+ session.save(u) + session.flush() + + names = [u.user_name for u in query.select()] + assert names == [uname] </ins><span class="cx"> finally: </span><del>- users.drop() - module_engine.dispose() </del><ins>+ module_metadata.drop_all(module_engine) </ins><span class="cx"> except Exception, e: </span><span class="cx"> import traceback </span><span class="cx"> traceback.print_exc() </span><span class="lines">@@ -119,8 +127,8 @@ </span><span class="cx"> </span><span class="cx"> # NOTE: I'm not sure how to give the test runner the option to </span><span class="cx"> # override these uris, or how to safely clear them after test runs </span><del>- a = Thread(target=run('sqlite://filename=threadtesta.db', 'jim', qa)) - b = Thread(target=run('sqlite://filename=threadtestb.db', 'joe', qb)) </del><ins>+ a = Thread(target=run('sqlite:///threadtesta.db', 'jim', qa)) + b = Thread(target=run('sqlite:///threadtestb.db', 'joe', qb)) </ins><span class="cx"> </span><span class="cx"> a.start() </span><span class="cx"> b.start() </span><span class="lines">@@ -134,12 +142,9 @@ </span><span class="cx"> if res != False: </span><span class="cx"> raise res </span><span class="cx"> </span><ins>+ </ins><span class="cx"> class ProxyEngineTest2(PersistTest): </span><span class="cx"> </span><del>- def setUp(self): - clear_mappers() - objectstore.clear() - </del><span class="cx"> def test_table_singleton_a(self): </span><span class="cx"> """set up for table singleton check </span><span class="cx"> """ </span><span class="lines">@@ -153,9 +158,10 @@ </span><span class="cx"> Column('cat_name', String)) </span><span class="cx"> </span><span class="cx"> engine.connect(testbase.db_uri) </span><del>- cats.create() - cats.drop() </del><span class="cx"> </span><ins>+ cats.create(engine) + cats.drop(engine) + </ins><span class="cx"> ProxyEngineTest2.cats_table_a = cats </span><span class="cx"> assert isinstance(cats, Table) </span><span class="cx"> </span><span class="lines">@@ -179,141 +185,8 @@ </span><span class="cx"> # this will fail because the old reference's local storage will </span><span class="cx"> # not have the default attributes </span><span class="cx"> engine.connect(testbase.db_uri) </span><del>- cats.create() - cats.drop() </del><ins>+ cats.create(engine) + cats.drop(engine) </ins><span class="cx"> </span><del>- 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 isinstance(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 = sqltypes.Integer().engine_impl(engine) - assert a.convert_bind_param(12, engine) == 24 - assert a.convert_bind_param([1,2,3], engine) == [1, 2, 3, 1, 2, 3] - - a2 = sqltypes.String().engine_impl(engine) - 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 = sqltypes.Integer().engine_impl(engine) - 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' - - </del><span class="cx"> if __name__ == "__main__": </span><span class="cx"> testbase.main() </span><del>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - </del></span></pre> </div> </div> </body> </html> |