diff --git a/rattail/db/changes.py b/rattail/db/changes.py index 06feda87c574698b46cc795310971be20f62e5d1..ca4bb490f24caedf1e697a6197a072b504e64d93 100644 --- a/rattail/db/changes.py +++ b/rattail/db/changes.py @@ -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,7 +110,8 @@ class ChangeRecorder(object): 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): """ diff --git a/rattail/tests/db/test_changes.py b/rattail/tests/db/test_changes.py index da61b38e74748b433823b9d223375f9c065b8200..2fbfffafebca8f4b70bd7a742d76c05c7245c387 100644 --- a/rattail/tests/db/test_changes.py +++ b/rattail/tests/db/test_changes.py @@ -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