def test_context_for_section_list_summary(people_answer_store): schema = load_schema_from_name("test_list_collector_section_summary") summary_context = SectionSummaryContext( language=DEFAULT_LANGUAGE_CODE, schema=schema, answer_store=people_answer_store, list_store=ListStore( [ {"items": ["PlwgoG", "UHPLbX"], "name": "people"}, {"items": ["gTrlio"], "name": "visitors"}, ] ), progress_store=ProgressStore(), metadata={"display_address": "70 Abingdon Road, Goathill"}, response_metadata={}, current_location=Location(section_id="section"), routing_path=RoutingPath( [ "primary-person-list-collector", "list-collector", "visitor-list-collector", ], section_id="section", ), ) context = summary_context() expected = { "summary": { "answers_are_editable": True, "collapsible": False, "custom_summary": [ { "add_link": "/questionnaire/people/add-person/?return_to=section-summary", "add_link_text": "Add someone to this household", "empty_list_text": "There are no householders", "list": { "editable": True, "list_items": [ { "edit_link": "/questionnaire/people/PlwgoG/edit-person/?return_to=section-summary", "item_title": "Toni Morrison", "primary_person": False, "remove_link": "/questionnaire/people/PlwgoG/remove-person/?return_to=section-summary", "list_item_id": "PlwgoG", }, { "edit_link": "/questionnaire/people/UHPLbX/edit-person/?return_to=section-summary", "item_title": "Barry Pheloung", "primary_person": False, "remove_link": "/questionnaire/people/UHPLbX/remove-person/?return_to=section-summary", "list_item_id": "UHPLbX", }, ], }, "list_name": "people", "title": "Household members staying overnight on 13 October 2019 at 70 Abingdon Road, Goathill", "type": "List", }, { "add_link": "/questionnaire/visitors/add-visitor/?return_to=section-summary", "add_link_text": "Add another visitor to this household", "empty_list_text": "There are no visitors", "list": { "editable": True, "list_items": [ { "edit_link": "/questionnaire/visitors/gTrlio/edit-visitor-person/?return_to=section-summary", "item_title": "", "primary_person": False, "remove_link": "/questionnaire/visitors/gTrlio/remove-visitor/?return_to=section-summary", "list_item_id": "gTrlio", } ], }, "list_name": "visitors", "title": "Visitors staying overnight on 13 October 2019 at 70 Abingdon Road, Goathill", "type": "List", }, ], "page_title": "People who live here and overnight visitors", "summary_type": "SectionSummary", "title": "People who live here and overnight visitors", } } assert context == expected
def test_context_for_driving_question_summary(): schema = load_schema_from_name("test_list_collector_driving_question") summary_context = SectionSummaryContext( DEFAULT_LANGUAGE_CODE, schema, AnswerStore( [ {"answer_id": "anyone-usually-live-at-answer", "value": "Yes"}, {"answer_id": "first-name", "value": "Toni", "list_item_id": "PlwgoG"}, { "answer_id": "last-name", "value": "Morrison", "list_item_id": "PlwgoG", }, ] ), ListStore([{"items": ["PlwgoG"], "name": "people"}]), ProgressStore(), {}, {}, current_location=Location(section_id="section"), routing_path=RoutingPath( ["anyone-usually-live-at", "anyone-else-live-at"], section_id="section" ), ) context = summary_context() expected = { "summary": { "answers_are_editable": True, "collapsible": False, "custom_summary": [ { "add_link": "/questionnaire/people/add-person/?return_to=section-summary", "add_link_text": "Add someone to this household", "empty_list_text": "There are no householders", "list": { "editable": True, "list_items": [ { "item_title": "Toni Morrison", "primary_person": False, "edit_link": "/questionnaire/people/PlwgoG/edit-person/?return_to=section-summary", "remove_link": "/questionnaire/people/PlwgoG/remove-person/?return_to=section-summary", "list_item_id": "PlwgoG", } ], }, "list_name": "people", "title": "Household members", "type": "List", } ], "page_title": "List Collector Driving Question Summary", "summary_type": "SectionSummary", "title": "List Collector Driving Question Summary", } } assert context == expected
def test_primary_person_not_in_payload_when_not_answered( fake_questionnaire_store): routing_path = [ RoutingPath( [ "list-collector", "next-interstitial", "another-list-collector-block" ], section_id="section-1", ) ] answer_objects = [ { "answer_id": "first-name", "value": "1", "list_item_id": "xJlKBy" }, { "answer_id": "last-name", "value": "1", "list_item_id": "xJlKBy" }, { "answer_id": "first-name", "value": "2", "list_item_id": "RfAGDc" }, { "answer_id": "last-name", "value": "2", "list_item_id": "RfAGDc" }, { "answer_id": "anyone-else", "value": "No" }, { "answer_id": "another-anyone-else", "value": "No" }, { "answer_id": "extraneous-answer", "value": "Bad", "list_item_id": "123" }, ] answers = AnswerStore(answer_objects) list_store = ListStore(existing_items=[{ "name": "people", "items": ["xJlKBy", "RfAGDc"] }]) fake_questionnaire_store.answer_store = answers fake_questionnaire_store.list_store = list_store schema = load_schema("test_list_collector") output = convert_answers(schema, fake_questionnaire_store, routing_path) data_dict = json.loads(json.dumps(output["data"]["lists"], for_json=True)) assert "primary_person" not in data_dict[0]
def test_when_rule_comparing_answer_values(self): answers = { "low": Answer(answer_id="low", value=1), "medium": Answer(answer_id="medium", value=5), "high": Answer(answer_id="high", value=10), "list_answer": Answer(answer_id="list_answer", value=["a", "abc", "cba"]), "other_list_answer": Answer( answer_id="other_list_answer", value=["x", "y", "z"] ), "other_list_answer_2": Answer( answer_id="other_list_answer_2", value=["a", "abc", "cba"] ), "text_answer": Answer(answer_id="small_string", value="abc"), "other_text_answer": Answer(answer_id="other_string", value="xyz"), } # An answer that won't be added to the answer store. missing_answer = Answer(answer_id="missing", value=1) param_list = [ (answers["medium"], "equals", answers["medium"], True), (answers["medium"], "equals", answers["low"], False), (answers["medium"], "greater than", answers["low"], True), (answers["medium"], "greater than", answers["high"], False), (answers["medium"], "less than", answers["high"], True), (answers["medium"], "less than", answers["low"], False), (answers["medium"], "equals", missing_answer, False), (answers["list_answer"], "contains", answers["text_answer"], True), (answers["list_answer"], "contains", answers["other_text_answer"], False), ( answers["list_answer"], "not contains", answers["other_text_answer"], True, ), (answers["list_answer"], "not contains", answers["text_answer"], False), ( answers["list_answer"], "contains any", answers["other_list_answer_2"], True, ), ( answers["list_answer"], "contains any", answers["other_list_answer"], False, ), ( answers["list_answer"], "contains all", answers["other_list_answer"], False, ), ( answers["list_answer"], "contains all", answers["other_list_answer_2"], True, ), (answers["text_answer"], "equals any", answers["list_answer"], True), (answers["text_answer"], "equals any", answers["other_list_answer"], False), ( answers["text_answer"], "not equals any", answers["other_list_answer"], True, ), (answers["text_answer"], "not equals any", answers["list_answer"], False), ] for lhs, comparison, rhs, expected_result in param_list: # Given with self.subTest( lhs=lhs, comparison=comparison, rhs=rhs, expected_result=expected_result ): answer_store = AnswerStore() for answer in answers.values(): answer_store.add_or_update(answer) when = [ { "id": lhs.answer_id, "condition": comparison, "comparison": {"id": rhs.answer_id, "source": "answers"}, } ] current_location = Location( section_id="some-section", block_id="some-block" ) self.assertEqual( evaluate_when_rules( when_rules=when, schema=get_schema(), metadata={}, answer_store=answer_store, list_store=ListStore(), current_location=current_location, routing_path_block_ids=None, ), expected_result, )
def people_list_store(): return ListStore([{"items": ["PlwgoG", "UHPLbX"], "name": "people"}])
def list_store(): return ListStore()
def setUp(self): super().setUp() self.schema = load_schema_from_name("test_calculated_summary") answers = [ { "value": 1, "answer_id": "first-number-answer" }, { "value": 2, "answer_id": "second-number-answer" }, { "value": 3, "answer_id": "second-number-answer-unit-total" }, { "value": 4, "answer_id": "second-number-answer-also-in-total" }, { "value": 5, "answer_id": "third-number-answer" }, { "value": 6, "answer_id": "third-and-a-half-number-answer-unit-total" }, { "value": "No", "answer_id": "skip-fourth-block-answer" }, { "value": 7, "answer_id": "fourth-number-answer" }, { "value": 8, "answer_id": "fourth-and-a-half-number-answer-also-in-total" }, { "value": 9, "answer_id": "fifth-percent-answer" }, { "value": 10, "answer_id": "fifth-number-answer" }, { "value": 11, "answer_id": "sixth-percent-answer" }, { "value": 12, "answer_id": "sixth-number-answer" }, ] self.answer_store = AnswerStore(answers) self.list_store = ListStore() self.progress_store = ProgressStore() self.block_type = "CalculatedSummary"
def test_placeholder_resolves_list_has_items_chain(mock_schema): list_store = ListStore([{ "items": ["abc123", "cde456", "fgh789"], "same_name_items": ["abc123", "fgh789"], "name": "people", }]) answer_store = AnswerStore([ { "answer_id": "first-name-answer", "value": "Joe", "list_item_id": "abc123", }, { "answer_id": "middle-names-answer", "value": "Michael", "list_item_id": "abc123", }, { "answer_id": "last-name-answer", "value": "Smith", "list_item_id": "abc123", }, { "answer_id": "first-name-answer", "value": "Marie", "list_item_id": "cde456", }, { "answer_id": "middle-names-answer", "value": "Jane", "list_item_id": "cde456", }, { "answer_id": "last-name-answer", "value": "Smith", "list_item_id": "cde456", }, ]) placeholder_transforms = [{ "placeholder": "persons_name", "transforms": [ { "transform": "list_has_items", "arguments": { "list_to_check": { "source": "list", "id_selector": "same_name_items", "identifier": "people", }, }, }, { "transform": "format_name", "arguments": { "include_middle_names": { "source": "previous_transform" }, "first_name": { "source": "answers", "identifier": "first-name-answer", }, "middle_names": { "source": "answers", "identifier": "middle-names-answer", }, "last_name": { "source": "answers", "identifier": "last-name-answer", }, }, }, ], }] mock_schema.is_repeating_answer = Mock(return_value=True) parser = PlaceholderParser( language="en", schema=mock_schema, list_store=list_store, answer_store=answer_store, list_item_id="abc123", ) placeholders = parser(placeholder_transforms) assert placeholders["persons_name"] == "Joe Michael Smith" parser = PlaceholderParser( language="en", schema=mock_schema, list_store=list_store, answer_store=answer_store, list_item_id="cde456", ) placeholders = parser(placeholder_transforms) assert placeholders["persons_name"] == "Marie Jane Smith"
def test_evaluate_when_rule_raises_if_bad_when_condition(self): when = {"when": [{"condition": "not set"}]} answer_store = AnswerStore() with self.assertRaises(Exception): evaluate_when_rules(when["when"], get_schema(), {}, answer_store, ListStore(), None)
def test_get_item(): store = ListStore() assert store["not_a_list"] == ListModel("not_a_list")
def test_delete_list_item_id(): store = ListStore() person = store.add_list_item("people") store.delete_list_item("people", person) assert not store._lists # pylint: disable=protected-access