Merge pull request #4 from sprockets/create-sessions-wo-envvar

Create sessions from URL
This commit is contained in:
Gavin M. Roy 2014-11-14 09:36:53 -05:00
commit 534bfb0744
6 changed files with 56 additions and 15 deletions

View file

@ -1,3 +1,6 @@
-r requirements.txt
-r test-requirements.txt
detox>=0.9,<1
sphinx>=1.2,<2 sphinx>=1.2,<2
sphinx-rtd-theme>=0.1,<1.0 sphinx-rtd-theme>=0.1,<1.0
sphinxcontrib-httpdomain>=1.2,<2 sphinxcontrib-httpdomain>=1.2,<2

View file

@ -1,5 +1,7 @@
Version History Version History
--------------- ---------------
- 2.0.1 [2014-11-14]
- Add db_url parameter to both session class initializers.
- 2.0.0 [2014-10-07] - 2.0.0 [2014-10-07]
- Change the environment variable format to be URI based instead of a variable per host, port, dbname, etc. - Change the environment variable format to be URI based instead of a variable per host, port, dbname, etc.
- 1.0.1 [2014-09-05] - 1.0.1 [2014-09-05]

View file

@ -4,7 +4,7 @@ import setuptools
setuptools.setup( setuptools.setup(
name='sprockets.clients.postgresql', name='sprockets.clients.postgresql',
version='2.0.0', version='2.0.1',
description=('PostgreSQL client library wrapper providing environment ' description=('PostgreSQL client library wrapper providing environment '
'variable based configuration'), 'variable based configuration'),
long_description=codecs.open('README.rst', encoding='utf-8').read(), long_description=codecs.open('README.rst', encoding='utf-8').read(),

View file

@ -30,7 +30,7 @@ running on ``foohost``, port ``6000`` using the username ``bar`` and the
password ``baz``, connecting to the ``foo`` database. 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) __version__ = '.'.join(str(v) for v in version_info)
import logging import logging
@ -95,13 +95,18 @@ class Session(queries.Session):
:param queries.cursor: The cursor type to use :param queries.cursor: The cursor type to use
:param int pool_idle_ttl: How long idle pools keep connections open :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 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, def __init__(self, dbname,
cursor_factory=queries.RealDictCursor, cursor_factory=queries.RealDictCursor,
pool_idle_ttl=pool.DEFAULT_IDLE_TTL, pool_idle_ttl=pool.DEFAULT_IDLE_TTL,
pool_max_size=pool.DEFAULT_MAX_SIZE): pool_max_size=pool.DEFAULT_MAX_SIZE,
super(Session, self).__init__(_get_uri(dbname), db_url=None):
if db_url is None:
db_url = _get_uri(dbname)
super(Session, self).__init__(db_url,
cursor_factory, cursor_factory,
pool_idle_ttl, pool_idle_ttl,
pool_max_size) 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 int pool_max_size: The maximum size of the pool to use
:param tornado.ioloop.IOLoop ioloop: Pass in the instance of the tornado :param tornado.ioloop.IOLoop ioloop: Pass in the instance of the tornado
IOLoop you would like to use. Defaults to the global instance. 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, def __init__(self, dbname,
cursor_factory=queries.RealDictCursor, cursor_factory=queries.RealDictCursor,
pool_idle_ttl=pool.DEFAULT_IDLE_TTL, pool_idle_ttl=pool.DEFAULT_IDLE_TTL,
pool_max_size=tornado_session.DEFAULT_MAX_POOL_SIZE, pool_max_size=tornado_session.DEFAULT_MAX_POOL_SIZE,
io_loop=None): io_loop=None, db_url=None):
super(TornadoSession, self).__init__(_get_uri(dbname), if db_url is None:
db_url = _get_uri(dbname)
super(TornadoSession, self).__init__(db_url,
cursor_factory, cursor_factory,
pool_idle_ttl, pool_idle_ttl,
pool_max_size, pool_max_size,

View file

@ -11,7 +11,8 @@ except ImportError:
import unittest import unittest
from sprockets.clients import postgresql from sprockets.clients import postgresql
import queries import queries.pool
import queries.tornado_session
from tornado import testing from tornado import testing
@ -28,25 +29,38 @@ class TestGetURI(unittest.TestCase):
class TestSession(unittest.TestCase): class TestSession(unittest.TestCase):
@mock.patch('queries.session.Session.__init__') @mock.patch('queries.session.Session.__init__')
def setUp(self, mock_init): def test_session_invokes_queries_session(self, mock_init):
self.mock_init = mock_init
os.environ['PGSQL_TEST2'] = 'postgresql://foo:baz@db1:5433/bar' os.environ['PGSQL_TEST2'] = 'postgresql://foo:baz@db1:5433/bar'
self.session = postgresql.Session('test2') self.session = postgresql.Session('test2')
self.assertTrue(mock_init.called)
def test_session_invokes_queries_session(self): @mock.patch('queries.session.Session.__init__')
self.assertTrue(self.mock_init.called) 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): class TestTornadoSession(unittest.TestCase):
@mock.patch('queries.tornado_session.TornadoSession.__init__') @mock.patch('queries.tornado_session.TornadoSession.__init__')
def setUp(self, mock_init): def test_session_invokes_queries_session(self, mock_init):
self.mock_init = mock_init
os.environ['PGSQL_TEST3'] = 'postgresql://foo:baz@db1:5434/bar' os.environ['PGSQL_TEST3'] = 'postgresql://foo:baz@db1:5434/bar'
self.session = postgresql.TornadoSession('test3') self.session = postgresql.TornadoSession('test3')
self.assertTrue(mock_init.called)
def test_session_invokes_queries_session(self): @mock.patch('queries.tornado_session.TornadoSession.__init__')
self.assertTrue(self.mock_init.called) 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): class SessionIntegrationTests(unittest.TestCase):

13
tox.ini Normal file
View file

@ -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