]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Extended 'engine_from_config' coercion for QueuePool size / overflow. [ticket:763]
authorJason Kirtland <jek@discorporate.us>
Wed, 29 Aug 2007 23:17:36 +0000 (23:17 +0000)
committerJason Kirtland <jek@discorporate.us>
Wed, 29 Aug 2007 23:17:36 +0000 (23:17 +0000)
Added a set of coercion tests.

CHANGES
lib/sqlalchemy/engine/__init__.py
test/engine/parseconnect.py

diff --git a/CHANGES b/CHANGES
index be5c2746749046f8fccb5455fdec1b2f6a5153fc..239ffaa7b51fea956219862265059349791f50c7 100644 (file)
--- 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
 ----------
index ae32b40bbc325eab5cf27dbd2b71717ba63c30c1..1c1107d3da87d3df7ed542c76ca810c8d0ecc781 100644 (file)
@@ -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
index 157afeb2fc02f95a24092a4869dc7ff5602f3f7e..5bf9512fb891df5e48b30d195cb51c32f031d69b 100644 (file)
@@ -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)