def test_previous_with_conditional_path(self): survey = load_schema_file("0_star_wars.json") expected_path = [ Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "choose-your-side-block"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "923ccc84-9d47-4a02-8ebc-1e9d14fcf10b"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "26f2c4b3-28ac-4072-9f18-a6a6c6f660db"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "cd3b74d1-b687-4051-9634-a8f9ce10a27d"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "an3b74d1-b687-4051-9634-a8f9ce10ard"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "846f8514-fed2-4bd7-8fb2-4b5fcb1622b1"), ] answer_1 = Answer(group_id="14ba4707-321d-441d-8d21-b8367366e766", block_id="choose-your-side-block", answer_id="ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c", value="Dark Side") answer_2 = Answer( group_id="14ba4707-321d-441d-8d21-b8367366e766", block_id="923ccc84-9d47-4a02-8ebc-1e9d14fcf10b", answer_id="pel989b8-5185-4ba6-b73f-c126e3a06ba7", value="Can I be a pain and have a goodies ship", ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) current_location = expected_path[3] expected_previous_location = expected_path[2] path_finder = PathFinder(survey, answer_store=answers) actual_previous_block = path_finder.get_previous_location( current_location=current_location) self.assertEqual(actual_previous_block, expected_previous_location) current_location = expected_path[2] expected_previous_location = expected_path[1] actual_previous_block = path_finder.get_previous_location( current_location=current_location) self.assertEqual(actual_previous_block, expected_previous_location)
def test_previous_with_conditional_path_alternative(self): survey = load_schema_file("0_star_wars.json") expected_path = [ Location("star-wars", 0, "choose-your-side-block"), Location("star-wars", 0, "light-side-pick-character-ship"), Location("star-wars", 0, "star-wars-trivia"), Location("star-wars", 0, "star-wars-trivia-part-2"), Location("star-wars", 0, "star-wars-trivia-part-3"), ] current_location = expected_path[2] expected_previous_location = expected_path[1] answer_1 = Answer( group_id="star-wars", block_id="choose-your-side-block", answer_id="choose-your-side-answer", value="Light Side" ) answer_2 = Answer( group_id="star-wars", block_id="light-side-pick-character-ship", answer_id="light-side-pick-ship-answer", value="No", ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual(path_finder.get_previous_location(current_location=current_location), expected_previous_location)
def test_previous_with_conditional_path_alternative(self): schema = load_schema_from_params('0', 'star_wars') schema.answer_is_in_repeating_group = MagicMock(return_value=False) expected_path = [ Location('star-wars', 0, 'choose-your-side-block'), Location('star-wars', 0, 'light-side-pick-character-ship'), Location('star-wars', 0, 'star-wars-trivia'), Location('star-wars', 0, 'star-wars-trivia-part-2'), Location('star-wars', 0, 'star-wars-trivia-part-3'), ] current_location = expected_path[2] expected_previous_location = expected_path[1] answer_1 = Answer( answer_id='choose-your-side-answer', value='Light Side' ) answer_2 = Answer( answer_id='light-side-pick-ship-answer', value='No', ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) path_finder = PathFinder(schema, answer_store=answers, metadata={}, completed_blocks=[]) self.assertEqual(path_finder.get_previous_location(current_location=current_location), expected_previous_location)
def test_repeating_groups_previous_location_second_instance(self): schema = load_schema_from_params('census', 'household') expected_path = [ Location('who-lives-here-relationship', 0, 'household-relationships'), Location('who-lives-here-relationship', 1, 'household-relationships'), ] path_finder = PathFinder(schema, AnswerStore(), metadata={}, completed_blocks=[]) self.assertEqual(path_finder.get_previous_location(current_location=expected_path[1]), expected_path[0])
def test_repeating_groups_previous_location_second_instance(self): survey = load_schema_file("census_household.json") expected_path = [ Location('who-lives-here-relationship', 0, 'household-relationships'), Location('who-lives-here-relationship', 1, 'household-relationships'), ] path_finder = PathFinder(survey) self.assertEqual(path_finder.get_previous_location(current_location=expected_path[1]), expected_path[0])
def test_get_previous_location_introduction(self): survey = load_schema_file("0_star_wars.json") path_finder = PathFinder(survey) first_location = Location("star-wars", 0, 'choose-your-side-block') previous_location = path_finder.get_previous_location(current_location=first_location) self.assertEqual('introduction', previous_location.block_id)
def test_previous_with_conditional_path(self): survey = load_schema_file("0_star_wars.json") expected_path = [ Location("star-wars", 0, "choose-your-side-block"), Location("star-wars", 0, "dark-side-pick-character-ship"), Location("star-wars", 0, "light-side-ship-type"), Location("star-wars", 0, "star-wars-trivia"), Location("star-wars", 0, "star-wars-trivia-part-2"), Location("star-wars", 0, "star-wars-trivia-part-3"), ] answer_1 = Answer( group_id="star-wars", block_id="choose-your-side-block", answer_id="choose-your-side-answer", value="Dark Side" ) answer_2 = Answer( group_id="star-wars", block_id="dark-side-pick-character-ship", answer_id="dark-side-pick-ship-answer", value="Can I be a pain and have a goodies ship", ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) current_location = expected_path[3] expected_previous_location = expected_path[2] path_finder = PathFinder(survey, answer_store=answers) actual_previous_block = path_finder.get_previous_location(current_location=current_location) self.assertEqual(actual_previous_block, expected_previous_location) current_location = expected_path[2] expected_previous_location = expected_path[1] actual_previous_block = path_finder.get_previous_location(current_location=current_location) self.assertEqual(actual_previous_block, expected_previous_location)
def test_get_previous_location_introduction(self): schema = load_schema_from_params('0', 'star_wars') schema.answer_is_in_repeating_group = MagicMock(return_value=False) path_finder = PathFinder(schema, AnswerStore(), metadata={}, completed_blocks=[]) first_location = Location('star-wars', 0, 'choose-your-side-block') previous_location = path_finder.get_previous_location(current_location=first_location) self.assertEqual('introduction', previous_location.block_id)
def test_previous_with_conditional_path(self): schema = load_schema_from_params('0', 'star_wars') schema.answer_is_in_repeating_group = MagicMock(return_value=False) expected_path = [ Location('star-wars', 0, 'choose-your-side-block'), Location('star-wars', 0, 'dark-side-pick-character-ship'), Location('star-wars', 0, 'light-side-ship-type'), Location('star-wars', 0, 'star-wars-trivia'), Location('star-wars', 0, 'star-wars-trivia-part-2'), Location('star-wars', 0, 'star-wars-trivia-part-3'), ] answer_1 = Answer( answer_id='choose-your-side-answer', value='Dark Side' ) answer_2 = Answer( answer_id='dark-side-pick-ship-answer', value='Can I be a pain and have a goodies ship', ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) current_location = expected_path[3] expected_previous_location = expected_path[2] path_finder = PathFinder(schema, answer_store=answers, metadata={}, completed_blocks=[]) actual_previous_block = path_finder.get_previous_location(current_location=current_location) self.assertEqual(actual_previous_block, expected_previous_location) current_location = expected_path[2] expected_previous_location = expected_path[1] actual_previous_block = path_finder.get_previous_location(current_location=current_location) self.assertEqual(actual_previous_block, expected_previous_location)
def test_repeating_groups_previous_location_introduction(self): survey = load_schema_file("test_repeating_household.json") expected_path = [ Location('multiple-questions-group', 0, 'introduction'), Location('multiple-questions-group', 0, 'household-composition'), ] path_finder = PathFinder(survey) self.assertEqual( path_finder.get_previous_location( current_location=expected_path[1]), expected_path[0])
def test_previous_block(self): survey = load_schema_file("1_0102.json") current_location = Location(block_id="internet-sales", group_id="rsi", group_instance=0) previous_location = Location(block_id="total-retail-turnover", group_id="rsi", group_instance=0) path_finder = PathFinder(survey) self.assertEqual( path_finder.get_previous_location( current_location=current_location), previous_location)
def _build_template(current_location, context, template, routing_path=None): metadata = get_metadata(current_user) metadata_context = build_metadata_context(metadata) answer_store = get_answer_store(current_user) front_end_navigation = _get_front_end_navigation(answer_store, current_location, metadata, routing_path) path_finder = PathFinder(g.schema_json, answer_store, metadata) previous_location = path_finder.get_previous_location(current_location) previous_url = previous_location.url( metadata) if previous_location is not None else None return _render_template(context, current_location, template, front_end_navigation, metadata_context, previous_url)
def test_previous_block(self): schema = load_schema_from_params('test', '0102') current_location = Location( block_id='internet-sales', group_id='rsi', group_instance=0 ) previous_location = Location( block_id='total-retail-turnover', group_id='rsi', group_instance=0 ) path_finder = PathFinder(schema, AnswerStore(), metadata={}, completed_blocks=[]) self.assertEqual(path_finder.get_previous_location(current_location=current_location), previous_location)
def _build_template(current_location, context=None, template=None): metadata = get_metadata(current_user) metadata_context = build_metadata_context(metadata) answer_store = get_answer_store(current_user) completed_blocks = get_completed_blocks(current_user) navigation = Navigation(g.schema_json, answer_store, metadata, completed_blocks) front_end_navigation = navigation.build_navigation(current_location.group_id, current_location.group_instance) path_finder = PathFinder(g.schema_json, answer_store, metadata) previous_location = path_finder.get_previous_location(current_location) previous_url = None is_first_block_for_group = SchemaHelper.is_first_block_id_for_group(g.schema_json, current_location.group_id, current_location.block_id) if previous_location is not None and not is_first_block_for_group and not current_location.block_id == 'thank-you': previous_url = previous_location.url(metadata) return _render_template(context, current_location.block_id, front_end_navigation, metadata_context, current_location, previous_url, template)
def test_repeating_groups_previous_location(self): survey = load_schema_file("test_repeating_household.json") expected_path = [ Location("multiple-questions-group", 0, "household-composition"), Location("repeating-group", 0, "repeating-block-1"), Location("repeating-group", 0, "repeating-block-2"), Location("repeating-group", 1, "repeating-block-1"), Location("repeating-group", 1, "repeating-block-2"), ] answer = Answer( group_id="multiple-questions-group", answer_instance=0, answer_id="first-name", block_id="household-composition", value="Joe Bloggs" ) answer_2 = Answer( group_id="multiple-questions-group", answer_instance=1, answer_id="first-name", block_id="household-composition", value="Sophie Bloggs" ) current_location = expected_path[4] expected_previous_location = expected_path[3] answers = AnswerStore() answers.add(answer) answers.add(answer_2) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual(expected_previous_location, path_finder.get_previous_location(current_location=current_location))
def test_previous_with_conditional_path_alternative(self): survey = load_schema_file("0_star_wars.json") expected_path = [ Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "choose-your-side-block"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "96682325-47ab-41e4-a56e-8315a19ffe2a"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "cd3b74d1-b687-4051-9634-a8f9ce10a27d"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "an3b74d1-b687-4051-9634-a8f9ce10ard"), Location("14ba4707-321d-441d-8d21-b8367366e766", 0, "846f8514-fed2-4bd7-8fb2-4b5fcb1622b1"), ] current_location = expected_path[2] expected_previous_location = expected_path[1] answer_1 = Answer(group_id="14ba4707-321d-441d-8d21-b8367366e766", block_id="choose-your-side-block", answer_id="ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c", value="Light Side") answer_2 = Answer( group_id="14ba4707-321d-441d-8d21-b8367366e766", block_id="96682325-47ab-41e4-a56e-8315a19ffe2a", answer_id="2e0989b8-5185-4ba6-b73f-c126e3a06ba7", value="No", ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual( path_finder.get_previous_location( current_location=current_location), expected_previous_location)
def test_repeating_groups_previous_location(self): schema = load_schema_from_params('test', 'repeating_household') expected_path = [ Location('multiple-questions-group', 0, 'household-composition'), Location('repeating-group', 0, 'repeating-block-1'), Location('repeating-group', 0, 'repeating-block-2'), Location('repeating-group', 1, 'repeating-block-1'), Location('repeating-group', 1, 'repeating-block-2'), ] answer = Answer( answer_instance=0, answer_id='first-name', value='Joe Bloggs' ) answer_2 = Answer( answer_instance=1, answer_id='first-name', value='Sophie Bloggs' ) current_location = expected_path[4] expected_previous_location = expected_path[3] answers = AnswerStore() answers.add(answer) answers.add(answer_2) path_finder = PathFinder(schema, answer_store=answers, metadata={}, completed_blocks=[]) self.assertEqual(expected_previous_location, path_finder.get_previous_location(current_location=current_location))