add new notification_status column to models.py

We now have a new column in the database, but it isn't being
populated. The first step is to make sure we update this column,
while still keeping the old enum based column up to date as well.

A couple of changes have had to happen to support this - one irritating
thing is that if we're ever querying columns individually, including
`Notification.status`, then we'll need to give that column a label,
since under the hood it translates to `Notification._status_enum`.
Accessing status through the ORM (i.e., my_noti.status = 'sending' or
similar) will work fine.
This commit is contained in:
Leo Hemsted
2017-05-04 17:09:04 +01:00
parent 34e6ab3211
commit 7e52fa4d13
6 changed files with 96 additions and 21 deletions

View File

@@ -311,6 +311,8 @@ def test_should_by_able_to_update_status_by_id(sample_template, sample_job, mmg_
data = _notification_json(sample_template, job_id=sample_job.id, status='sending')
notification = Notification(**data)
dao_create_notification(notification)
assert notification._status_enum == 'sending'
assert notification._status_fkey == 'sending'
assert Notification.query.get(notification.id).status == 'sending'
@@ -321,6 +323,8 @@ def test_should_by_able_to_update_status_by_id(sample_template, sample_job, mmg_
assert updated.updated_at == datetime(2000, 1, 2, 12, 0, 0)
assert Notification.query.get(notification.id).status == 'delivered'
assert notification.updated_at == datetime(2000, 1, 2, 12, 0, 0)
assert notification._status_enum == 'delivered'
assert notification._status_fkey == 'delivered'
def test_should_not_update_status_by_id_if_not_sending_and_does_not_update_job(notify_db, notify_db_session):
@@ -825,7 +829,7 @@ def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_ses
) == months
def test_update_notification(sample_notification, sample_template):
def test_update_notification(sample_notification):
assert sample_notification.status == 'created'
sample_notification.status = 'failed'
dao_update_notification(sample_notification)
@@ -833,6 +837,24 @@ def test_update_notification(sample_notification, sample_template):
assert notification_from_db.status == 'failed'
def test_update_notification_with_no_notification_status(sample_notification):
# specifically, it has an old enum status, but not a new status (because the upgrade script has just run)
sample_notification._status_fkey = None
sample_notification._enum_status = 'created'
dao_update_notification(sample_notification)
assert sample_notification.status == 'created'
assert sample_notification._enum_status == 'created'
assert sample_notification._status_fkey == None
sample_notification.status = 'failed'
dao_update_notification(sample_notification)
notification_from_db = Notification.query.get(sample_notification.id)
assert notification_from_db.status == 'failed'
assert notification_from_db._status_enum == 'failed'
assert notification_from_db._status_fkey == 'failed'
@freeze_time("2016-01-10 12:00:00.000000")
def test_should_delete_notifications_after_seven_days(notify_db, notify_db_session):
assert len(Notification.query.all()) == 0

View File

@@ -1240,17 +1240,16 @@ def test_get_monthly_notification_stats(mocker, client, sample_service, url, exp
assert json.loads(response.get_data(as_text=True)) == expected_json
def test_get_services_with_detailed_flag(notify_api, notify_db, notify_db_session):
def test_get_services_with_detailed_flag(client, notify_db, notify_db_session):
notifications = [
create_sample_notification(notify_db, notify_db_session),
create_sample_notification(notify_db, notify_db_session),
create_sample_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST)
]
with notify_api.test_request_context(), notify_api.test_client() as client:
resp = client.get(
'/service?detailed=True',
headers=[create_authorization_header()]
)
resp = client.get(
'/service?detailed=True',
headers=[create_authorization_header()]
)
assert resp.status_code == 200
data = json.loads(resp.get_data(as_text=True))['data']