Changeset - 2a1e9b072046
[Not reviewed]
0 4 0
Lance Edgar (lance) - 10 years ago 2014-05-31 01:05:51
lance@edbob.org
Make `get_default_engine()` accept section arg; add tests.
4 files changed with 50 insertions and 18 deletions:
0 comments (0 inline, 0 general)
rattail/db/__init__.py
Show inline comments
 
#!/usr/bin/env python
 
# -*- coding: utf-8  -*-
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2012 Lance Edgar
 
#  Copyright © 2010-2014 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -30,19 +29,12 @@ import warnings
 

	
 
from sqlalchemy.orm import sessionmaker
 

	
 
from .util import get_engines
 
from .util import get_default_engine
 

	
 

	
 
Session = sessionmaker()
 

	
 

	
 
def get_default_engine(config):
 
    """
 
    Fetch the default SQLAlchemy database engine.
 
    """
 
    return get_engines(config).get('default')
 

	
 

	
 
def configure_session_factory(config, session_factory=None):
 
    """
 
    Configure a session factory using the provided settings.
rattail/db/sync/__init__.py
Show inline comments
 
#!/usr/bin/env python
 
# -*- coding: utf-8  -*-
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2012 Lance Edgar
 
#  Copyright © 2010-2014 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -23,7 +22,7 @@
 
################################################################################
 

	
 
"""
 
``rattail.db.sync`` -- Database Synchronization
 
Database Synchronization
 
"""
 

	
 
import sys
 
@@ -39,8 +38,8 @@ from sqlalchemy.exc import OperationalError
 

	
 
import edbob
 

	
 
from .. import model
 
from .. import get_engines
 
from rattail.db import model
 
from rattail.db.util import get_engines
 

	
 

	
 
log = logging.getLogger(__name__)
rattail/db/util.py
Show inline comments
 
#!/usr/bin/env python
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
@@ -116,3 +115,23 @@ def get_engines(config, section=None):
 
        warnings.warn(u"Defining database engines in [edbob.db] is deprecated; please "
 
                      u"use the [rattail.db] section instead.", DeprecationWarning)
 
    return engines
 

	
 

	
 
def get_default_engine(config, section=None):
 
    """
 
    Fetch the default database engine defined in the given config object for a
 
    given section.
 

	
 
    :param config: A ``ConfigParser`` instance containing app configuration.
 

	
 
    :type section: string
 
    :param section: Optional section name within which the configuration
 
       options are defined.  If not specified, ``'rattail.db'`` is assumed.
 

	
 
    :returns: A SQLAlchemy engine instance, or ``None``.
 

	
 
    .. note::
 
       This function calls :func:`get_engines()` for the heavy lifting; see
 
       that function for more details on how the engine configuration is read.
 
    """
 
    return get_engines(config, section=section).get(u'default')
tests/db/test_util.py
Show inline comments
 
@@ -85,3 +85,25 @@ class TestGetEngines(TestCase):
 
        self.assertEqual(sorted(engines.keys()), [u'host', u'store'])
 
        self.assertEqual(unicode(engines[u'host'].url), u'sqlite:///rattail.host.sqlite')
 
        self.assertEqual(unicode(engines[u'store'].url), u'sqlite:///rattail.store.sqlite')
 

	
 

	
 
class TestGetDefaultEngine(TestCase):
 

	
 
    def setUp(self):
 
        self.config = AppConfigParser(u'rattail')
 

	
 
    def test_default_engine_is_loaded_from_rattail_db_section_by_default(self):
 
        self.config.set(u'rattail.db', u'keys', u'default')
 
        self.config.set(u'rattail.db', u'default.url', u'sqlite://')
 
        engine = util.get_default_engine(self.config)
 
        self.assertEqual(unicode(engine.url), u'sqlite://')
 

	
 
    def test_default_engine_is_loaded_from_custom_section_if_specified(self):
 
        self.config.set(u'mycustomdb', u'keys', u'default')
 
        self.config.set(u'mycustomdb', u'default.url', u'sqlite://')
 
        engine = util.get_default_engine(self.config, section=u'mycustomdb')
 
        self.assertEqual(unicode(engine.url), u'sqlite://')
 

	
 
    def test_no_engine_is_returned_if_none_is_defined(self):
 
        engine = util.get_default_engine(self.config)
 
        self.assertTrue(engine is None)
0 comments (0 inline, 0 general)