From: Jason Kirtland Date: Wed, 29 Aug 2007 23:17:36 +0000 (+0000) Subject: Extended 'engine_from_config' coercion for QueuePool size / overflow. [ticket:763] X-Git-Tag: rel_0_4beta6~70 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d9d21cc14982654cadd7678d44e14df62d812d2;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Extended 'engine_from_config' coercion for QueuePool size / overflow. [ticket:763] Added a set of coercion tests. --- diff --git a/CHANGES b/CHANGES index be5c274674..239ffaa7b5 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,9 @@ CHANGES 0.4.0beta5 ---------- +- Extended 'engine_from_config' coercion for QueuePool size / overflow. + [ticket:763] + - mysql views can be reflected again. [ticket:748] - AssociationProxy can now take custom getters and setters. @@ -19,6 +22,7 @@ CHANGES - [ticket:748] - [ticket:760] - [ticket:762] + - [ticket:763] 0.4.0beta4 ---------- diff --git a/lib/sqlalchemy/engine/__init__.py b/lib/sqlalchemy/engine/__init__.py index ae32b40bbc..1c1107d3da 100644 --- a/lib/sqlalchemy/engine/__init__.py +++ b/lib/sqlalchemy/engine/__init__.py @@ -166,21 +166,30 @@ def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): 'prefix' argument indicates the prefix to be searched for. A select set of keyword arguments will be "coerced" to their - expected type based on string values. in a future release, this - functionality will be expanded to include dialect-specific + expected type based on string values. In a future release, this + functionality will be expanded and include dialect-specific arguments. """ - opts = dict([(key[len(prefix):], configuration[key]) + opts = _coerce_config(configuration, prefix) + opts.update(kwargs) + url = opts.pop('url') + return create_engine(url, **opts) + +def _coerce_config(configuration, prefix): + """Convert configuration values to expected types.""" + + options = dict([(key[len(prefix):], configuration[key]) for key in configuration if key.startswith(prefix)]) - for opt, type_ in ( + for option, type_ in ( ('convert_unicode', bool), ('pool_timeout', int), ('echo', bool), ('echo_pool', bool), ('pool_recycle', int), + ('pool_size', int), + ('max_overflow', int), + ('pool_threadlocal', bool), ): - util.coerce_kw_type(opts, opt, type_) - opts.update(kwargs) - url = opts.pop('url') - return create_engine(url, **opts) + util.coerce_kw_type(options, option, type_) + return options diff --git a/test/engine/parseconnect.py b/test/engine/parseconnect.py index 157afeb2fc..5bf9512fb8 100644 --- a/test/engine/parseconnect.py +++ b/test/engine/parseconnect.py @@ -1,6 +1,7 @@ import testbase +import ConfigParser, StringIO from sqlalchemy import * -from sqlalchemy import exceptions, pool +from sqlalchemy import exceptions, pool, engine import sqlalchemy.engine.url as url from testlib import * @@ -52,12 +53,56 @@ class CreateEngineTest(PersistTest): e = create_engine('postgres://scott:tiger@somehost/test?fooz=somevalue', connect_args={'foober':12, 'lala':18, 'hoho':{'this':'dict'}}, module=dbapi) c = e.connect() + def test_coerce_config(self): + raw = r""" +[prefixed] +sqlalchemy.url=postgres://scott:tiger@somehost/test?fooz=somevalue +sqlalchemy.convert_unicode=0 +sqlalchemy.echo=0 +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=postgres://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': 'postgres://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_(engine._coerce_config(prefixed, 'sqlalchemy.') == expected) + + plain = dict(ini.items('plain')) + self.assert_(engine._coerce_config(plain, '') == expected) + def test_engine_from_config(self): dbapi = MockDBAPI() config = { 'sqlalchemy.url':'postgres://scott:tiger@somehost/test?fooz=somevalue', - 'sqlalchemy.pool_recycle':50 + 'sqlalchemy.pool_recycle':'50' } e = engine_from_config(config, module=dbapi)