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 23 insertions and 22 deletions:
0 comments (0 inline, 0 general)
rattail/db/sync/__init__.py
Show inline comments
 
@@ -37,35 +37,38 @@ 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.
rattail/db/sync/linux.py
Show inline comments
 
@@ -27,25 +27,25 @@
 
"""
 

	
 
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',
tests/db/sync/test_init.py
Show inline comments
 
@@ -324,40 +324,38 @@ class SynchronizerTests(SyncTestCase):
 
        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):
 

	
 
        # 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
 
        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'})
 
    def test_get_sync_engines(self):
 
        config = Mock()
 
        config.get.return_value = None
 
        self.assertIsNone(sync.get_sync_engines(config))
 

	
 
        with patch('rattail.db.sync.get_engines') as get_engines:
 
            get_engines.return_value = {
 
                'one': 'first',
 
                'two': 'second',
 
                'three': 'third',
 
                }
 
            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()
tests/db/sync/test_linux.py
Show inline comments
 
@@ -10,25 +10,25 @@ 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):
 

	
0 comments (0 inline, 0 general)