From 336c47dd9ab98e81124240b12c6f7b9935390565 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Fri, 14 Nov 2014 08:11:07 -0500 Subject: [PATCH 1/3] Add db_url parameter to session classes. --- sprockets/clients/postgresql/__init__.py | 17 ++++++++++--- tests.py | 32 +++++++++++++++++------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/sprockets/clients/postgresql/__init__.py b/sprockets/clients/postgresql/__init__.py index a20eced..b3b9da5 100644 --- a/sprockets/clients/postgresql/__init__.py +++ b/sprockets/clients/postgresql/__init__.py @@ -95,13 +95,18 @@ class Session(queries.Session): :param queries.cursor: The cursor type to use :param int pool_idle_ttl: How long idle pools keep connections open :param int pool_max_size: The maximum size of the pool to use + :param str db_url: Optional database connection URL. Use this when + you need to connect to a database that is only known at runtime. """ def __init__(self, dbname, cursor_factory=queries.RealDictCursor, pool_idle_ttl=pool.DEFAULT_IDLE_TTL, - pool_max_size=pool.DEFAULT_MAX_SIZE): - super(Session, self).__init__(_get_uri(dbname), + pool_max_size=pool.DEFAULT_MAX_SIZE, + db_url=None): + if db_url is None: + db_url = _get_uri(dbname) + super(Session, self).__init__(db_url, cursor_factory, pool_idle_ttl, pool_max_size) @@ -125,14 +130,18 @@ class TornadoSession(tornado_session.TornadoSession): :param int pool_max_size: The maximum size of the pool to use :param tornado.ioloop.IOLoop ioloop: Pass in the instance of the tornado IOLoop you would like to use. Defaults to the global instance. + :param str db_url: Optional database connection URL. Use this when + you need to connect to a database that is only known at runtime. """ def __init__(self, dbname, cursor_factory=queries.RealDictCursor, pool_idle_ttl=pool.DEFAULT_IDLE_TTL, pool_max_size=tornado_session.DEFAULT_MAX_POOL_SIZE, - io_loop=None): - super(TornadoSession, self).__init__(_get_uri(dbname), + io_loop=None, db_url=None): + if db_url is None: + db_url = _get_uri(dbname) + super(TornadoSession, self).__init__(db_url, cursor_factory, pool_idle_ttl, pool_max_size, diff --git a/tests.py b/tests.py index 005afe6..0557ad7 100644 --- a/tests.py +++ b/tests.py @@ -11,7 +11,8 @@ except ImportError: import unittest from sprockets.clients import postgresql -import queries +import queries.pool +import queries.tornado_session from tornado import testing @@ -28,25 +29,38 @@ class TestGetURI(unittest.TestCase): class TestSession(unittest.TestCase): @mock.patch('queries.session.Session.__init__') - def setUp(self, mock_init): - self.mock_init = mock_init + def test_session_invokes_queries_session(self, mock_init): os.environ['PGSQL_TEST2'] = 'postgresql://foo:baz@db1:5433/bar' self.session = postgresql.Session('test2') + self.assertTrue(mock_init.called) - def test_session_invokes_queries_session(self): - self.assertTrue(self.mock_init.called) + @mock.patch('queries.session.Session.__init__') + def test_session_invokes_queries_session_with_url(self, mock_init): + self.session = postgresql.Session('test6', + db_url='postgresql://localhost/b') + mock_init.assert_called_once_with( + 'postgresql://localhost/b', queries.RealDictCursor, + queries.pool.DEFAULT_IDLE_TTL, queries.pool.DEFAULT_MAX_SIZE, + ) class TestTornadoSession(unittest.TestCase): @mock.patch('queries.tornado_session.TornadoSession.__init__') - def setUp(self, mock_init): - self.mock_init = mock_init + def test_session_invokes_queries_session(self, mock_init): os.environ['PGSQL_TEST3'] = 'postgresql://foo:baz@db1:5434/bar' self.session = postgresql.TornadoSession('test3') + self.assertTrue(mock_init.called) - def test_session_invokes_queries_session(self): - self.assertTrue(self.mock_init.called) + @mock.patch('queries.tornado_session.TornadoSession.__init__') + def test_session_invokes_queries_session_with_url(self, mock_init): + self.session = postgresql.TornadoSession( + 'test7', db_url='postgresql://localhost/b') + mock_init.assert_called_once_with( + 'postgresql://localhost/b', queries.RealDictCursor, + queries.pool.DEFAULT_IDLE_TTL, + queries.tornado_session.DEFAULT_MAX_POOL_SIZE, None, + ) class SessionIntegrationTests(unittest.TestCase): From 4e47fce5c71acbba36cc1ff3d09bbbe7e26e426f Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Fri, 14 Nov 2014 08:13:45 -0500 Subject: [PATCH 2/3] Add tox for local multi-version testing. --- dev-requirements.txt | 3 +++ tox.ini | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tox.ini diff --git a/dev-requirements.txt b/dev-requirements.txt index d6b937f..3c9f11e 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1,6 @@ +-r requirements.txt +-r test-requirements.txt +detox>=0.9,<1 sphinx>=1.2,<2 sphinx-rtd-theme>=0.1,<1.0 sphinxcontrib-httpdomain>=1.2,<2 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..ccd1f70 --- /dev/null +++ b/tox.ini @@ -0,0 +1,13 @@ +[tox] +envlist = py26,py27,py33,py34 + +[testenv] +deps = + -rrequirements.txt + -rtest-requirements.txt +commands = {envbindir}/nosetests + +[testenv:py26] +deps = + {[testenv]deps} + unittest2 From 233e1e12956b2dd8d0562ac7d23888e844b99c42 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Fri, 14 Nov 2014 08:14:46 -0500 Subject: [PATCH 3/3] Bump version to 2.0.1. --- docs/history.rst | 2 ++ setup.py | 2 +- sprockets/clients/postgresql/__init__.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/history.rst b/docs/history.rst index 4d9f54b..d7793d9 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -1,5 +1,7 @@ Version History --------------- +- 2.0.1 [2014-11-14] + - Add db_url parameter to both session class initializers. - 2.0.0 [2014-10-07] - Change the environment variable format to be URI based instead of a variable per host, port, dbname, etc. - 1.0.1 [2014-09-05] diff --git a/setup.py b/setup.py index 326f134..838519a 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ import setuptools setuptools.setup( name='sprockets.clients.postgresql', - version='2.0.0', + version='2.0.1', description=('PostgreSQL client library wrapper providing environment ' 'variable based configuration'), long_description=codecs.open('README.rst', encoding='utf-8').read(), diff --git a/sprockets/clients/postgresql/__init__.py b/sprockets/clients/postgresql/__init__.py index b3b9da5..0c4c8cb 100644 --- a/sprockets/clients/postgresql/__init__.py +++ b/sprockets/clients/postgresql/__init__.py @@ -30,7 +30,7 @@ running on ``foohost``, port ``6000`` using the username ``bar`` and the password ``baz``, connecting to the ``foo`` database. """ -version_info = (2, 0, 0) +version_info = (2, 0, 1) __version__ = '.'.join(str(v) for v in version_info) import logging