Use a ModelList for lists of jobs

This follows the pattern of what we’ve done with services, users and
events.

It gives us a way of neatly instantiating a model for each item in the
list we get back from the API and reduces the complexity of the view
layer code.

Now is a good time to do this because we’re going to be making a bunch
of changes to the jobs pages, and those changes will be easier to code
and understand with a sensible model behind them.
This commit is contained in:
Chris Hill-Scott
2020-01-08 14:29:56 +00:00
parent 5e7ec3e30d
commit 25464a141b
13 changed files with 106 additions and 76 deletions

View File

@@ -1366,7 +1366,8 @@ def test_should_show_all_jobs_with_valid_statuses(
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_usage,
mock_get_inbound_sms_summary
mock_get_inbound_sms_summary,
mock_get_free_sms_fragment_limit,
):
logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))

View File

@@ -518,7 +518,7 @@ def test_should_cancel_job(
mock_get_service_template,
mocker,
):
mock_cancel = mocker.patch('app.main.jobs.job_api_client.cancel_job')
mock_cancel = mocker.patch('app.job_api_client.cancel_job')
client_request.post(
'main.cancel_job',
service_id=SERVICE_ONE_ID,
@@ -687,7 +687,7 @@ def test_dont_cancel_letter_job_when_to_early_to_cancel(
'app.notification_api_client.get_notification_count_for_job_id', return_value=number_of_processed_notifications
)
mock_cancel = mocker.patch('app.main.jobs.job_api_client.cancel_letter_job')
mock_cancel = mocker.patch('app.job_api_client.cancel_letter_job')
page = client_request.post(
'main.cancel_letter_job',
service_id=SERVICE_ONE_ID,

View File

@@ -1,7 +1,7 @@
import pytest
from app.models.job import Job
from app.statistics_utils import (
add_rate_to_job,
add_rates_to,
statistics_by_state,
sum_of_statistics,
@@ -128,7 +128,7 @@ def test_service_statistics_by_state():
(1, 4, 20)
])
def test_add_rate_to_job_calculates_rate(failed, delivered, expected_failure_rate):
resp = add_rate_to_job(
resp = Job(
{
'notifications_failed': failed,
'notifications_delivered': delivered,
@@ -136,11 +136,11 @@ def test_add_rate_to_job_calculates_rate(failed, delivered, expected_failure_rat
}
)
assert resp['failure_rate'] == expected_failure_rate
assert resp.failure_rate == expected_failure_rate
def test_add_rate_to_job_preserves_initial_fields():
resp = add_rate_to_job(
resp = Job(
{
'notifications_failed': 0,
'notifications_delivered': 0,
@@ -148,4 +148,5 @@ def test_add_rate_to_job_preserves_initial_fields():
}
)
assert set(resp.keys()) == {'notifications_failed', 'notifications_delivered', 'id', 'failure_rate'}
assert resp.notifications_failed == resp.notifications_delivered == resp.failure_rate == 0
assert resp.id == 'foo'

View File

@@ -1732,12 +1732,16 @@ def mock_get_uploads(mocker, api_user_active):
'notification_count': 10,
'created_at': '2016-01-01 11:09:00.061258',
'statistics': [{'count': 8, 'status': 'delivered'}, {'count': 2, 'status': 'temporary-failure'}],
'scheduled_for': None,
'job_status': 'finished',
'upload_type': 'job'},
{'id': 'job_id_1',
'original_file_name': 'some.csv',
'notification_count': 1,
'created_at': '2016-01-01 11:09:00.061258',
'statistics': [{'count': 1, 'status': 'delivered'}],
'scheduled_for': None,
'job_status': 'finished',
'upload_type': 'letter'}
]
return {
@@ -1747,8 +1751,8 @@ def mock_get_uploads(mocker, api_user_active):
'next': 'services/{}/uploads?page={}'.format(service_id, page + 1)
}
}
return mocker.patch('app.job_api_client.get_uploads', side_effect=_get_uploads)
# Why is mocking on the model needed?
return mocker.patch('app.models.job.PaginatedUploads.client', side_effect=_get_uploads)
@pytest.fixture(scope='function')