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
 
@@ -43,23 +43,26 @@ 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
rattail/db/sync/linux.py
Show inline comments
 
@@ -33,13 +33,13 @@ 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):
tests/db/sync/test_init.py
Show inline comments
 
@@ -330,28 +330,26 @@ class SynchronizerTests(SyncTestCase):
 
        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()
tests/db/sync/test_linux.py
Show inline comments
 
@@ -16,13 +16,13 @@ class SyncDaemonTests(TestCase):
 

	
 
        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'
0 comments (0 inline, 0 general)