def test_convert_event_list_to_dictionary_malformed_events(self): """Tests that a KeyError is raised if the events are malformed (missing either tag or timestamp)""" function_input = [ { "id": "cd3fdc36-f060-41e4-bc75-624f6f31e111", "collectionExerciseId": "6327160f-d7a8-4fcc-8551-a69c50b33e5f", "tag": "mps", }, { "id": "d950c192-e58d-4dcb-9a38-76658dbea6dc", "collectionExerciseId": "6327160f-d7a8-4fcc-8551-a69c50b33e5f", "tag": "go_live", "timestamp": "2020-03-01T06:00:00.000Z", }, ] with self.assertRaises(KeyError): convert_event_list_to_dictionary(function_input)
def format_data_for_template(collection_exercise, survey): """ Takes a collection exercise and a survey and formats them into an format the overview template can use to display the data. :param collection_exercise: A dictionary containing information a collection exercise :type collection_exercise: dict :param survey: A dictionary containing information on a survey :type survey: dict :return: A dictionary containing all the data needed for the overview template. :rtype: dict """ formatted_data = get_sample_data(collection_exercise, survey) next_key_date = get_nearest_future_key_date(collection_exercise.get("events")) if next_key_date: key_date_tag = get_display_text_for_event(next_key_date["tag"]) key_date_timestamp = format_datetime_to_string(next_key_date["timestamp"]) else: key_date_tag = "N/A" key_date_timestamp = "N/A" formatted_data["next_key_date_tag"] = key_date_tag formatted_data["next_key_date_timestamp"] = key_date_timestamp formatted_data["exerciseRef"] = collection_exercise.get("exerciseRef", "N/A") formatted_data["userDescription"] = collection_exercise.get("userDescription", "N/A") formatted_data["survey_id"] = survey["surveyRef"] formatted_data["shortName"] = survey["shortName"] formatted_data["longName"] = survey["longName"] formatted_data["state"] = collection_exercise.get("state", "N/A") # We can't be certain of what events are present so we need to add the ones that are present and put a sensible # blank value to the ones that are absent. 'ref_period_start', 'ref_period_end' and 'employment' are # returned to us as events, even though they're really metadata for the collection exercise... events = convert_event_list_to_dictionary(collection_exercise.get("events")) possible_events_list = [ "mps", "go_live", "return_by", "exercise_end", "ref_period_start", "ref_period_end", "employment", ] for event in possible_events_list: formatted_data[event] = format_datetime_to_string(events.get(event), "%A %d %b %Y %H:%M") logger.info( "Successfully formatted data for overview page", collection_exercise=collection_exercise["id"], survey=survey["id"], ) return formatted_data
def format_data_for_template(collection_exercise, survey): """ Takes a collection exercise and a survey and formats them into an format the overview template can use to display the data. :param collection_exercise: A dictionary containing information a collection exercise :type collection_exercise: dict :param survey: A dictionary containing information on a survey :type survey: dict :return: A dictionary containing all the data needed for the overview template. :rtype: dict """ formatted_data = get_sample_data(collection_exercise, survey) next_key_date = get_nearest_future_key_date( collection_exercise.get('events')) if next_key_date: key_date_tag = get_display_text_for_event(next_key_date['tag']) key_date_timestamp = format_datetime_to_string( next_key_date['timestamp']) else: key_date_tag = 'N/A' key_date_timestamp = 'N/A' formatted_data['next_key_date_tag'] = key_date_tag formatted_data['next_key_date_timestamp'] = key_date_timestamp formatted_data['exerciseRef'] = collection_exercise.get( 'exerciseRef', 'N/A') formatted_data['userDescription'] = collection_exercise.get( 'userDescription', 'N/A') formatted_data['survey_id'] = survey['surveyRef'] formatted_data['shortName'] = survey['shortName'] formatted_data['longName'] = survey['longName'] # We can't be certain of what events are present so we need to add the ones that are present and put a sensible # blank value to the ones that are absent. 'ref_period_start', 'ref_period_end' and 'employment' are # returned to us as events, even though they're really metadata for the collection exercise... events = convert_event_list_to_dictionary( collection_exercise.get('events')) possible_events_list = [ 'mps', 'go_live', 'return_by', 'exercise_end', 'ref_period_start', 'ref_period_end', 'employment' ] for event in possible_events_list: formatted_data[event] = format_datetime_to_string( events.get(event), '%A %d %b %Y %H:%M') logger.info("Successfully formatted data for overview page", collection_exercise=collection_exercise['id'], survey=survey['id']) return formatted_data
def test_convert_event_list_to_dictionary(self): with open(f"{project_root}/test_data/collection_exercise/" f"closest_future_collection_exercise.json") as json_data: collection_exercise = json.load(json_data) function_input = collection_exercise["events"] expected_output = { "mps": "2020-07-01T06:00:00.000Z", "go_live": "2020-07-02T06:00:00.000Z", "return_by": "2020-07-20T06:00:00.000Z", "reminder": "2020-07-21T06:00:00.000Z", "exercise_end": "2020-07-30T06:00:00.000Z", "ref_period_start": "2020-06-01T06:00:00.000Z", "ref_period_end": "2020-06-30T06:00:00.000Z", } output = convert_event_list_to_dictionary(function_input) self.assertEqual(output, expected_output)
def test_convert_event_list_to_dictionary_empty_list(self): function_input = [] expected_output = {} output = convert_event_list_to_dictionary(function_input) self.assertEqual(output, expected_output)