From 6c56f9be31d2eef4f74e4addd413a4aaf939ae99 Mon Sep 17 00:00:00 2001 From: Adam Shimali Date: Mon, 22 Feb 2016 14:52:16 +0000 Subject: [PATCH] Add count of rows in csv file as a notification count. --- app/main/views/sms.py | 5 ++-- app/notify_client/job_api_client.py | 5 ++-- tests/__init__.py | 3 ++- .../app/main/notify_client/test_job_client.py | 6 +++-- tests/app/main/views/test_sms.py | 27 ++++++++++++------- tests/conftest.py | 3 ++- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/app/main/views/sms.py b/app/main/views/sms.py index a135643f7..a4ae64aa3 100644 --- a/app/main/views/sms.py +++ b/app/main/views/sms.py @@ -127,7 +127,7 @@ def check_sms(service_id, upload_id): template_id = upload_data.get('template_id') raw_template = templates_dao.get_service_template_or_404(service_id, template_id)['data'] upload_result = _get_rows(contents, raw_template) - print(upload_result) + session['upload_data']['notification_count'] = len(upload_result['rows']) template = Template( raw_template, values=upload_result['rows'][0] if upload_result['valid'] else {}, @@ -148,9 +148,10 @@ def check_sms(service_id, upload_id): upload_data = session['upload_data'] original_file_name = upload_data.get('original_file_name') template_id = upload_data.get('template_id') + notification_count = upload_data.get('notification_count') session.pop('upload_data') try: - job_api_client.create_job(upload_id, service_id, template_id, original_file_name) + job_api_client.create_job(upload_id, service_id, template_id, original_file_name, notification_count) except HTTPError as e: if e.status_code == 404: abort(404) diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index 7b7be15c9..e39b19a23 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -20,14 +20,15 @@ class JobApiClient(BaseAPIClient): else: return self.get(url='/service/{}/job'.format(service_id)) - def create_job(self, job_id, service_id, template_id, original_file_name): + def create_job(self, job_id, service_id, template_id, original_file_name, notification_count): data = { "id": job_id, "service": service_id, "template": template_id, "original_file_name": original_file_name, "bucket_name": "service-{}-notify".format(service_id), - "file_name": "{}.csv".format(job_id) + "file_name": "{}.csv".format(job_id), + "notification_count": notification_count } resp = self.post(url='/service/{}/job'.format(service_id), data=data) diff --git a/tests/__init__.py b/tests/__init__.py index 8842d5537..850f1b53a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -95,6 +95,7 @@ def job_json(): 'original_file_name': 'thisisatest.csv', 'bucket_name': 'service-1-{}-notify'.format(job_id), 'file_name': '{}.csv'.format(job_id), - 'created_at': created_at + 'created_at': created_at, + 'notification_count': 1 } return data diff --git a/tests/app/main/notify_client/test_job_client.py b/tests/app/main/notify_client/test_job_client.py index 1333c530d..e59b2927a 100644 --- a/tests/app/main/notify_client/test_job_client.py +++ b/tests/app/main/notify_client/test_job_client.py @@ -7,6 +7,7 @@ def test_client_creates_job_data_correctly(mocker): service_id = str(uuid.uuid4()) template_id = 1 original_file_name = 'test.csv' + notification_count = 1 expected_data = { "id": job_id, @@ -14,7 +15,8 @@ def test_client_creates_job_data_correctly(mocker): "template": template_id, "original_file_name": original_file_name, "bucket_name": "service-{}-notify".format(service_id), - "file_name": "{}.csv".format(job_id) + "file_name": "{}.csv".format(job_id), + "notification_count": 1 } expected_url = '/service/{}/job'.format(service_id) @@ -22,6 +24,6 @@ def test_client_creates_job_data_correctly(mocker): client = JobApiClient() mock_post = mocker.patch('app.notify_client.job_api_client.JobApiClient.post') - client.create_job(job_id, service_id, template_id, original_file_name) + client.create_job(job_id, service_id, template_id, original_file_name, notification_count) mock_post.assert_called_once_with(url=expected_url, data=expected_data) diff --git a/tests/app/main/views/test_sms.py b/tests/app/main/views/test_sms.py index 470847848..573e41d14 100644 --- a/tests/app/main/views/test_sms.py +++ b/tests/app/main/views/test_sms.py @@ -136,16 +136,20 @@ def test_upload_csvfile_with_valid_phone_shows_all_numbers(app_, response = client.post(url_for('main.send_sms', service_id=12345, template_id=54321), data=upload_data, follow_redirects=True) + with client.session_transaction() as sess: + assert int(sess['upload_data']['template_id']) == 54321 + assert sess['upload_data']['original_file_name'] == 'valid.csv' + assert sess['upload_data']['notification_count'] == 6 - content = response.get_data(as_text=True) + content = response.get_data(as_text=True) - assert response.status_code == 200 - assert '+44 7700 900981' in content - assert '+44 7700 900982' in content - assert '+44 7700 900983' in content - assert '+44 7700 900984' in content - assert '+44 7700 900985' in content - assert '+44 7700 900986' in content + assert response.status_code == 200 + assert '+44 7700 900981' in content + assert '+44 7700 900982' in content + assert '+44 7700 900983' in content + assert '+44 7700 900984' in content + assert '+44 7700 900985' in content + assert '+44 7700 900986' in content @moto.mock_s3 @@ -164,15 +168,18 @@ def test_create_job_should_call_api(app_, job_id = job_data['id'] original_file_name = job_data['original_file_name'] template_id = job_data['template'] + notification_count = job_data['notification_count'] with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) with client.session_transaction() as session: - session['upload_data'] = {'original_file_name': original_file_name, 'template_id': template_id} + session['upload_data'] = {'original_file_name': original_file_name, + 'template_id': template_id, + 'notification_count': notification_count} url = url_for('main.check_sms', service_id=service_one['id'], upload_id=job_id) response = client.post(url, data=job_data, follow_redirects=True) assert response.status_code == 200 assert 'We’ve started sending your messages' in response.get_data(as_text=True) - mock_create_job.assert_called_with(job_id, service_id, template_id, original_file_name) + mock_create_job.assert_called_with(job_id, service_id, template_id, original_file_name, notification_count) diff --git a/tests/conftest.py b/tests/conftest.py index d53b1e72e..308ee42b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -468,13 +468,14 @@ def job_data(): @pytest.fixture(scope='function') def mock_create_job(mocker, job_data): - def _create(job_id, service_id, template_id, file_name): + def _create(job_id, service_id, template_id, file_name, notification_count): job_data['id'] = job_id job_data['service'] = service_id job_data['template'] = template_id job_data['bucket_name'] = 'service-{}-notify'.format(job_id) job_data['original_file_name'] = file_name job_data['file_name'] = '{}.csv'.format(job_id) + job_data['notification_count'] = notification_count return job_data return mocker.patch('app.job_api_client.create_job', side_effect=_create)