def test_navigation_skip_condition_show_group(self): survey = load_schema_file("test_navigation.json") metadata = { "eq_id": '1', "collection_exercise_sid": '999', "form_type": "some_form" } completed_blocks = [] answer_store = AnswerStore() answer_1 = Answer( value="Buildings", group_instance=0, block_id='insurance-type', group_id='property-details', answer_instance=0, answer_id='insurance-type-answer' ) answer_store.add(answer_1) navigation = Navigation(survey, answer_store, metadata, completed_blocks=completed_blocks) user_navigation = navigation.build_navigation('property-details', 0) link_names = [d['link_name'] for d in user_navigation] self.assertIn('House Details', link_names)
def test_build_navigation_submit_answers_link_not_visible_when_no_completed_blocks(self): survey = load_schema_file("census_household.json") metadata = { "eq_id": '1', "collection_exercise_sid": '999', "form_type": "some_form" } completed_blocks = [] routing_path = [] navigation = Navigation(survey, AnswerStore(), metadata, completed_blocks, routing_path) confirmation_link = { 'link_name': 'Submit answers', 'highlight': False, 'repeating': False, 'completed': False, 'link_url': Location('questionnaire-completed', 0, 'confirmation').url(metadata) } navigation_links = navigation.build_navigation('permanent-or-family-home', 0) self.assertNotIn(confirmation_link, navigation_links) self.assertEqual(len(navigation_links), 2)
def test_build_navigation_submit_answers_link_hidden_when_not_all_sections_completed(self): survey = load_schema_file("test_navigation_confirmation.json") metadata = { "eq_id": '1', "collection_exercise_sid": '999', "form_type": "some_form" } completed_blocks = [ Location('property-details', 0, 'insurance-type'), Location('property-details', 0, 'insurance-address'), Location('property-details', 0, 'property-interstitial'), Location('house-details', 0, 'house-type'), Location('multiple-questions-group', 0, 'household-composition'), ] navigation = Navigation(survey, AnswerStore(), metadata, completed_blocks) confirmation_link = { 'link_name': 'Submit answers', 'highlight': False, 'repeating': False, 'completed': False, 'link_url': Location('confirmation-group', 0, 'confirmation').url(metadata) } navigation_links = navigation.build_navigation('property-details', 0) self.assertNotIn(confirmation_link, navigation_links) self.assertEqual(len(navigation_links), 4)
def test_post_form_for_radio_other_selected(self): with self.test_request_context(): survey = load_schema_file("test_radio.json") block_json = SchemaHelper.get_block(survey, 'radio-mandatory') location = Location('radio', 0, 'radio-mandatory') error_messages = SchemaHelper.get_messages(survey) answer_store = AnswerStore([{ 'answer_id': 'radio-mandatory-answer', 'block_id': 'radio-mandatory', 'value': 'Other', 'answer_instance': 0, }, { 'answer_id': 'other-answer-mandatory', 'block_id': 'block-1', 'value': 'Other text field value', 'answer_instance': 0, }]) radio_answer = SchemaHelper.get_first_answer_for_block(block_json) text_answer = 'other-answer-mandatory' form, _ = post_form_for_location( block_json, location, answer_store, MultiDict({ '{answer_id}'.format(answer_id=radio_answer['id']): 'Other', '{answer_id}'.format(answer_id=text_answer): 'Other text field value', }), error_messages) other_text_field = getattr(form, 'other-answer-mandatory') self.assertEqual(other_text_field.data, 'Other text field value')
def test_should_not_show_block_for_zero_repeats(self): survey = load_schema_file("test_repeating_household.json") # Default is to count answers, so switch to using value survey['groups'][-2]['routing_rules'][0]['repeat']['type'] = 'answer_value' expected_path = [ Location("multiple-questions-group", 0, "introduction"), Location("multiple-questions-group", 0, "household-composition"), Location("summary-group", 0, "summary") ] answer = Answer( group_id="multiple-questions-group", group_instance=0, answer_id="first-name", block_id="household-composition", value="0" ) answers = AnswerStore() answers.add(answer) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual(expected_path, path_finder.get_routing_path())
def test_get_form_deserialises_month_year_dates(self): with self.test_request_context(): survey = load_schema_file("test_dates.json") block_json = SchemaHelper.get_block(survey, "date-block") location = SchemaHelper.get_first_location(survey) error_messages = SchemaHelper.get_messages(survey) form, _ = get_form_for_location( block_json, location, AnswerStore([{ 'answer_id': 'month-year-answer', 'group_id': 'dates', 'group_instance': 0, 'block_id': 'date-block', 'value': '05/2015', 'answer_instance': 0, }]), error_messages) self.assertTrue(hasattr(form, "month-year-answer")) month_year_field = getattr(form, "month-year-answer") self.assertEqual(month_year_field.data, { 'month': '5', 'year': '2015', })
def test_generate_relationship_form_errors_are_correctly_mapped(self): with self.test_request_context(): survey = load_schema_file("test_relationship_household.json") block_json = SchemaHelper.get_block(survey, 'relationships') error_messages = SchemaHelper.get_messages(survey) answer = SchemaHelper.get_first_answer_for_block(block_json) generated_form = generate_relationship_form( block_json, 3, None, error_messages=error_messages) form = generate_relationship_form( block_json, 3, { 'csrf_token': generated_form.csrf_token.current_token, '{answer_id}-0'.format(answer_id=answer['id']): '1', '{answer_id}-1'.format(answer_id=answer['id']): '3', }, error_messages=error_messages) form.validate() mapped_errors = form.map_errors() message = "Not a valid choice" self.assertTrue( self._error_exists(answer['id'], message, mapped_errors))
def test_first_group_id(self): survey = load_schema_file("test_repeating_household.json") first_group_id = "multiple-questions-group" self.assertEqual(SchemaHelper.get_first_group_id(survey), first_group_id)
def test_generate_relationship_form_creates_form_from_data(self): with self.test_request_context(): survey = load_schema_file("test_relationship_household.json") block_json = SchemaHelper.get_block(survey, 'relationships') error_messages = SchemaHelper.get_messages(survey) answer = SchemaHelper.get_first_answer_for_block(block_json) form = generate_relationship_form( block_json, 3, { '{answer_id}-0'.format(answer_id=answer['id']): 'Husband or Wife', '{answer_id}-1'.format(answer_id=answer['id']): 'Brother or Sister', '{answer_id}-2'.format(answer_id=answer['id']): 'Relation - other', }, error_messages=error_messages) self.assertTrue(hasattr(form, answer['id'])) expected_form_data = [ 'Husband or Wife', 'Brother or Sister', 'Relation - other' ] self.assertEqual(form.data[answer['id']], expected_form_data)
def test_post_form_for_household_composition(self): with self.test_request_context(): survey = load_schema_file("census_household.json") block_json = SchemaHelper.get_block(survey, 'household-composition') location = Location('who-lives-here', 0, 'household-composition') error_messages = SchemaHelper.get_messages(survey) form, _ = post_form_for_location( block_json, location, AnswerStore(), { 'household-0-first-name': 'Joe', 'household-0-last-name': '', 'household-1-first-name': 'Bob', 'household-1-last-name': 'Seymour', }, error_messages) self.assertEqual(len(form.household.entries), 2) self.assertEqual(form.household.entries[0].data, { 'first-name': 'Joe', 'middle-names': '', 'last-name': '' }) self.assertEqual(form.household.entries[1].data, { 'first-name': 'Bob', 'middle-names': '', 'last-name': 'Seymour' })
def test_next_location_goto_summary(self): survey = load_schema_file("0_star_wars.json") expected_path = [ Location("star-wars", 0, 'introduction'), Location("star-wars", 0, 'choose-your-side-block'), Location("star-wars", 0, 'summary'), ] answer = Answer( group_id="star-wars", group_instance=0, block_id="choose-your-side-block", answer_id="choose-your-side-answer", value="I prefer Star Trek", ) answers = AnswerStore() answers.add(answer) path_finder = PathFinder(survey, answer_store=answers) current_location = expected_path[1] expected_next_location = expected_path[2] next_location = path_finder.get_next_location(current_location=current_location) self.assertEqual(next_location, expected_next_location)
def test_next_location_empty_routing_rules(self): survey = load_schema_file("test_checkbox.json") expected_path = [ Location("checkboxes", 0, 'introduction'), Location("checkboxes", 0, 'mandatory-checkbox'), Location("checkboxes", 0, 'non-mandatory-checkbox'), Location("checkboxes", 0, 'summary') ] answer_1 = Answer( group_id="checkboxes", block_id="mandatory-checkbox", answer_id="mandatory-checkbox-answer", value="Cheese", ) answer_2 = Answer( group_id="checkboxes", block_id="non-mandatory-checkbox", answer_id="non-mandatory-checkbox-answer", value="deep pan", ) answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) path_finder = PathFinder(survey, answer_store=answers) current_location = expected_path[1] expected_next_location = expected_path[2] next_location = path_finder.get_next_location(current_location=current_location) self.assertEqual(next_location, expected_next_location)
def test_first_block_id(self): survey = load_schema_file("test_repeating_household.json") first_block_id = "introduction" self.assertEqual(SchemaHelper.get_first_block_id(survey), first_block_id)
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_routing_basic_and_conditional_path(self): survey = load_schema_file("0_star_wars.json") expected_path = [ Location("star-wars", 0, "introduction"), 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"), Location("star-wars", 0, "summary"), ] 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) path_finder = PathFinder(survey, answer_store=answers) routing_path = path_finder.get_routing_path() self.assertEqual(routing_path, expected_path)
def setUp(self): super().setUp() survey = load_schema_file("census_household.json") self.block_json = SchemaHelper.get_block(survey, 'household-composition') self.error_messages = SchemaHelper.get_messages(survey)
def test_form_date_range_populates_data(self): with self.test_request_context(): survey = load_schema_file("1_0102.json") block_json = SchemaHelper.get_block(survey, "reporting-period") error_messages = SchemaHelper.get_messages(survey) data = { 'period-from-day': '01', 'period-from-month': '3', 'period-from-year': '2016', 'period-to-day': '31', 'period-to-month': '3', 'period-to-year': '2016' } expected_form_data = { 'csrf_token': '', 'period-from': { 'day': '01', 'month': '3', 'year': '2016' }, 'period-to': { 'day': '31', 'month': '3', 'year': '2016' } } form = generate_form(block_json, data, error_messages) self.assertEqual(form.data, expected_form_data)
def test_get_routing_path_when_first_block_in_group_skipped(self): # Given survey = load_schema_file('test_skip_condition_group.json') answer_store = AnswerStore() answer_store.add(Answer(group_id='do-you-want-to-skip-group', block_id='do-you-want-to-skip', answer_id='do-you-want-to-skip-answer', value='Yes')) # When path_finder = PathFinder(survey, answer_store=answer_store) # Then expected_route = [ { 'block_id': 'do-you-want-to-skip-block', 'group_id': 'do-you-want-to-skip-group', 'group_instance': 0 }, { 'block_id': 'summary', 'group_id': 'should-skip-group', 'group_instance': 0 } ] pytest.xfail(reason="Known bug when skipping last group due to summary bundled into it") self.assertEqual(path_finder.get_routing_path('should-skip-group'), expected_route)
def test_introduction_not_in_path_when_not_in_schema(self): survey = load_schema_file("census_individual.json") path_finder = PathFinder(survey) blocks = [b.block_id for b in path_finder.get_routing_path()] self.assertNotIn('introduction', blocks)
def test_get_answers_that_repeat_in_block(self): survey = load_schema_file("test_repeating_household.json") answers = [ answer for answer in SchemaHelper.get_answers_that_repeat_in_block( survey, 'household-composition') ] self.assertEqual(len(answers), 3)
def test_build_navigation_summary_link_not_visible_when_hidden_group_not_completed(self): survey = load_schema_file("test_navigation.json") # Payment details group not displayed in navigation survey['groups'][5]['hide_in_navigation'] = True metadata = { "eq_id": '1', "collection_exercise_sid": '999', "form_type": "some_form" } # Payment details thus not completed completed_blocks = [ Location('property-details', 0, 'insurance-type'), Location('property-details', 0, 'insurance-address'), Location('property-details', 0, 'property-interstitial'), Location('house-details', 0, 'house-type'), Location('multiple-questions-group', 0, 'household-composition'), Location('repeating-group', 0, 'repeating-block-1'), Location('repeating-group', 0, 'repeating-block-2'), Location('extra-cover', 0, 'extra-cover-block'), Location('extra-cover', 0, 'extra-cover-interstitial'), Location('extra-cover-items-group', 0, 'extra-cover-items'), ] routing_path = [ Location('property-details', 0, 'insurance-type'), Location('property-details', 0, 'insurance-address'), Location('property-details', 0, 'property-interstitial'), Location('house-details', 0, 'house-type'), Location('multiple-questions-group', 0, 'household-composition'), Location('repeating-group', 0, 'repeating-block-1'), Location('repeating-group', 0, 'repeating-block-2'), Location('payment-details', 0, 'credit-card'), Location('payment-details', 0, 'expiry-date'), Location('payment-details', 0, 'security-code'), Location('payment-details', 0, 'security-code-interstitial'), Location('extra-cover', 0, 'extra-cover-block'), Location('extra-cover', 0, 'extra-cover-interstitial'), Location('extra-cover-items-group', 0, 'extra-cover-items'), Location('confirmation-group', 0, 'confirmation'), ] navigation = Navigation(survey, AnswerStore(), metadata, completed_blocks, routing_path) confirmation_link = { 'link_name': 'Summary', 'highlight': False, 'repeating': False, 'completed': False, 'link_url': Location('summary-group', 0, 'summary').url(metadata) } navigation_links = navigation.build_navigation('property-details', 0) self.assertNotIn(confirmation_link, navigation_links) self.assertEqual(len(navigation_links), 3)
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_groups_that_repeat_with_answer_id(self): survey = load_schema_file("test_repeating_household.json") groups = [ group for group in SchemaHelper.get_groups_that_repeat_with_answer_id( survey, 'first-name') ] self.assertEqual(len(groups), 1)
def test_build_navigation_submit_answers_link_visible_when_all_sections_complete(self): survey = load_schema_file("test_navigation_confirmation.json") metadata = { "eq_id": '1', "collection_exercise_sid": '999', "form_type": "some_form" } completed_blocks = [ Location('property-details', 0, 'insurance-type'), Location('property-details', 0, 'insurance-address'), Location('property-details', 0, 'property-interstitial'), Location('house-details', 0, 'house-type'), Location('multiple-questions-group', 0, 'household-composition'), Location('repeating-group', 0, 'repeating-block-1'), Location('repeating-group', 0, 'repeating-block-2'), Location('extra-cover', 0, 'extra-cover-block'), Location('extra-cover', 0, 'extra-cover-interstitial'), Location('payment-details', 0, 'credit-card'), Location('payment-details', 0, 'expiry-date'), Location('payment-details', 0, 'security-code'), Location('payment-details', 0, 'security-code-interstitial'), Location('extra-cover-items-group', 0, 'extra-cover-items'), ] routing_path = [ Location('property-details', 0, 'insurance-type'), Location('property-details', 0, 'insurance-address'), Location('property-details', 0, 'property-interstitial'), Location('house-details', 0, 'house-type'), Location('multiple-questions-group', 0, 'household-composition'), Location('repeating-group', 0, 'repeating-block-1'), Location('repeating-group', 0, 'repeating-block-2'), Location('extra-cover', 0, 'extra-cover-block'), Location('extra-cover', 0, 'extra-cover-interstitial'), Location('payment-details', 0, 'credit-card'), Location('payment-details', 0, 'expiry-date'), Location('payment-details', 0, 'security-code'), Location('payment-details', 0, 'security-code-interstitial'), Location('extra-cover-items-group', 0, 'extra-cover-items'), Location('confirmation-group', 0, 'confirmation'), ] navigation = Navigation(survey, AnswerStore(), metadata, completed_blocks, routing_path) confirmation_link = { 'link_name': 'Submit answers', 'highlight': False, 'repeating': False, 'completed': False, 'link_url': Location('confirmation-group', 0, 'confirmation').url(metadata) } navigation_links = navigation.build_navigation('property-details', 0) self.assertIn(confirmation_link, navigation_links) self.assertEqual(len(navigation_links), 5)
def test_get_next_location_introduction(self): survey = load_schema_file("0_star_wars.json") path_finder = PathFinder(survey) introduction = Location('star-wars', 0, 'introduction') next_location = path_finder.get_next_location(current_location=introduction) self.assertEqual('choose-your-side-block', next_location.block_id)
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_get_repeating_rule(self): survey = load_schema_file("test_repeating_household.json") groups = [group for group in SchemaHelper.get_groups(survey)] rule = SchemaHelper.get_repeat_rule(groups[1]) self.assertEqual( { 'type': 'answer_count', 'answer_id': 'first-name', 'navigation_label_answer_ids': ['first-name', 'last-name'], }, rule)
def test_given_some_completed_blocks_when_get_latest_location_then_go_to_next_uncompleted_block(self): # Given no completed blocks survey = load_schema_file("test_repeating_household.json") path_finder = PathFinder(survey) completed_block = [Location('multiple-questions-group', 0, 'introduction')] # When go latest location latest_location = path_finder.get_latest_location(completed_block) # Then go to the first block self.assertEqual(Location('multiple-questions-group', 0, 'household-composition'), latest_location)
def test_form_ids_match_block_answer_ids(self): with self.test_request_context(): survey = load_schema_file("1_0102.json") block_json = SchemaHelper.get_block(survey, "reporting-period") error_messages = SchemaHelper.get_messages(survey) form = generate_form(block_json, {}, error_messages) for answer in SchemaHelper.get_answers_for_block(block_json): self.assertTrue(hasattr(form, answer['id']))
def test_option_has_other(self): with self.test_request_context(): survey = load_schema_file("test_checkbox.json") block_json = SchemaHelper.get_block(survey, "mandatory-checkbox") error_messages = SchemaHelper.get_messages(survey) form = generate_form(block_json, {}, error_messages) self.assertFalse( form.option_has_other("mandatory-checkbox-answer", 1)) self.assertTrue( form.option_has_other("mandatory-checkbox-answer", 6))