Files @ 110c68468203
Branch filter:

Location: rattail-project/rattail/tests/db/test_changes.py

lance
Update changelog
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# -*- coding: utf-8 -*-

from __future__ import unicode_literals, absolute_import

from unittest import TestCase

from sqlalchemy import orm
from mock import patch, DEFAULT, Mock, MagicMock, call

from rattail import db
from rattail.db import changes, model
from .. 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)
        self.assertTrue(Session.rattail_record_changes)

    def test_session_instance(self):
        session = db.Session()
        self.assertFalse(session.rattail_record_changes)
        changes.record_changes(session)
        self.assertTrue(session.rattail_record_changes)
        session.close()

    def test_recorder(self):

        # no recorder
        session = db.Session()
        self.assertRaises(AttributeError, getattr, session, 'rattail_change_recorder')
        session.close()

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

        # specify recorder instance
        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, 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')
        session = db.Session()
        changes.record_changes(session, config=config)
        self.assertIs(type(session.rattail_change_recorder), changes.ChangeRecorder)
        session.close()

        # invalid recorder
        session = db.Session()
        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(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(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(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(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(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()
            obj = Model(uuid=uuid, person=person)
            recorder.process_deleted_object(self.session, obj)
            self.assertEqual(self.session.query(model.Change).count(), 2)

            change = self.session.query(model.Change).filter_by(class_name=Model.__name__).one()
            self.assertEqual(change.object_key, uuid)
            self.assertTrue(change.deleted)

            change = self.session.query(model.Change).filter_by(class_name='Person').one()
            self.assertEqual(change.object_key, '06100a34178e11e6a8633ca9f40bc550')
            self.assertFalse(change.deleted)

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

        employee = model.Employee(uuid='b302ac38178e11e6b8843ca9f40bc550')
        for Model in (model.EmployeeStore, model.EmployeeDepartment):

            self.assertEqual(self.session.query(model.Change).count(), 0)
            uuid = get_uuid()
            obj = Model(uuid=uuid, employee=employee)
            recorder.process_deleted_object(self.session, obj)
            self.assertEqual(self.session.query(model.Change).count(), 2)

            change = self.session.query(model.Change).filter_by(class_name=Model.__name__).one()
            self.assertEqual(change.object_key, uuid)
            self.assertTrue(change.deleted)

            change = self.session.query(model.Change).filter_by(class_name='Employee').one()
            self.assertEqual(change.object_key, 'b302ac38178e11e6b8843ca9f40bc550')
            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(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')
        self.assertFalse(change.deleted)

        recorder.record_change(self.session, class_name='Invalid', object_key='invalid', deleted=True)
        self.assertEqual(self.session.query(model.Change).count(), 2)
        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(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)

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

        # ignore instance with no UUID attribute
        self.assertFalse(recorder.record_rattail_change(self.session, model.Setting()))
        self.assertFalse(recorder.record_rattail_change(self.session, model.Permission()))
        self.assertEqual(self.session.query(model.Change).count(), 0)

        product = model.Product(uuid='c1b0fb94178f11e680fd3ca9f40bc550')

        # default
        self.assertTrue(recorder.record_rattail_change(self.session, product))
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.object_key, 'c1b0fb94178f11e680fd3ca9f40bc550')
        self.assertFalse(change.deleted)
        self.session.query(model.Change).delete(synchronize_session=False)

        # new
        self.assertTrue(recorder.record_rattail_change(self.session, product, type_='new'))
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.object_key, 'c1b0fb94178f11e680fd3ca9f40bc550')
        self.assertFalse(change.deleted)
        self.session.query(model.Change).delete(synchronize_session=False)

        # dirty
        self.assertTrue(recorder.record_rattail_change(self.session, product, type_='dirty'))
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.object_key, 'c1b0fb94178f11e680fd3ca9f40bc550')
        self.assertFalse(change.deleted)
        self.session.query(model.Change).delete(synchronize_session=False)

        # deleted
        self.assertTrue(recorder.record_rattail_change(self.session, product, type_='deleted'))
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.object_key, 'c1b0fb94178f11e680fd3ca9f40bc550')
        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(self.config)

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

    #     # don't record changes for changes
    #     self.assertFalse(recorder.record_change(session, model.Change()))

    #     # don't record changes for objects with no uuid attribute
    #     self.assertFalse(recorder.record_change(session, object()))

    #     # none of the above should have involved a call to `ensure_uuid()`
    #     self.assertFalse(recorder.ensure_uuid.called)

    #     # so far no *new* changes have been created
    #     self.assertFalse(session.add.called)

    #     # mock up session to force new change creation
    #     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(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)
        self.assertEqual(product.uuid, 'some_uuid')
        self.assertFalse(get_uuid.called)

        # no uuid yet, auto-generate
        uuid_column.foreign_keys = False
        get_uuid.return_value = 'another_uuid'
        product = model.Product()
        self.assertTrue(product.uuid is None)
        recorder.ensure_uuid(product)
        get_uuid.assert_called_once_with()
        self.assertEqual(product.uuid, 'another_uuid')

        # some heavy mocking for following tests
        uuid_column.foreign_keys = True
        remote_side = MagicMock(key='uuid')
        prop = MagicMock(__class__=orm.RelationshipProperty, key='foreign_thing')
        prop.remote_side.__len__.return_value = 1
        prop.remote_side.__iter__.return_value = [remote_side]
        object_mapper.return_value.iterate_properties.__iter__.return_value = [prop]
        
        # uuid fetched from existing foreign key object
        get_uuid.reset_mock()
        instance = Mock(uuid=None, foreign_thing=Mock(uuid='secondary_uuid'))
        recorder.ensure_uuid(instance)
        self.assertFalse(get_uuid.called)
        self.assertEqual(instance.uuid, 'secondary_uuid')

        # foreign key object doesn't exist; uuid generated as fallback
        get_uuid.return_value = 'fallback_uuid'
        instance = Mock(uuid=None, foreign_thing=None)
        recorder.ensure_uuid(instance)
        get_uuid.assert_called_once_with()
        self.assertEqual(instance.uuid, 'fallback_uuid')


class TestFunctionalChanges(DataTestCase):

    def setUp(self):
        super(TestFunctionalChanges, self).setUp()
        changes.record_changes(self.session)

    def test_add(self):
        product = model.Product()
        self.session.add(product)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 1)
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.instance_uuid, product.uuid)
        self.assertFalse(change.deleted)

    def test_change(self):
        product = model.Product()
        self.session.add(product)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 1)
        self.session.query(model.Change).delete()
        self.assertEqual(self.session.query(model.Change).count(), 0)

        product.description = 'Acme Bricks'
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 1)
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.instance_uuid, product.uuid)
        self.assertFalse(change.deleted)

    def test_delete(self):
        product = model.Product()
        self.session.add(product)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 1)
        self.session.query(model.Change).delete()
        self.assertEqual(self.session.query(model.Change).count(), 0)

        self.session.delete(product)

        self.assertEqual(self.session.query(model.Change).count(), 1)
        change = self.session.query(model.Change).one()
        self.assertEqual(change.class_name, 'Product')
        self.assertEqual(change.instance_uuid, product.uuid)
        self.assertTrue(change.deleted)

    def test_orphan_change(self):
        department = model.Department()
        subdepartment = model.Subdepartment()
        department.subdepartments.append(subdepartment)
        self.session.add(department)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 2)
        change = self.session.query(model.Change).filter_by(class_name='Department').one()
        self.assertFalse(change.deleted)
        change = self.session.query(model.Change).filter_by(class_name='Subdepartment').one()
        self.assertFalse(change.deleted)

        self.session.query(model.Change).delete()
        self.assertEqual(self.session.query(model.Change).count(), 0)

        # Creating an orphaned Subdepartment, which should be recorded as a
        # *change* due to the cascade rules in effect.
        department.subdepartments.remove(subdepartment)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 2)
        change = self.session.query(model.Change).filter_by(class_name='Department').one()
        self.assertFalse(change.deleted)
        change = self.session.query(model.Change).filter_by(class_name='Subdepartment').one()
        self.assertFalse(change.deleted)
        self.assertEqual(self.session.query(model.Subdepartment).count(), 1)
    
    def test_orphan_delete(self):
        customer = model.Customer()
        group = model.CustomerGroup()
        customer.groups.append(group)
        self.session.add(customer)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 3)
        change = self.session.query(model.Change).filter_by(class_name='Customer').one()
        self.assertFalse(change.deleted)
        change = self.session.query(model.Change).filter_by(class_name='CustomerGroup').one()
        self.assertFalse(change.deleted)
        change = self.session.query(model.Change).filter_by(class_name='CustomerGroupAssignment').one()
        self.assertFalse(change.deleted)

        self.session.query(model.Change).delete()
        self.assertEqual(self.session.query(model.Change).count(), 0)

        # Creating an orphaned CustomerGroupAssociation, which should be
        # recorded as a *deletion* due to the cascade rules in effect.  Note
        # that the CustomerGroup is not technically an orphan and in fact is
        # not even changed.
        customer.groups.remove(group)
        self.session.commit()

        self.assertEqual(self.session.query(model.Change).count(), 2)
        change = self.session.query(model.Change).filter_by(class_name='Customer').one()
        self.assertFalse(change.deleted)
        change = self.session.query(model.Change).filter_by(class_name='CustomerGroupAssignment').one()
        self.assertTrue(change.deleted)
        self.assertEqual(self.session.query(model.CustomerGroupAssignment).count(), 0)