def test_next_with_conditional_path(self): survey = load_schema_file("0_star_wars.json") expected_path = [ "f22b1ba4-d15f-48b8-a1f3-db62b6f34cc0", "96682325-47ab-41e4-a56e-8315a19ffe2a", "cd3b74d1-b687-4051-9634-a8f9ce10a27d", "an3b74d1-b687-4051-9634-a8f9ce10ard", "846f8514-fed2-4bd7-8fb2-4b5fcb1622b1" ] answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "Light Side", "2e0989b8-5185-4ba6-b73f-c126e3a06ba7": "No" } current_block_id = expected_path[1] expected_next_block_id = expected_path[2] navigator = Navigator(survey) actual_next_block_id = navigator.get_next_location(answers, current_block_id) self.assertEqual(actual_next_block_id, expected_next_block_id) current_block_id = expected_path[2] expected_next_block_id = expected_path[3] actual_next_block_id = navigator.get_next_location(answers, current_block_id) self.assertEqual(actual_next_block_id, expected_next_block_id)
def test_previous_with_conditional_path(self): survey = load_schema_file("0_star_wars.json") expected_path = [ "f22b1ba4-d15f-48b8-a1f3-db62b6f34cc0", "923ccc84-9d47-4a02-8ebc-1e9d14fcf10b", "26f2c4b3-28ac-4072-9f18-a6a6c6f660db", "cd3b74d1-b687-4051-9634-a8f9ce10a27d", "an3b74d1-b687-4051-9634-a8f9ce10ard", "846f8514-fed2-4bd7-8fb2-4b5fcb1622b1" ] answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "Dark Side", "pel989b8-5185-4ba6-b73f-c126e3a06ba7": "Can I be a pain and have a goodies ship" } current_block_id = expected_path[3] expected_previous_block_id = expected_path[2] navigator = Navigator(survey) actual_previous_block_id = navigator.get_previous_location(answers, current_block_id) self.assertEqual(actual_previous_block_id, expected_previous_block_id) current_block_id = expected_path[2] expected_previous_block_id = expected_path[1] actual_previous_block_id = navigator.get_previous_location(answers, current_block_id) self.assertEqual(actual_previous_block_id, expected_previous_block_id)
def test_next_location_empty_routing_rules(self): survey = load_schema_file("test_checkbox.json") navigator = Navigator(survey) # Force some empty routing rules navigator.blocks[0]['routing_rules'] = [] expected_path = [ 'introduction', 'f22b1ba4-d15f-48b8-a1f3-db62b6f34cc0', 'f22b1ba4-d15f-48b8-a1f3-db62b6f34cc1', 'summary' ] answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "Cheese", "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23": "deep pan", } current_location_id = expected_path[1] expected_next_location_id = expected_path[2] next_location_id = navigator.get_next_location(answers, current_location_id) self.assertEqual(next_location_id, expected_next_location_id)
def test_first_block(self): survey = load_schema_file("1_0102.json") first_block_id = "5bce8d8f-0af8-4d35-b77d-744e6179b406" navigator = Navigator(survey) self.assertEqual(navigator.get_first_block_id(), first_block_id)
def test_get_previous_location(self): survey = load_schema_file("0_star_wars.json") navigator = Navigator(survey) next_location = navigator.get_previous_location(current_location_id='f22b1ba4-d15f-48b8-a1f3-db62b6f34cc0') self.assertEqual('introduction', next_location)
def test_next_block(self): survey = load_schema_file("1_0102.json") current_block_id = "7418732e-12fb-4270-8307-5682ac63bfae" next_block_id = "02ed26ad-4cfc-4e29-a946-630476228b2c" navigator = Navigator(survey) self.assertEqual(navigator.get_next_location(current_location_id=current_block_id), next_block_id)
def test_interstitial_post_blocks(self): survey = load_schema_file("0_star_wars.json") navigator = Navigator(survey) answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "Light Side" } self.assertFalse('summary' in navigator.get_location_path(answers))
def test_get_previous_location_introduction(self): survey = load_schema_file("0_star_wars.json") path_finder = PathFinder(survey) first_location = Location("14ba4707-321d-441d-8d21-b8367366e766", 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_next_location_introduction(self): survey = load_schema_file("0_star_wars.json") path_finder = PathFinder(survey) introduction = Location('14ba4707-321d-441d-8d21-b8367366e766', 0, 'introduction') next_location = path_finder.get_next_location( current_location=introduction) self.assertEqual('choose-your-side-block', next_location.block_id)
def test_update_questionnaire_store_with_answer_data(self): g.schema_json = load_schema_file("census_household.json") location = Location('who-lives-here', 0, 'household-composition') answers = [ Answer(group_id='who-lives-here', group_instance=0, block_id='household-composition', answer_id='first-name', answer_instance=0, value='Joe'), Answer(group_id='who-lives-here', group_instance=0, block_id='household-composition', answer_id='middle-names', answer_instance=0, value=''), Answer(group_id='who-lives-here', group_instance=0, block_id='household-composition', answer_id='last-name', answer_instance=0, value='Bloggs'), Answer(group_id='who-lives-here', group_instance=0, block_id='household-composition', answer_id='first-name', answer_instance=1, value='Bob'), Answer(group_id='who-lives-here', group_instance=0, block_id='household-composition', answer_id='middle-names', answer_instance=1, value=''), Answer(group_id='who-lives-here', group_instance=0, block_id='household-composition', answer_id='last-name', answer_instance=1, value='Seymour') ] update_questionnaire_store_with_answer_data(self.question_store, location, answers) self.assertEquals(self.question_store.completed_blocks, [location]) for answer in answers: self.assertIn(answer.__dict__, self.question_store.answer_store.answers)
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_generate_relationship_form_creates_empty_form(self): 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, {}, error_messages=error_messages) self.assertTrue(hasattr(form, answer['id'])) self.assertEqual(len(form.data[answer['id']]), 3)
def test_generate_month_year_date_form_creates_empty_form(self): survey = load_schema_file("test_dates.json") block_json = SchemaHelper.get_block(survey, 'date-block') error_messages = SchemaHelper.get_messages(survey) answers = SchemaHelper.get_answers_by_id_for_block(block_json) form = get_month_year_form(answers['month-year-answer'], error_messages=error_messages) self.assertFalse(hasattr(form, 'day')) self.assertTrue(hasattr(form, 'month')) self.assertTrue(hasattr(form, 'year'))
def test_get_next_location_confirmation(self): answer = Answer( answer_id="ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c", value="Orson Krennic", ) answer_store = AnswerStore() answer_store.add(answer) survey = load_schema_file("0_rogue_one.json") navigator = PathFinder(survey, answer_store) next_location = navigator.get_next_location( SchemaHelper.get_last_location(survey)) self.assertEqual('summary', next_location.block_id)
def test_answer_with_child_inherits_mandatory_from_parent(self): survey = load_schema_file("test_radio.json") block_json = SchemaHelper.get_block(survey, "block-1") error_messages = SchemaHelper.get_messages(survey) form = generate_form(block_json, {'ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c': 'Other'}, error_messages) child_field = getattr(form, 'other-answer-mandatory') self.assertIsInstance(child_field.validators[0], validators.InputRequired)
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_answer_errors_are_mapped(self): survey = load_schema_file("1_0112.json") block_json = SchemaHelper.get_block(survey, "total-retail-turnover") error_messages = SchemaHelper.get_messages(survey) form = generate_form(block_json, {'total-retail-turnover-answer': "-1"}, error_messages) form.validate() answer_errors = form.answer_errors('total-retail-turnover-answer') self.assertIn( "The value cannot be negative. Please correct your answer.", answer_errors)
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 test_routing_basic_path(self): survey = load_schema_file("1_0112.json") expected_path = [ "980b148e-0856-4e50-9afe-67a4fa6ae13b", "6c8a2f39-e0d8-406f-b463-2151225abea2", "0c7c8876-6a63-4251-ac29-b821b3e9b1bc", "a42b5752-1896-4f52-9d58-320085be92a7", "0b29d3f7-5905-43d8-9921-5b353db68104", "7e2d49eb-ffc7-4a61-a45d-eba336d1d0e6", ] navigator = Navigator(survey) routing_path = navigator.get_routing_path() self.assertEqual(routing_path, expected_path)
def test_get_form_for_household_relationship(self): survey = load_schema_file("census_household.json") block_json = SchemaHelper.get_block(survey, 'household-relationships') location = Location('who-lives-here-relationship', 0, 'household-relationships') error_messages = SchemaHelper.get_messages(survey) answer_store = AnswerStore([{ 'group_id': 'who-lives-here-relationship', 'group_instance': 0, 'answer_id': 'first-name', 'block_id': 'household-composition', 'value': 'Joe', 'answer_instance': 0, }, { 'group_id': 'who-lives-here-relationship', 'group_instance': 0, 'answer_id': 'last-name', 'block_id': 'household-composition', 'value': 'Bloggs', 'answer_instance': 0, }, { 'group_id': 'who-lives-here-relationship', 'group_instance': 1, 'answer_id': 'first-name', 'block_id': 'household-composition', 'value': 'Jane', 'answer_instance': 1, }, { 'group_id': 'who-lives-here-relationship', 'group_instance': 1, 'answer_id': 'last-name', 'block_id': 'household-composition', 'value': 'Bloggs', 'answer_instance': 1, }]) form, _ = get_form_for_location(block_json, location, answer_store, error_messages) answer = SchemaHelper.get_first_answer_for_block(block_json) self.assertTrue(hasattr(form, answer['id'])) field_list = getattr(form, answer['id']) # With two people, we need to define 1 relationship self.assertEqual(len(field_list.entries), 1)
def test_get_next_location_should_not_skip_when_no_answers(self): # Given survey = load_schema_file('test_skip_condition_group.json') current_location = Location('do-you-want-to-skip-group', 0, 'do-you-want-to-skip') answer_store = AnswerStore() # When path_finder = PathFinder(survey, answer_store=answer_store) # Then expected_location = Location('should-skip-group', 0, 'should-skip') self.assertEqual( path_finder.get_next_location(current_location=current_location), expected_location)
def test_interstitial_post_blocks(self): survey = load_schema_file("0_star_wars.json") answer = Answer(group_id="14ba4707-321d-441d-8d21-b8367366e766", block_id="choose-your-side-block", answer_id="ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c", value="Light Side") answers = AnswerStore() answers.add(answer) path_finder = PathFinder(survey, answer_store=answers) self.assertFalse( Location("14ba4707-321d-441d-8d21-b8367366e766", 0, 'summary') in path_finder.get_location_path())
def test_routing_basic_path(self): survey = load_schema_file("1_0112.json") expected_path = [ Location("rsi", 0, "reporting-period"), Location("rsi", 0, "total-retail-turnover"), Location("rsi", 0, "internet-sales"), Location("rsi", 0, "changes-in-retail-turnover"), Location("rsi", 0, "number-of-employees"), Location("rsi", 0, "changes-in-employees") ] path_finder = PathFinder(survey) routing_path = path_finder.get_routing_path() self.assertEqual(routing_path, expected_path)
def test_form_errors_are_correctly_mapped(self): survey = load_schema_file("1_0112.json") block_json = SchemaHelper.get_block(survey, "total-retail-turnover") error_messages = SchemaHelper.get_messages(survey) form = generate_form(block_json, {}, error_messages) form.validate() mapped_errors = form.map_errors() message = "Please provide a value, even if your value is 0." self.assertTrue( self._error_exists('total-retail-turnover-answer', message, mapped_errors))
def test_routing_backwards_continues_to_summary_when_complete(self): survey = load_schema_file("test_household_question.json") expected_path = [ Location('multiple-questions-group', 0, 'introduction'), Location('multiple-questions-group', 0, 'household-composition'), Location('multiple-questions-group', 0, 'household-summary'), Location('multiple-questions-group', 0, 'summary'), ] current_location = expected_path[2] expected_next_location = expected_path[3] answers = AnswerStore() answer_1 = Answer(group_id="multiple-questions-group", group_instance=0, block_id="household-composition", answer_id="household-full-name", answer_instance=0, value="Joe Bloggs") answer_2 = Answer(group_id="multiple-questions-group", group_instance=0, block_id="household-composition", answer_id="household-full-name", answer_instance=1, value="Sophie Bloggs") answer_3 = Answer(group_id="multiple-questions-group", group_instance=0, block_id="household-summary", answer_id="household-composition-add-another", answer_instance=0, value="Yes") answers.add(answer_1) answers.add(answer_2) answers.add(answer_3) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual( expected_next_location, path_finder.get_next_location(current_location=current_location))
def test_next_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, "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"), ] 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) current_location = expected_path[1] expected_next_location = expected_path[2] path_finder = PathFinder(survey, answer_store=answers) actual_next_block = path_finder.get_next_location( current_location=current_location) self.assertEqual(actual_next_block, expected_next_location) current_location = expected_path[2] expected_next_location = expected_path[3] actual_next_block = path_finder.get_next_location( current_location=current_location) self.assertEqual(actual_next_block, expected_next_location)
def test_form_subfield_errors_are_correctly_mapped(self): 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) message = "Please provide an answer to continue." form.validate() mapped_errors = form.map_errors() self.assertTrue(self._error_exists('period-to', message, mapped_errors)) self.assertTrue( self._error_exists('period-from', message, mapped_errors))
def test_post_form_for_household_relationship(self): survey = load_schema_file("census_household.json") block_json = SchemaHelper.get_block(survey, 'household-relationships') location = Location('who-lives-here-relationship', 0, 'household-relationships') error_messages = SchemaHelper.get_messages(survey) answer_store = AnswerStore([{ 'answer_id': 'first-name', 'block_id': 'household-composition', 'value': 'Joe', 'answer_instance': 0, }, { 'answer_id': 'last-name', 'block_id': 'household-composition', 'value': 'Bloggs', 'answer_instance': 0, }, { 'answer_id': 'first-name', 'block_id': 'household-composition', 'value': 'Jane', 'answer_instance': 1, }, { 'answer_id': 'last-name', 'block_id': 'household-composition', 'value': 'Bloggs', 'answer_instance': 1, }]) answer = SchemaHelper.get_first_answer_for_block(block_json) form, _ = post_form_for_location( block_json, location, answer_store, MultiDict({'{answer_id}-0'.format(answer_id=answer['id']): '3'}), error_messages) self.assertTrue(hasattr(form, answer['id'])) field_list = getattr(form, answer['id']) # With two people, we need to define 1 relationship self.assertEqual(len(field_list.entries), 1) # Check the data matches what was passed from request self.assertEqual(field_list.entries[0].data, "3")
def test_repeating_groups_no_of_answers(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"), Location("repeating-group", 2, "repeating-block-1"), Location("repeating-group", 2, "repeating-block-2"), ] answer = Answer(group_id="multiple-questions-group", group_instance=0, answer_instance=0, answer_id="first-name", block_id="household-composition", value="Joe Bloggs") answer_2 = Answer(group_id="multiple-questions-group", group_instance=0, answer_instance=1, answer_id="first-name", block_id="household-composition", value="Sophie Bloggs") answer_3 = Answer(group_id="multiple-questions-group", group_instance=0, answer_instance=2, answer_id="first-name", block_id="household-composition", value="Gregg Bloggs") answers = AnswerStore() answers.add(answer) answers.add(answer_2) answers.add(answer_3) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual(expected_path, path_finder.get_routing_path())
def test_update_questionnaire_store_with_date_form_data(self): g.schema_json = load_schema_file("test_dates.json") location = Location("a23d36db-6b07-4ce0-94b2-a843369511e3", 0, "date-block") form_data = { 'single-date-answer': { 'day': '12', 'month': '03', 'year': '2016' }, 'month-year-answer': { 'month': '11', 'year': '2014' }, } update_questionnaire_store_with_form_data(self.question_store, location, form_data) self.assertEquals(self.question_store.completed_blocks, [location]) self.assertIn( { 'group_id': 'a23d36db-6b07-4ce0-94b2-a843369511e3', 'group_instance': 0, 'block_id': 'date-block', 'answer_id': 'single-date-answer', 'answer_instance': 0, 'value': '12/03/2016', }, self.question_store.answer_store.answers) self.assertIn( { 'group_id': 'a23d36db-6b07-4ce0-94b2-a843369511e3', 'group_instance': 0, 'block_id': 'date-block', 'answer_id': 'month-year-answer', 'answer_instance': 0, 'value': '11/2014', }, self.question_store.answer_store.answers)
def test_get_form_for_household_composition(self): 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, _ = get_form_for_location(block_json, location, AnswerStore(), error_messages) self.assertTrue(hasattr(form, "household")) self.assertEquals(len(form.household.entries), 1) first_field_entry = form.household[0] self.assertTrue(hasattr(first_field_entry, "first-name")) self.assertTrue(hasattr(first_field_entry, "middle-names")) self.assertTrue(hasattr(first_field_entry, "last-name"))
def test_next_with_conditional_path_when_value_not_in_metadata(self): survey = load_schema_file("test_metadata_routing.json") expected_path = [ Location("group1", 0, "block1"), Location("group1", 0, "block2"), ] current_location = expected_path[0] expected_next_location = expected_path[1] metadata = {"variant_flags": {}} path_finder = PathFinder(survey, metadata=metadata) self.assertEqual( expected_next_location, path_finder.get_next_location(current_location=current_location))
def test_navigation_skip_condition_change_answer(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) change_answer = Answer(value="Contents", group_instance=0, block_id='insurance-type', group_id='property-details', answer_instance=0, answer_id='insurance-type-answer') answer_store.update(change_answer) user_navigation = navigation.build_navigation('property-details', 0) link_names = [d['link_name'] for d in user_navigation] self.assertNotIn('House Details', link_names)
def test_get_date_range_fields(self): survey = load_schema_file("test_dates.json") error_messages = SchemaHelper.get_messages(survey) questions = SchemaHelper.get_questions_by_id(survey) from_field, to_field = get_date_range_fields( questions["date-range-question"], { 'day': '1', 'month': '01', 'year': '2016' }, error_messages) self.assertTrue(hasattr(from_field.args[0], 'day')) self.assertTrue(hasattr(from_field.args[0], 'month')) self.assertTrue(hasattr(from_field.args[0], 'year')) self.assertTrue(hasattr(to_field.args[0], 'day')) self.assertTrue(hasattr(to_field.args[0], 'month')) self.assertTrue(hasattr(to_field.args[0], 'year'))
def test_get_form_for_block_location(self): survey = load_schema_file("1_0102.json") block_json = SchemaHelper.get_block(survey, "reporting-period") location = SchemaHelper.get_first_location(survey) error_messages = SchemaHelper.get_messages(survey) form, _ = get_form_for_location(block_json, location, AnswerStore(), error_messages) self.assertTrue(hasattr(form, "period-to")) self.assertTrue(hasattr(form, "period-from")) period_from_field = getattr(form, "period-from") period_to_field = getattr(form, "period-to") self.assertIsInstance(period_from_field.day.validators[0], DateRequired) self.assertIsInstance(period_to_field.day.validators[0], DateRequired)
def test_routing_conditional_path(self): survey = load_schema_file("0_star_wars.json") expected_path = [ "f22b1ba4-d15f-48b8-a1f3-db62b6f34cc0", "96682325-47ab-41e4-a56e-8315a19ffe2a", "cd3b74d1-b687-4051-9634-a8f9ce10a27d", "an3b74d1-b687-4051-9634-a8f9ce10ard", "846f8514-fed2-4bd7-8fb2-4b5fcb1622b1" ] answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "Light Side", "2e0989b8-5185-4ba6-b73f-c126e3a06ba7": "No" } navigator = Navigator(survey) routing_path = navigator.get_routing_path(answers) self.assertEqual(routing_path, expected_path)
def test_next_location_goto_summary(self): survey = load_schema_file("0_star_wars.json") navigator = Navigator(survey) expected_path = [ 'introduction', 'f22b1ba4-d15f-48b8-a1f3-db62b6f34cc0', 'summary' ] answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "I prefer Star Trek" } current_location_id = expected_path[1] expected_next_location_id = expected_path[2] next_location_id = navigator.get_next_location(answers, current_location_id) self.assertEqual(next_location_id, expected_next_location_id)
def test_get_next_location_summary(self): survey = load_schema_file("0_star_wars.json") 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) current_location = Location('14ba4707-321d-441d-8d21-b8367366e766', 0, 'an3b74d1-b687-4051-9634-a8f9ce10ard') next_location = path_finder.get_next_location( current_location=current_location) expected_next_location = Location( "14ba4707-321d-441d-8d21-b8367366e766", 0, '846f8514-fed2-4bd7-8fb2-4b5fcb1622b1') self.assertEqual(expected_next_location, next_location) current_location = expected_next_location next_location = path_finder.get_next_location( current_location=current_location) expected_next_location = Location( "14ba4707-321d-441d-8d21-b8367366e766", 0, 'summary') self.assertEqual(expected_next_location, next_location)
def test_repeating_groups_conditional_location_path(self): survey = load_schema_file( "test_repeating_and_conditional_routing.json") expected_path = [ Location("repeat-value-group", 0, "introduction"), Location("repeat-value-group", 0, "no-of-repeats"), Location("repeated-group", 0, "repeated-block"), Location("repeated-group", 0, "age-block"), Location("repeated-group", 0, "shoe-size-block"), Location("repeated-group", 1, "repeated-block"), Location("repeated-group", 1, "shoe-size-block"), Location("repeated-group", 0, "summary"), Location("repeated-group", 0, "thank-you"), ] answer_1 = Answer(group_id="repeat-value-group", block_id="no-of-repeats", answer_id="no-of-repeats-answer", value="2") answer_2 = Answer(group_id="repeated-group", group_instance=0, block_id="repeated-block", answer_id="conditional-answer", value="Age and Shoe Size") answer_3 = Answer(group_id="repeated-group", group_instance=1, block_id="repeated-block", answer_id="conditional-answer", value="Shoe Size Only") answers = AnswerStore() answers.add(answer_1) answers.add(answer_2) answers.add(answer_3) path_finder = PathFinder(survey, answer_store=answers) self.assertEqual(expected_path, path_finder.get_location_path())
def test_get_form_deserialises_dates(self): survey = load_schema_file("1_0102.json") block_json = SchemaHelper.get_block(survey, "reporting-period") location = SchemaHelper.get_first_location(survey) error_messages = SchemaHelper.get_messages(survey) form, _ = get_form_for_location( block_json, location, AnswerStore([{ 'answer_id': 'period-from', 'group_id': 'rsi', 'group_instance': 0, 'block_id': 'reporting-period', 'value': '01/05/2015', 'answer_instance': 0, }, { 'answer_id': 'period-to', 'group_id': 'rsi', 'group_instance': 0, 'block_id': 'reporting-period', 'value': '01/09/2017', 'answer_instance': 0, }]), error_messages) self.assertTrue(hasattr(form, "period-to")) self.assertTrue(hasattr(form, "period-from")) period_to_field = getattr(form, "period-to") period_from_field = getattr(form, "period-from") self.assertEquals(period_from_field.data, { 'day': '01', 'month': '5', 'year': '2015', }) self.assertEquals(period_to_field.data, { 'day': '01', 'month': '9', 'year': '2017', })
def test_get_next_location_summary(self): survey = load_schema_file("0_star_wars.json") navigator = Navigator(survey) answers = { "ca3ce3a3-ae44-4e30-8f85-5b6a7a2fb23c": "Light Side", "2e0989b8-5185-4ba6-b73f-c126e3a06ba7": "No" } current_location_id = 'an3b74d1-b687-4051-9634-a8f9ce10ard' next_location_id = navigator.get_next_location(answers, current_location_id) expected_next_location_id = '846f8514-fed2-4bd7-8fb2-4b5fcb1622b1' self.assertEqual(expected_next_location_id, next_location_id) current_location_id = '846f8514-fed2-4bd7-8fb2-4b5fcb1622b1' next_location_id = navigator.get_next_location(answers, current_location_id) expected_next_location_id = 'summary' self.assertEqual(expected_next_location_id, next_location_id)
def setUp(self): survey = load_schema_file("0_rogue_one.json") self.navigator = Navigator(survey)