Changeset - 55ab6194bfc2
[Not reviewed]
0 2 0
Lance Edgar (lance) - 3 years ago 2021-10-03 16:11:56
lance@edbob.org
Tweak how we call the change recorder

to satisfy broken tests.. not sure if this is ideal quite yet but
seems to pass tests anyway
2 files changed with 23 insertions and 13 deletions:
0 comments (0 inline, 0 general)
rattail/db/changes.py
Show inline comments
 
@@ -59,13 +59,13 @@ def record_changes(session, recorder=None, config=None):
 
    :param session: A :class:`sqlalchemy:sqlalchemy.orm.session.Session` class,
 
       or instance thereof.
 
    """
 
    if isinstance(recorder, ChangeRecorder):
 
        pass
 
    elif callable(recorder):
 
        recorder = recorder()
 
        recorder = recorder(config)
 
    elif recorder is None:
 
        if config:
 
            spec = config.get('rattail.db', 'changes.recorder', usedb=False)
 
            if spec:
 
                recorder = load_object(spec)(config)
 
        if not recorder:
 
@@ -107,13 +107,14 @@ class ChangeRecorder(object):
 
    is_modified_kw = {}
 
    if parse_version(sa.__version__) < parse_version('0.8'):
 
        is_modified_kw['passive'] = True
 

	
 
    def __init__(self, config):
 
        self.config = config
 
        self.model = config.get_model()
 
        if self.config:
 
            self.model = config.get_model()
 

	
 
    def __call__(self, session, flush_context, instances):
 
        """
 
        Method invoked when session ``before_flush`` event occurs.
 
        """
 
        # TODO: what a mess, need to look into this again at some point...
rattail/tests/db/test_changes.py
Show inline comments
 
@@ -13,12 +13,15 @@ from rattail.tests import DataTestCase
 
from rattail.config import RattailConfig
 
from rattail.core import get_uuid
 

	
 

	
 
class TestRecordChangesFunc(TestCase):
 

	
 
    def setUp(self):
 
        self.config = RattailConfig()
 

	
 
    def test_session_class(self):
 
        Session = orm.sessionmaker()
 
        if hasattr(Session, 'kw'):
 
            self.assertRaises(KeyError, Session.kw.__getitem__, 'rattail_record_changes')
 
        self.assertRaises(AttributeError, getattr, Session, 'rattail_record_changes')
 
        changes.record_changes(Session)
 
@@ -42,21 +45,21 @@ class TestRecordChangesFunc(TestCase):
 
        session = db.Session()
 
        changes.record_changes(session)
 
        self.assertIs(type(session.rattail_change_recorder), changes.ChangeRecorder)
 
        session.close()
 

	
 
        # specify recorder instance
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 
        session = db.Session()
 
        changes.record_changes(session, recorder=recorder)
 
        self.assertIs(session.rattail_change_recorder, recorder)
 
        session.close()
 

	
 
        # specify recorder factory
 
        session = db.Session()
 
        changes.record_changes(session, recorder=changes.ChangeRecorder)
 
        changes.record_changes(session, recorder=changes.ChangeRecorder, config=self.config)
 
        self.assertIs(type(session.rattail_change_recorder), changes.ChangeRecorder)
 
        session.close()
 

	
 
        # specify recorder spec via config
 
        config = RattailConfig()
 
        config.set('rattail.db', 'changes.recorder', 'rattail.db.changes:ChangeRecorder')
 
@@ -70,53 +73,56 @@ class TestRecordChangesFunc(TestCase):
 
        self.assertRaises(ValueError, changes.record_changes, session, recorder='bogus')
 
        session.close()
 

	
 

	
 
class TestChangeRecorder(DataTestCase):
 

	
 
    def extra_setup(self):
 
        self.config = RattailConfig()
 

	
 
    def test_ignore_object(self):
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 
        self.assertTrue(recorder.ignore_object(model.Setting()))
 
        self.assertTrue(recorder.ignore_object(model.Change()))
 
        self.assertTrue(recorder.ignore_object(model.DataSyncChange()))
 
        self.assertFalse(recorder.ignore_object(model.Product()))
 
        self.assertFalse(recorder.ignore_object(model.Customer()))
 

	
 
    def test_process_new_object(self):
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 
        product = model.Product(uuid='6de299ca178d11e6be2c3ca9f40bc550')
 
        recorder.process_new_object(self.session, product)
 
        change = self.session.query(model.Change).one()
 
        self.assertEqual(change.class_name, 'Product')
 
        self.assertEqual(change.object_key, '6de299ca178d11e6be2c3ca9f40bc550')
 
        self.assertFalse(change.deleted)
 

	
 
    def test_process_dirty_object(self):
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 
        product = model.Product(uuid='6de299ca178d11e6be2c3ca9f40bc550')
 
        recorder.process_dirty_object(self.session, product)
 
        change = self.session.query(model.Change).one()
 
        self.assertEqual(change.class_name, 'Product')
 
        self.assertEqual(change.object_key, '6de299ca178d11e6be2c3ca9f40bc550')
 
        self.assertFalse(change.deleted)
 

	
 
    def test_process_deleted_object(self):
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 
        product = model.Product(uuid='6de299ca178d11e6be2c3ca9f40bc550')
 
        recorder.process_deleted_object(self.session, product)
 
        change = self.session.query(model.Change).one()
 
        self.assertEqual(change.class_name, 'Product')
 
        self.assertEqual(change.object_key, '6de299ca178d11e6be2c3ca9f40bc550')
 
        self.assertTrue(change.deleted)
 

	
 
    def test_process_deleted_object_special(self):
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 

	
 
        person = model.Person(uuid='06100a34178e11e6a8633ca9f40bc550')
 
        for Model in (model.PersonEmailAddress, model.PersonPhoneNumber, model.PersonMailingAddress):
 

	
 
            self.assertEqual(self.session.query(model.Change).count(), 0)
 
            uuid = get_uuid()
 
@@ -152,13 +158,13 @@ class TestChangeRecorder(DataTestCase):
 
            self.assertFalse(change.deleted)
 

	
 
            self.session.query(model.Change).delete(synchronize_session=False)
 

	
 
    def test_record_change(self):
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 

	
 
        recorder.record_change(self.session, class_name='Bogus', object_key='bogus', deleted=False)
 
        self.assertEqual(self.session.query(model.Change).count(), 1)
 
        change = self.session.query(model.Change).one()
 
        self.assertEqual(change.class_name, 'Bogus')
 
        self.assertEqual(change.object_key, 'bogus')
 
@@ -169,13 +175,13 @@ class TestChangeRecorder(DataTestCase):
 
        change = self.session.query(model.Change).filter_by(class_name='Invalid').one()
 
        self.assertEqual(change.object_key, 'invalid')
 
        self.assertTrue(change.deleted)
 

	
 
    def test_record_rattail_change(self):
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 

	
 
        # ignore change object (TODO: redundant?)
 
        self.assertFalse(recorder.record_rattail_change(self.session, model.Change()))
 
        self.assertFalse(recorder.record_rattail_change(self.session, model.DataSyncChange()))
 
        self.assertEqual(self.session.query(model.Change).count(), 0)
 

	
 
@@ -223,14 +229,17 @@ class TestChangeRecorder(DataTestCase):
 
        self.assertTrue(change.deleted)
 
        self.session.query(model.Change).delete(synchronize_session=False)
 

	
 

	
 
class TestChangeRecorderLegacy(TestCase):
 

	
 
    def setUp(self):
 
        self.config = RattailConfig()
 

	
 
    def test_init(self):
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 

	
 
    # def test_record_change(self):
 
    #     session = Mock()
 
    #     recorder = changes.ChangeRecorder()
 
    #     recorder.ensure_uuid = Mock()
 

	
 
@@ -250,13 +259,13 @@ class TestChangeRecorderLegacy(TestCase):
 
    #     session.query.return_value = session
 
    #     session.get.return_value = None
 
    #     self.assertTrue(recorder.record_change(session, model.Product()))
 

	
 
    @patch.multiple('rattail.db.changes', get_uuid=DEFAULT, object_mapper=DEFAULT)
 
    def test_ensure_uuid(self, get_uuid, object_mapper):
 
        recorder = changes.ChangeRecorder()
 
        recorder = changes.ChangeRecorder(self.config)
 
        uuid_column = Mock()
 
        object_mapper.return_value.columns.__getitem__.return_value = uuid_column
 

	
 
        # uuid already present
 
        product = model.Product(uuid='some_uuid')
 
        recorder.ensure_uuid(product)
0 comments (0 inline, 0 general)