def test_get_sample_data_collection_exercise_or_survey_not_found( self, mock_request): """If the survey or collection exercise are invalid, a 404 will be returned and the sample data section will return mostly 'N/A'. The dashboard url will still present with the info available.""" mock_request.get( url_dashboard, json={'message': 'Invalid collection exercise or survey ID'}, status_code=404) with open( f'{project_root}/test_data/collection_exercise/closest_past_collection_exercise.json' ) as json_data: collection_exercise = json.load(json_data) expected_url = 'http://localhost:8078/dashboard/collection-exercise/aec41b04-a177-4994-b385-a16136242d05' expected_output = { 'completed': 'N/A', 'dashboard_url': expected_url, 'in_progress': 'N/A', 'not_started': 'N/A', 'sample_size': 'N/A' } with self.app.app_context(): output = get_sample_data(collection_exercise, surveys_json) self.assertEqual(output, expected_output)
def test_get_sample_data_zero_sample_size(self, mock_request): """Tests getting sample data and the sample size is 0. This should display the hardcoded 0 for completed. We hardcode it because if we tried to divide by 0 then we'd get an exception.""" copied_dashboard_response = deepcopy(reporting_json) copied_dashboard_response['report']['sampleSize'] = 0 mock_request.get(url_dashboard, json=copied_dashboard_response, status_code=200) with open( f'{project_root}/test_data/collection_exercise/closest_past_collection_exercise.json' ) as json_data: collection_exercise = json.load(json_data) expected_url = 'http://localhost:8078/dashboard/collection-exercise/aec41b04-a177-4994-b385-a16136242d05' expected_output = { 'completed': '0 (0.0%)', 'dashboard_url': expected_url, 'in_progress': '600', 'not_started': '100', 'sample_size': '0' } with self.app.app_context(): output = get_sample_data(collection_exercise, surveys_json) self.assertEqual(output, expected_output)
def test_get_sample_data(self, mock_request): """Tests getting sample data when everything is working as expected""" mock_request.get(url_dashboard, json=reporting_json, status_code=200) with open( f'{project_root}/test_data/collection_exercise/closest_past_collection_exercise.json' ) as json_data: collection_exercise = json.load(json_data) expected_url = 'http://localhost:8078/dashboard/collection-exercise/aec41b04-a177-4994-b385-a16136242d05' expected_output = { 'completed': '700 (70.0%)', 'dashboard_url': expected_url, 'in_progress': '600', 'not_started': '100', 'sample_size': '1000' } with self.app.app_context(): output = get_sample_data(collection_exercise, surveys_json) self.assertEqual(output, expected_output)