Merge pull request #3251 from alphagov/job-model

Make models for individual jobs and collections of jobs
This commit is contained in:
Chris Hill-Scott
2020-01-16 15:52:21 +00:00
committed by GitHub
20 changed files with 385 additions and 304 deletions

View File

@@ -1369,7 +1369,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

@@ -514,9 +514,11 @@ def test_should_show_scheduled_job(
def test_should_cancel_job(
client_request,
fake_uuid,
mock_get_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,
@@ -549,6 +551,7 @@ def test_should_not_show_cancelled_job(
def test_should_cancel_letter_job(
client_request,
mocker,
mock_get_service_letter_template,
active_user_with_permissions
):
job_id = str(uuid.uuid4())
@@ -564,7 +567,7 @@ def test_should_cancel_letter_job(
mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}])
mocker.patch('app.notification_api_client.get_notifications_for_service', return_value=notifications_json)
mocker.patch('app.notification_api_client.get_notification_count_for_job_id', return_value=5)
mock_cancel = mocker.patch('app.main.jobs.job_api_client.cancel_letter_job', return_value=5)
mock_cancel = mocker.patch('app.job_api_client.cancel_letter_job', return_value=5)
client_request.post(
'main.cancel_letter_job',
service_id=SERVICE_ONE_ID,
@@ -602,7 +605,7 @@ def test_should_not_show_cancel_link_for_letter_job_if_too_late(
mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}])
mocker.patch(
'app.notification_api_client.get_notifications_for_service',
side_effect=[notifications_json]
return_value=notifications_json
)
page = client_request.get(
@@ -639,7 +642,7 @@ def test_should_show_cancel_link_for_letter_job(
mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}])
mocker.patch(
'app.notification_api_client.get_notifications_for_service',
side_effect=[notifications_json]
return_value=notifications_json,
)
page = client_request.get(
@@ -684,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

@@ -3,6 +3,7 @@ from unittest.mock import ANY
import pytest
from app.models.job import Job, PaginatedJobs
from app.notify_client.job_api_client import JobApiClient
@@ -113,16 +114,15 @@ def test_client_parses_job_stats(mocker):
expected_url = '/service/{}/job/{}'.format(service_id, job_id)
client = JobApiClient()
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
result = client.get_job(service_id, job_id)
result = Job.from_id(job_id, service_id=service_id)
mock_get.assert_called_once_with(url=expected_url, params={})
assert result['data']['notifications_requested'] == 80
assert result['data']['notifications_sent'] == 50
assert result['data']['notification_count'] == 80
assert result['data']['notifications_failed'] == 40
assert result.notifications_requested == 80
assert result.notifications_sent == 50
assert result.notification_count == 80
assert result.notifications_failed == 40
def test_client_parses_empty_job_stats(mocker):
@@ -149,16 +149,15 @@ def test_client_parses_empty_job_stats(mocker):
expected_url = '/service/{}/job/{}'.format(service_id, job_id)
client = JobApiClient()
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
result = client.get_job(service_id, job_id)
result = Job.from_id(job_id, service_id=service_id)
mock_get.assert_called_once_with(url=expected_url, params={})
assert result['data']['notifications_requested'] == 0
assert result['data']['notifications_sent'] == 0
assert result['data']['notification_count'] == 80
assert result['data']['notifications_failed'] == 0
assert result.notifications_requested == 0
assert result.notifications_sent == 0
assert result.notification_count == 80
assert result.notifications_failed == 0
def test_client_parses_job_stats_for_service(mocker):
@@ -221,22 +220,21 @@ def test_client_parses_job_stats_for_service(mocker):
expected_url = '/service/{}/job'.format(service_id)
client = JobApiClient()
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
result = client.get_jobs(service_id)
result = PaginatedJobs(service_id)
mock_get.assert_called_once_with(url=expected_url, params={'page': 1})
assert result['data'][0]['id'] == job_1_id
assert result['data'][0]['notifications_requested'] == 80
assert result['data'][0]['notifications_sent'] == 50
assert result['data'][0]['notification_count'] == 80
assert result['data'][0]['notifications_failed'] == 40
assert result['data'][1]['id'] == job_2_id
assert result['data'][1]['notifications_requested'] == 40
assert result['data'][1]['notifications_sent'] == 25
assert result['data'][1]['notification_count'] == 40
assert result['data'][1]['notifications_failed'] == 20
mock_get.assert_called_once_with(url=expected_url, params={'page': 1, 'statuses': ANY})
assert result[0].id == job_1_id
assert result[0].notifications_requested == 80
assert result[0].notifications_sent == 50
assert result[0].notification_count == 80
assert result[0].notifications_failed == 40
assert result[1].id == job_2_id
assert result[1].notifications_requested == 40
assert result[1].notifications_sent == 25
assert result[1].notification_count == 40
assert result[1].notifications_failed == 20
def test_client_parses_empty_job_stats_for_service(mocker):
@@ -281,22 +279,21 @@ def test_client_parses_empty_job_stats_for_service(mocker):
expected_url = '/service/{}/job'.format(service_id)
client = JobApiClient()
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
result = client.get_jobs(service_id)
result = PaginatedJobs(service_id)
mock_get.assert_called_once_with(url=expected_url, params={'page': 1})
assert result['data'][0]['id'] == job_1_id
assert result['data'][0]['notifications_requested'] == 0
assert result['data'][0]['notifications_sent'] == 0
assert result['data'][0]['notification_count'] == 80
assert result['data'][0]['notifications_failed'] == 0
assert result['data'][1]['id'] == job_2_id
assert result['data'][1]['notifications_requested'] == 0
assert result['data'][1]['notifications_sent'] == 0
assert result['data'][1]['notification_count'] == 40
assert result['data'][1]['notifications_failed'] == 0
mock_get.assert_called_once_with(url=expected_url, params={'page': 1, 'statuses': ANY})
assert result[0].id == job_1_id
assert result[0].notifications_requested == 0
assert result[0].notifications_sent == 0
assert result[0].notification_count == 80
assert result[0].notifications_failed == 0
assert result[1].id == job_2_id
assert result[1].notifications_requested == 0
assert result[1].notifications_sent == 0
assert result[1].notification_count == 40
assert result[1].notifications_failed == 0
def test_cancel_job(mocker):

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,24 +128,25 @@ 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(
{
'notifications_failed': failed,
'notifications_delivered': delivered,
'id': 'foo'
}
)
resp = Job({
'statistics': [
{'status': 'failed', 'count': failed},
{'status': 'delivered', 'count': delivered},
],
'id': 'foo',
})
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(
{
'notifications_failed': 0,
'notifications_delivered': 0,
'id': 'foo'
}
)
resp = Job({
'statistics': [
{'status': 'failed', 'count': 0},
{'status': 'delivered', 'count': 0},
],
'id': 'foo',
})
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'