Beispiel #1
0
    def test_remove_all_answers_with_list_item_id(self):
        answer_store = AnswerStore(existing_answers=[
            {
                "answer_id": "test1",
                "value": 1,
                "list_item_id": "abcdef"
            },
            {
                "answer_id": "test2",
                "value": 2,
                "list_item_id": "abcdef"
            },
            {
                "answer_id": "test3",
                "value": 3,
                "list_item_id": "uvwxyz"
            },
        ])

        questionnaire_store = MagicMock(
            spec=QuestionnaireStore,
            completed_blocks=[],
            answer_store=answer_store,
            list_store=MagicMock(spec=ListStore),
            progress_store=ProgressStore(),
        )

        self.questionnaire_store_updater = QuestionnaireStoreUpdater(
            self.location, self.schema, questionnaire_store,
            self.current_question)
        self.questionnaire_store_updater.remove_list_item_and_answers(
            "abc", "abcdef")

        assert len(answer_store) == 1
        assert answer_store.get_answer("test3", "uvwxyz")
Beispiel #2
0
def add_relationships_unrelated_answers(
    answer_store: AnswerStore,
    list_store: ListStore,
    schema: QuestionnaireSchema,
    section_id: str,
    relationships_block: Mapping,
    answers_payload: AnswerStore,
) -> Optional[RelationshipStore]:
    relationships_answer_id = schema.get_first_answer_id_for_block(
        relationships_block["id"])
    relationships_answer = answer_store.get_answer(relationships_answer_id)
    if not relationships_answer:
        return None

    relationship_store = RelationshipStore(
        relationships_answer.value)  # type: ignore
    list_name = relationships_block["for_list"]
    unrelated_block = relationships_block["unrelated_block"]
    unrelated_block_id = unrelated_block["id"]
    unrelated_answer_id = schema.get_first_answer_id_for_block(
        unrelated_block_id)
    unrelated_no_answer_values = schema.get_unrelated_block_no_answer_values(
        unrelated_answer_id)

    relationship_router = RelationshipRouter(
        answer_store=answer_store,
        relationship_store=relationship_store,
        section_id=section_id,
        list_name=list_name,
        list_item_ids=list_store[list_name].items,
        relationships_block_id=relationships_block["id"],
        unrelated_block_id=unrelated_block_id,
        unrelated_answer_id=unrelated_answer_id,
        unrelated_no_answer_values=unrelated_no_answer_values,
    )

    for location in relationship_router.path:
        if location.block_id == unrelated_block_id and (
                unrelated_answer := answer_store.get_answer(
                    unrelated_answer_id, list_item_id=location.list_item_id)):
            answers_payload.add_or_update(unrelated_answer)
Beispiel #3
0
def add_list_collector_answers(
    answer_store: AnswerStore,
    list_store: ListStore,
    schema: QuestionnaireSchema,
    list_collector_block: Mapping,
    answers_payload: AnswerStore,
) -> None:
    """Add answers from a ListCollector block.
    Output is added to the `answers_payload` argument."""

    answers_ids_in_add_block = schema.get_answer_ids_for_list_items(
        list_collector_block["id"])
    list_name = list_collector_block["for_list"]
    list_item_ids = list_store[list_name].items
    if answers_ids_in_add_block:
        for list_item_id in list_item_ids:
            for answer_id in answers_ids_in_add_block:
                answer = answer_store.get_answer(answer_id, list_item_id)
                if answer:
                    answers_payload.add_or_update(answer)