Esempio n. 1
0
def test_transform_variants_with_question_variants(question_variant_schema):
    schema = QuestionnaireSchema(question_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, transformed_block, "Question 1, 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, transformed_block, "Question 1, Yes")
Esempio n. 2
0
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")
Esempio n. 3
0
    def rendered_block(self):
        transformed_block = transform_variants(
            self.block,
            self._schema,
            self._questionnaire_store.metadata,
            self._questionnaire_store.answer_store,
            self._questionnaire_store.list_store,
            self._current_location,
        )

        content_page_title = transformed_block.get(
            "page_title") or self._get_content_title(transformed_block)
        self._set_page_title(content_page_title)
        return self.placeholder_renderer.render(
            transformed_block, self._current_location.list_item_id)
Esempio n. 4
0
    def _render_block(self, block_id):
        block_schema = self._schema.get_block(block_id)
        transformed_block = transform_variants(
            block_schema,
            self._schema,
            self._questionnaire_store.metadata,
            self._questionnaire_store.answer_store,
            self._questionnaire_store.list_store,
            self._current_location,
        )

        self.page_title = self._get_page_title(transformed_block)

        return self.placeholder_renderer.render(
            transformed_block, self._current_location.list_item_id
        )
Esempio n. 5
0
def test_transform_variants_with_no_variants(question_schema):
    schema = QuestionnaireSchema(question_schema)
    answer_store = AnswerStore({})
    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"]),
    )

    assert transformed_block == block
Esempio n. 6
0
    def _render_block(self, block_id):
        block_schema = self._schema.get_block(block_id)

        variant_block = transform_variants(
            block_schema,
            self._schema,
            self._questionnaire_store.metadata,
            self._questionnaire_store.answer_store,
            self._questionnaire_store.list_store,
            self._current_location,
        )

        rendered_question = self.placeholder_renderer.render(
            variant_block["question"], self._current_location.list_item_id)

        if variant_block["question"]:
            self.page_title = self._get_page_title(variant_block["question"])

        return {**variant_block, **{"question": rendered_question}}
Esempio n. 7
0
def test_transform_variants_with_content(content_variant_schema):
    schema = QuestionnaireSchema(content_variant_schema)
    answer_store = AnswerStore({})
    answer_store.add_or_update(Answer(answer_id="age-answer", value="18"))
    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"]),
    )

    assert transformed_block != block
    assert "content_variants" not in transformed_block
    assert transformed_block["content"][0]["title"] == "You are over 16"
Esempio n. 8
0
    def rendered_block(self):
        transformed_block = transform_variants(
            self.block,
            self._schema,
            self._questionnaire_store.metadata,
            self._questionnaire_store.answer_store,
            self._questionnaire_store.list_store,
            self._current_location,
        )
        page_title = transformed_block.get("page_title") or self._get_safe_page_title(
            transformed_block["question"]["title"]
        )

        self._set_page_title(page_title)
        rendered_question = self.placeholder_renderer.render(
            transformed_block["question"], self._current_location.list_item_id
        )
        return {
            **transformed_block,
            **{"question": rendered_question},
        }
    def _remove_unwanted_questions_answers(self, block, answer_ids_to_keep,
                                           current_location):
        """
        Evaluates questions in a block and removes any questions not containing a relevant answer
        """
        transformed_block = transform_variants(
            block,
            self._schema,
            self._metadata,
            self._answer_store,
            self._list_store,
            current_location=current_location,
        )
        transformed_block = deepcopy(transformed_block)
        transformed_block = QuestionnaireSchema.get_mutable_deepcopy(
            transformed_block)
        block_question = transformed_block["question"]

        matching_answers = []
        for answer_id in answer_ids_to_keep:
            matching_answers.extend(
                self._schema.get_answers_by_answer_id(answer_id))

        questions_to_keep = [
            self._schema.parent_id_map[answer["id"]]
            for answer in matching_answers
        ]

        if block_question["id"] in questions_to_keep:
            answers_to_keep = [
                answer for answer in block_question["answers"]
                if answer["id"] in answer_ids_to_keep
            ]
            block_question["answers"] = answers_to_keep

        return transformed_block