set updated_at manually to avoid ORM overwriting my changes

when ORM level changes are made (eg `my_model.my_column = my_value`),
the ORM will read the column definition to see if it should apply any
defaults.The updated_at columns that we use all define
`onupdate=datetime.datetime.utcnow`. We can't patch this out as the
function pointer to the original function has already been grabbed by
this at import time - so freezegun or `mocker.patch` won't work.

So we have to use the query syntax to set the `updated_at` timestamp in
the DB without going through the ORM layer.
This commit is contained in:
Leo Hemsted
2019-11-14 12:58:58 +00:00
parent cfe82f8f4a
commit 2d7bf664f5

View File

@@ -149,6 +149,8 @@ def test_reduce_sms_provider_priority_switches_provider(
mmg.priority = starting_priorities['mmg'] mmg.priority = starting_priorities['mmg']
firetext.priority = starting_priorities['firetext'] firetext.priority = starting_priorities['firetext']
# need to update these manually to avoid triggering the `onupdate` clause of the updated_at column
ProviderDetails.query.filter(ProviderDetails.notification_type == 'sms').update({'updated_at': datetime.min})
# switch away from mmg. currently both 50/50 # switch away from mmg. currently both 50/50
dao_reduce_sms_provider_priority('mmg') dao_reduce_sms_provider_priority('mmg')
@@ -166,6 +168,9 @@ def test_reduce_sms_provider_priority_adds_rows_to_history_table(
): ):
mocker.patch('app.dao.provider_details_dao.get_user_by_id', return_value=sample_user) mocker.patch('app.dao.provider_details_dao.get_user_by_id', return_value=sample_user)
mmg = get_provider_details_by_identifier('mmg') mmg = get_provider_details_by_identifier('mmg')
# need to update these manually to avoid triggering the `onupdate` clause of the updated_at column
ProviderDetails.query.filter(ProviderDetails.notification_type == 'sms').update({'updated_at': datetime.min})
provider_history_rows = ProviderDetailsHistory.query.filter( provider_history_rows = ProviderDetailsHistory.query.filter(
ProviderDetailsHistory.id == mmg.id ProviderDetailsHistory.id == mmg.id
).order_by( ).order_by(
@@ -185,14 +190,14 @@ def test_reduce_sms_provider_priority_adds_rows_to_history_table(
assert updated_provider_history_rows[0].priority == 90 assert updated_provider_history_rows[0].priority == 90
@freeze_time('2017-05-01 14:00:00')
def test_reduce_sms_provider_priority_does_nothing_if_providers_have_recently_changed( def test_reduce_sms_provider_priority_does_nothing_if_providers_have_recently_changed(
mocker, mocker,
restore_provider_details, restore_provider_details,
): ):
mock_is_slow = mocker.patch('app.celery.scheduled_tasks.is_delivery_slow_for_providers') mock_is_slow = mocker.patch('app.celery.scheduled_tasks.is_delivery_slow_for_providers')
mock_reduce = mocker.patch('app.celery.scheduled_tasks.dao_reduce_sms_provider_priority') mock_reduce = mocker.patch('app.celery.scheduled_tasks.dao_reduce_sms_provider_priority')
get_provider_details_by_identifier('mmg').updated_at = datetime(2017, 5, 1, 13, 51) ProviderDetails.query.filter(ProviderDetails.identifier == 'firetext').update({'updated_at': datetime.min})
ProviderDetails.query.filter(ProviderDetails.identifier == 'mmg').update({'updated_at': datetime.utcnow()})
dao_reduce_sms_provider_priority('firetext') dao_reduce_sms_provider_priority('firetext')