def test_should_repeat_for_answer_answer_value(self): # Given repeat = {'answer_id': 'my_answer', 'type': 'answer_value'} answer_store = AnswerStore() answer_store.add(Answer(answer_id='my_answer', value='3')) # When number_of_repeats = evaluate_repeat(repeat, answer_store) self.assertEqual(number_of_repeats, 3)
def test_build_answers_excludes_answers_not_in_routing_path(self): # Given self.answer_store.add(Answer( answer_id='first_name', value='Joe', )) self.answer_store.add(Answer( answer_id='lastname', value='Bloggs', )) answer_ids_on_path = ['first_name'] # When context_answers = _build_answers(self.answer_store, self.schema, answer_ids_on_path) # Then self.assertEqual(len(context_answers), 1) self.assertEqual(context_answers['first_name'], 'Joe')
def test_matches_new_answer_against_old_answer(self): # Has group_instance_id answer_1 = Answer( answer_id='4', answer_instance=1, group_instance=1, group_instance_id=None, value=25, ) # No group_instance_id answer_2 = { 'answer_id': '4', 'answer_instance': 1, 'group_instance': 1, 'value': 25, } self.assertEqual(answer_1.matches_dict(answer_2), True)
def test_remove_all_answers(self): answer_1 = Answer( group_id="group1", block_id="block1", answer_id="answer1", value=10, ) answer_2 = Answer( group_id="group1", block_id="block1", answer_id="answer2", value=20, ) self.store.add(answer_1) self.store.add(answer_2) self.store.remove() self.assertEqual(self.store.map(), {})
def test_matches_answer_dict(self): answer_1 = Answer( block_id="3", answer_id="4", answer_instance=1, group_id="5", group_instance=1, value=25, ) answer_2 = { "block_id": "3", "answer_id": "4", "answer_instance": 1, "group_id": "5", "group_instance": 1, "value": 25, } self.assertEqual(answer_1.matches_dict(answer_2), True)
def test_get_answer_with_list(basic_answer_store): assert basic_answer_store.get_answer("answer1", "abc123") == Answer.from_dict({ "answer_id": "answer1", "list_item_id": "abc123", "value": 10 })
def test_matches_answer(self): answer_1 = Answer( block_id="3", answer_id="4", answer_instance=1, group_id="5", group_instance=1, value=25, ) answer_2 = Answer( block_id="3", answer_id="4", answer_instance=1, group_id="5", group_instance=1, value=65, ) self.assertEqual(answer_1.matches(answer_2), True)
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_filter_chaining_count(self): self.store.add(Answer( answer_id='1', answer_instance=0, group_instance=1, value=25, )) self.store.add(Answer( answer_id='2', answer_instance=0, group_instance=1, value="'Twenty Five'", )) self.assertEqual(self.store.count(), 2) self.assertEqual(self.store.filter(answer_ids=['2']).count(), 1) self.assertEqual(self.store.filter(answer_ids=['1', '2']).count(), 2)
def test_checkbox_answer_with_detail_answer_returns_the_value(self): # Given self.answer_store.add_or_update( Answer(answer_id="answer_1", value=["Light Side", "Other"])) self.answer_store.add_or_update( Answer(answer_id="child_answer", value="Test")) options = [ { "label": "Light Side", "value": "Light Side" }, { "label": "Other", "value": "Other", "detail_answer": { "id": "child_answer", "type": "TextField" }, }, ] answer_schema = [{ "id": "answer_1", "label": "Which side?", "type": "Checkbox", "options": options, }] question_schema = { "id": "question_id", "title": "question_title", "type": "GENERAL", "answers": answer_schema, } # When question = Question(question_schema, self.answer_store, self.schema, None) # Then self.assertEqual(len(question.answers[0]["value"]), 2) self.assertEqual( question.answers[0]["value"][1]["detail_answer_value"], "Test")
def test_transform_variants_list_collector(list_collector_variant_schema): schema = QuestionnaireSchema(list_collector_variant_schema) answer_store = AnswerStore({}) answer_store.add_or_update(Answer(answer_id="when-answer", value="no")) metadata = {} block = schema.get_block("block1") section_id = schema.get_section_id_for_block_id(block["id"]) transformed_block = transform_variants( block, schema, metadata, answer_store, ListStore({}), Location(section_id=section_id, block_id=block["id"]), ) compare_transformed_block(block["add_block"], transformed_block["add_block"], "Add, No") compare_transformed_block(block["remove_block"], transformed_block["remove_block"], "Remove, No") compare_transformed_block(block["edit_block"], transformed_block["edit_block"], "Edit, No") answer_store.add_or_update(Answer(answer_id="when-answer", value="yes")) transformed_block = transform_variants( block, schema, metadata, answer_store, ListStore({}), Location(section_id=section_id, block_id=block["id"]), ) compare_transformed_block(block["add_block"], transformed_block["add_block"], "Add, Yes") compare_transformed_block(block["remove_block"], transformed_block["remove_block"], "Remove, Yes") compare_transformed_block(block["edit_block"], transformed_block["edit_block"], "Edit, Yes")
def test_checkbox_answer_with_numeric_detail_answer_returns_number(self): # Given self.answer_store.add_or_update( Answer(answer_id="answer_1", value=[1, "Other"])) self.answer_store.add_or_update( Answer(answer_id="child_answer", value=2)) options = [ { "label": 1, "value": 1 }, { "label": "Other", "value": "Other", "detail_answer": { "id": "child_answer", "type": "Number" }, }, ] answer_schema = [{ "id": "answer_1", "label": "How many cakes have you had today?", "type": "Checkbox", "options": options, }] question_schema = { "id": "question_id", "title": "question_title", "type": "GENERAL", "answers": answer_schema, } # When question = Question(question_schema, self.answer_store, self.schema, None) # Then self.assertEqual(len(question.answers[0]["value"]), 2) self.assertEqual( question.answers[0]["value"][1]["detail_answer_value"], 2)
def test_checkbox_button_other_option_text(self): # Given self.answer_store.add_or_update( Answer(answer_id="answer_1", value=["Light Side", "other"])) self.answer_store.add_or_update( Answer(answer_id="child_answer", value="Neither")) options = [ { "label": "Light Side", "value": "Light Side" }, { "label": "other", "value": "other", "detail_answer": { "id": "child_answer" }, }, ] answer_schema = { "id": "answer_1", "label": "Which side?", "type": "Checkbox", "options": options, } question_schema = { "id": "question_id", "title": "question_title", "type": "GENERAL", "answers": [answer_schema], } # When question = Question(question_schema, self.answer_store, self.schema, None) # Then self.assertEqual(len(question.answers[0]["value"]), 2) self.assertEqual(question.answers[0]["value"][0]["label"], "Light Side") self.assertEqual( question.answers[0]["value"][1]["detail_answer_value"], "Neither")
def serialise_relationship_answers(location, answer_id, listfield_data): answers = [] for index, listfield_value in enumerate(listfield_data): answer = Answer( location=location, answer_id=answer_id, answer_instance=index, value=listfield_value, ) answers.append(answer) return answers
def test_add_inserts_instances(self): answer_1 = Answer( block_id="3", answer_id="4", answer_instance=1, group_id="5", group_instance=1, value=25, ) self.store.add(answer_1) answer_1.answer_instance = 2 self.store.add(answer_1) answer_1.answer_instance = 3 self.store.add(answer_1) self.assertEqual(len(self.store.answers), 3)
def test_updates_answer_no_list_id(basic_answer_store): store_length = len(basic_answer_store) duplicate_answer = Answer(answer_id="answer3", value=300) basic_answer_store.add_or_update(duplicate_answer) assert len(basic_answer_store) == store_length assert basic_answer_store.get_answer("answer3").value == 300
def test_go_to_next_question_for_multiple_answers(self): # Given goto = { 'id': 'next-question', 'when': [{ 'id': 'my_answer', 'condition': 'equals', 'value': 'Yes' }, { 'id': 'my_other_answer', 'condition': 'equals', 'value': '2' }] } answer_store = AnswerStore() answer_store.add(Answer(answer_id='my_answer', value='Yes')) answer_store.add(Answer(answer_id='my_other_answer', value='2')) self.assertTrue(evaluate_goto(goto, {}, answer_store, 0))
def test_remove_answers_by_block_id_that_does_not_exist(self): answer_1 = Answer( group_id="group1", block_id="block1", answer_id="answer1", value=10, ) answer_2 = Answer( group_id="group1", block_id="block1", answer_id="answer2", value=20, ) self.store.add(answer_1) self.store.add(answer_2) self.store.remove(block_id="block2") expected_answers = {"answer1": 10, "answer2": 20} self.assertEqual(self.store.map(), expected_answers)
def serialise_composition_answers(data): answers = [] for index, person_data in enumerate(data): for answer_id in person_data.keys(): answer = Answer( answer_id=answer_id, answer_instance=index, value=person_data[answer_id], ) answers.append(answer) return answers
def test_adds_answer(self): answer = Answer( answer_id='4', answer_instance=1, group_instance=1, value=25, ) self.store.add(answer) self.assertEqual(len(self.store.answers), 1)
def test_build_relationship_choices_limited(self): answer_store = AnswerStore() for i in range(0, 50): answer_store.add( Answer( answer_id='first-name', answer_instance=i, value='Joe' + str(i), )) answer_store.add( Answer( answer_id='last-name', answer_instance=i, value='Bloggs' + str(i), )) choices = build_relationship_choices(answer_store, 0) self.assertEqual(len(choices), answer_store.EQ_MAX_NUM_REPEATS - 1)
def test_list_serialisation(store_to_serialise): serialised_store = list(store_to_serialise) assert serialised_store == [ Answer.from_dict({ "answer_id": "answer1", "value": 10, "list_item_id": "abc123" }), Answer.from_dict({ "answer_id": "answer2", "value": 20, "list_item_id": "xyz987" }), Answer.from_dict({ "answer_id": "answer3", "value": 30, "list_item_id": None }), ]
def test_return_to_summary_if_complete(self): schema = load_schema_from_params('test', 'summary') # All blocks completed completed_blocks = [ Location('summary-group', 0, 'radio'), Location('summary-group', 0, 'test-number-block'), Location('dessert-group', 0, 'dessert-block') ] answer_store = AnswerStore() answer_store.add(Answer(answer_id='radio-answer', value='Bacon')) answer_store.add(Answer(answer_id='test-currency', value='123')) answer_store.add(Answer(answer_id='dessert', value='Cake')) summary_location = Location('dessert-group', 0, 'summary') path_finder = PathFinder(schema, answer_store, metadata={}, completed_blocks=completed_blocks) self.assertEqual(path_finder.get_next_location(current_location=completed_blocks[0]), summary_location)
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_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_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_excessive_repeating_groups_conditional_location_path(self): schema = load_schema_from_params('test', 'repeating_and_conditional_routing') schema.answer_is_in_repeating_group = MagicMock(return_value=False) answers = AnswerStore() answers.add(Answer( answer_id='no-of-repeats-answer', value='10000' )) for i in range(50): answers.add(Answer( group_instance=i, answer_id='conditional-answer', value='Shoe Size Only' )) path_finder = PathFinder(schema, answer_store=answers, metadata={}, completed_blocks=[]) self.assertEqual('summary', path_finder.get_full_routing_path().pop().block_id)
def test_return_to_section_summary_if_section_complete(self): schema = load_schema_from_params('test', 'section_summary') schema.answer_is_in_repeating_group = MagicMock(return_value=False) # All blocks completed completed_blocks = [ Location('property-details', 0, 'insurance-type'), Location('property-details', 0, 'insurance-address'), Location('address-length', 0, 'address-duration'), ] answer_store = AnswerStore() answer_store.add(Answer(answer_id='insurance-type-answer', value='Contents')) answer_store.add(Answer(answer_id='insurance-address-answer', value='123 Test Road')) answer_store.add(Answer(answer_id='address-duration-answer', value='No')) summary_location = Location('property-details-summary-group', 0, 'property-details-summary') path_finder = PathFinder(schema, answer_store, metadata={}, completed_blocks=completed_blocks) self.assertEqual(path_finder.get_next_location(current_location=completed_blocks[0]), summary_location)
def test_filter_answers_does_not_escapes_values(self): self.store.add(Answer( answer_id='1', answer_instance=0, group_instance=1, value=25, )) self.store.add(Answer( answer_id='2', answer_instance=0, group_instance=1, value="'Twenty Five'", )) filtered = self.store.filter(['1', '2']) self.assertEqual(len(filtered.answers), 2) self.assertEqual(filtered[0]['value'], 25) self.assertEqual(filtered[1]['value'], "'Twenty Five'")
def test_raises_error_on_update_nonexisting(self): answer_1 = Answer( answer_id='4', answer_instance=1, group_instance=1, value=25, ) with self.assertRaises(ValueError) as ite: self.store.update(answer_1) self.assertIn('Answer instance does not exist in store', str(ite.exception))