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 22 insertions and 12 deletions:
0 comments (0 inline, 0 general)
rattail/db/changes.py
Show inline comments
 
@@ -62,7 +62,7 @@ def record_changes(session, recorder=None, config=None):
 
    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)
 
@@ -110,6 +110,7 @@ class ChangeRecorder(object):
 

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

	
 
    def __call__(self, session, flush_context, instances):
rattail/tests/db/test_changes.py
Show inline comments
 
@@ -16,6 +16,9 @@ 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'):
 
@@ -45,7 +48,7 @@ class TestRecordChangesFunc(TestCase):
 
        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)
 
@@ -53,7 +56,7 @@ class TestRecordChangesFunc(TestCase):
 

	
 
        # 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()
 

	
 
@@ -73,8 +76,11 @@ class TestRecordChangesFunc(TestCase):
 

	
 
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()))
 
@@ -83,7 +89,7 @@ class TestChangeRecorder(DataTestCase):
 

	
 
    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()
 
@@ -93,7 +99,7 @@ class TestChangeRecorder(DataTestCase):
 

	
 
    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()
 
@@ -103,7 +109,7 @@ class TestChangeRecorder(DataTestCase):
 

	
 
    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()
 
@@ -113,7 +119,7 @@ class TestChangeRecorder(DataTestCase):
 

	
 
    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):
 
@@ -155,7 +161,7 @@ class TestChangeRecorder(DataTestCase):
 

	
 
    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)
 
@@ -172,7 +178,7 @@ class TestChangeRecorder(DataTestCase):
 

	
 
    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()))
 
@@ -226,8 +232,11 @@ class TestChangeRecorder(DataTestCase):
 

	
 
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()
 
@@ -253,7 +262,7 @@ class TestChangeRecorderLegacy(TestCase):
 

	
 
    @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
 

	
0 comments (0 inline, 0 general)