Changeset - 23ee011f0419
[Not reviewed]
0 4 0
Lance Edgar (lance) - 11 years ago 2014-01-26 16:55:47
lance@edbob.org
Make `get_sync_engines()` require a config object.
4 files changed with 17 insertions and 16 deletions:
0 comments (0 inline, 0 general)
rattail/db/sync/__init__.py
Show inline comments
 
@@ -25,59 +25,62 @@
 
"""
 
``rattail.db.sync`` -- Database Synchronization
 
"""
 

	
 
import sys
 
import time
 
import logging
 

	
 
if sys.platform == 'win32': # pragma no cover
 
    import win32api
 

	
 
import sqlalchemy.exc
 
from sqlalchemy.orm import sessionmaker, class_mapper
 
from sqlalchemy.exc import OperationalError
 

	
 
import edbob
 

	
 
from .. import model
 
from .. import get_engines
 

	
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
def get_sync_engines():
 
    keys = edbob.config.get('rattail.db', 'syncs')
 
def get_sync_engines(config):
 
    """
 
    Fetch the database engines to which data should be synchronized.
 
    """
 
    keys = config.get('rattail.db', 'syncs')
 
    if not keys:
 
        return None
 

	
 
    engines = get_engines(edbob.config)
 
    engines = get_engines(config)
 
    sync_engines = {}
 
    for key in keys.split(','):
 
        key = key.strip()
 
        sync_engines[key] = engines[key]
 
    log.debug("get_sync_engines: Found engine keys: %s" % ','.join(sync_engines.keys()))
 
    log.debug("get_sync_engines: found engine keys: {0}".format(','.join(sync_engines.keys())))
 
    return sync_engines
 

	
 

	
 
class Synchronizer(object):
 
    """
 
    Default implementation of database synchronization logic.  Subclass this if
 
    you have special processing needs.
 
    """
 

	
 
    # This defines the `model` module which will be used to obtain references
 
    # to model classes (`Product` etc.).  If you need to synchronize custom
 
    # model classes of which Rattail is not aware, you must override this.
 
    # Note that the module you specify must be a superset of Rattail.
 
    model = model
 

	
 
    def __init__(self, local_engine, remote_engines):
 
        self.Session = sessionmaker()
 
        self.local_engine = local_engine
 
        self.remote_engines = remote_engines
 

	
 
    def loop(self):
 
        log.info("Synchronizer.loop: using remote engines: {0}".format(
 
                ', '.join(self.remote_engines.iterkeys())))
 
        while True:
rattail/db/sync/linux.py
Show inline comments
 
@@ -15,49 +15,49 @@
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
 
#  more details.
 
#
 
#  You should have received a copy of the GNU Affero General Public License
 
#  along with Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 

	
 
"""
 
``rattail.db.sync.linux`` -- Database Synchronization for Linux
 
"""
 

	
 
import edbob
 

	
 
from ...daemon import Daemon
 
from .. import get_default_engine
 
from . import get_sync_engines, synchronize_changes
 

	
 

	
 
class SyncDaemon(Daemon):
 

	
 
    def run(self):
 
        remote_engines = get_sync_engines()
 
        remote_engines = get_sync_engines(edbob.config)
 
        if remote_engines:
 
            local_engine = get_default_engine(edbob.config)
 
            synchronize_changes(local_engine, remote_engines)
 

	
 

	
 
def get_daemon(pidfile=None):
 
    """
 
    Get a :class:`SyncDaemon` instance.
 
    """
 

	
 
    if pidfile is None:
 
        pidfile = edbob.config.get('rattail.db', 'sync.pid_path',
 
                                   default='/var/run/rattail/dbsync.pid')
 
    return SyncDaemon(pidfile)
 

	
 

	
 
def start_daemon(pidfile=None, daemonize=True):
 
    """
 
    Start the database synchronization daemon.
 
    """
 

	
 
    get_daemon(pidfile).start(daemonize)
 

	
 

	
tests/db/sync/test_init.py
Show inline comments
 
@@ -312,58 +312,56 @@ class SynchronizerTests(SyncTestCase):
 
        self.assertEqual(session.query(model.ProductCost).count(), 0)
 
        session.rollback()
 
        session.close()
 

	
 
    def test_delete_CustomerGroup(self):
 
        synchronizer = sync.Synchronizer(self.local_engine, self.remote_engines)
 

	
 
        session = self.Session(bind=self.local_engine)
 
        group = model.CustomerGroup()
 
        customer = model.Customer()
 
        customer.groups.append(group)
 
        session.add(customer)
 
        session.flush()
 
        assignment = session.query(model.CustomerGroupAssignment).one()
 
        self.assertEqual(assignment.customer_uuid, customer.uuid)
 
        self.assertEqual(assignment.group_uuid, group.uuid)
 
        synchronizer.delete_CustomerGroup(session, group)
 
        self.assertEqual(session.query(model.CustomerGroupAssignment).count(), 0)
 
        session.rollback()
 
        session.close()
 

	
 

	
 
class ModuleTests(TestCase):
 

	
 
    @patch.multiple('rattail.db.sync', edbob=DEFAULT, get_engines=DEFAULT)
 
    def test_get_sync_engines(self, edbob, get_engines):
 
    def test_get_sync_engines(self):
 
        config = Mock()
 
        config.get.return_value = None
 
        self.assertIsNone(sync.get_sync_engines(config))
 

	
 
        # nothing configured
 
        edbob.config.get.return_value = None
 
        self.assertTrue(sync.get_sync_engines() is None)
 

	
 
        # fake config with 2 out of 3 engines synced
 
        with patch('rattail.db.sync.get_engines') as get_engines:
 
            get_engines.return_value = {
 
                'one': 'first',
 
                'two': 'second',
 
                'three': 'third',
 
                }
 
        edbob.config.get.return_value = 'one, two'
 
        engines = sync.get_sync_engines()
 
        self.assertEqual(engines, {'one': 'first', 'two': 'second'})
 
            config.get.return_value = 'one, two'
 
            self.assertEqual(sync.get_sync_engines(config), {'one': 'first', 'two': 'second'})
 
            get_engines.assert_called_once_with(config)
 

	
 
    @patch.multiple('rattail.db.sync', edbob=DEFAULT, Synchronizer=DEFAULT)
 
    def test_synchronize_changes(self, edbob, Synchronizer):
 

	
 
        local_engine = Mock()
 
        remote_engines = Mock()
 

	
 
        # default synchronizer class
 
        edbob.config.get.return_value = None
 
        sync.synchronize_changes(local_engine, remote_engines)
 
        Synchronizer.assert_called_once_with(local_engine, remote_engines)
 
        Synchronizer.return_value.loop.assert_called_once_with()
 

	
 
        # custom synchronizer class
 
        edbob.config.get.return_value = 'some_class'
 
        sync.synchronize_changes(local_engine, remote_engines)
 
        edbob.load_spec.return_value.assert_called_once_with(local_engine, remote_engines)
 
        edbob.load_spec.return_value.return_value.loop.assert_called_once_with()
tests/db/sync/test_linux.py
Show inline comments
 

	
 
from unittest import TestCase
 
from mock import patch, DEFAULT
 

	
 
from rattail.db.sync import linux
 

	
 

	
 
class SyncDaemonTests(TestCase):
 

	
 
    @patch.multiple('rattail.db.sync.linux',
 
                    edbob=DEFAULT,
 
                    get_default_engine=DEFAULT,
 
                    get_sync_engines=DEFAULT,
 
                    synchronize_changes=DEFAULT)
 
    def test_run(self, edbob, get_default_engine, get_sync_engines, synchronize_changes):
 

	
 
        daemon = linux.SyncDaemon('/tmp/rattail_dbsync.pid')
 

	
 
        # no remote engines configured
 
        get_sync_engines.return_value = None
 
        daemon.run()
 
        get_sync_engines.assert_called_once_with()
 
        get_sync_engines.assert_called_once_with(edbob.config)
 
        self.assertFalse(get_default_engine.called)
 
        self.assertFalse(synchronize_changes.called)
 

	
 
        # with remote engines configured
 
        get_sync_engines.return_value = 'fake_remotes'
 
        get_default_engine.return_value = 'fake_local'
 
        daemon.run()
 
        synchronize_changes.assert_called_once_with('fake_local', 'fake_remotes')
 

	
 

	
 
class ModuleTests(TestCase):
 

	
 
    @patch.multiple('rattail.db.sync.linux', edbob=DEFAULT, SyncDaemon=DEFAULT)
 
    def test_get_daemon(self, edbob, SyncDaemon):
 

	
 
        # pid file provided
 
        linux.get_daemon('some_pidfile')
 
        self.assertFalse(edbob.config.get.called)
 
        SyncDaemon.assert_called_once_with('some_pidfile')
 

	
 
        # no pid file; fall back to config
 
        SyncDaemon.reset_mock()
 
        edbob.config.get.return_value = 'configured_pidfile'
 
        linux.get_daemon()
0 comments (0 inline, 0 general)