コード例 #1
0
    def test_location_url(self):
        location = RelationshipLocation(
            section_id="household",
            block_id="relationships",
            list_item_id="id1",
            to_list_item_id="id2",
        )
        location_url = location.url()

        self.assertEqual(
            location_url,
            "http://test.localdomain/questionnaire/relationships/id1/to/id2/",
        )
コード例 #2
0
def test_list_source_id_selector_primary_person(primary_person_list_item_id,
                                                expected_result):
    location = RelationshipLocation(
        section_id="some-section",
        block_id="some-block",
        list_item_id="item-1",
        to_list_item_id="item-2",
        list_name="household",
    )

    rule_evaluator = get_rule_evaluator(
        list_store=ListStore([{
            "name": "some-list",
            "primary_person": primary_person_list_item_id,
            "items": get_list_items(3),
        }]),
        location=location,
    )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "list",
                    "identifier": "some-list",
                    "selector": "primary_person",
                },
                {
                    "source": "location",
                    "identifier": "list_item_id"
                },
            ]
        }) is expected_result)
コード例 #3
0
    def test_primary_person_checks_location(self):
        answer_store = AnswerStore()
        list_store = ListStore(existing_items=[{
            "name": "people",
            "primary_person": "abcdef",
            "items": ["abcdef", "12345"],
        }])

        current_location = RelationshipLocation(
            section_id="some-section",
            block_id="some-block",
            list_item_id="abcdef",
            to_list_item_id="12345",
        )

        when_rules = [{
            "list": "people",
            "id_selector": "primary_person",
            "condition": "equals",
            "comparison": {
                "source": "location",
                "id": "list_item_id"
            },
        }]

        self.assertTrue(
            evaluate_when_rules(
                when_rules=when_rules,
                schema=get_schema(),
                metadata={},
                answer_store=answer_store,
                list_store=list_store,
                current_location=current_location,
            ))
コード例 #4
0
def unrelated_relationship_location(list_item_id):
    return RelationshipLocation(
        section_id="relationships-section",
        list_name="people",
        list_item_id=list_item_id,
        block_id="related-to-anyone-else",
    )
コード例 #5
0
def relationship_location(list_item_id, to_list_item_id):
    return RelationshipLocation(
        section_id="relationships-section",
        list_name="people",
        list_item_id=list_item_id,
        to_list_item_id=to_list_item_id,
        block_id="relationships",
    )
コード例 #6
0
    def test_for_json(self):
        location = RelationshipLocation(
            section_id="household",
            block_id="relationships",
            list_item_id="id1",
            to_list_item_id="id2",
        )
        json = location.for_json()

        self.assertEqual(
            json,
            {
                "section_id": "household",
                "block_id": "relationships",
                "list_item_id": "id1",
                "to_list_item_id": "id2",
            },
        )
コード例 #7
0
    def _individual_relationships_routing_path(self, from_list_item_id,
                                               to_list_item_ids):
        path = []
        number_of_unrelated_relationships = 0
        number_of_relationships_left = len(to_list_item_ids)
        unrelated_block_in_path = False
        for to_item_id in to_list_item_ids:
            if (self.unrelated_block_id and number_of_relationships_left >= 2
                    and number_of_unrelated_relationships == 2
                    and not unrelated_block_in_path):
                path.append(
                    RelationshipLocation(
                        section_id=self.section_id,
                        block_id=self.unrelated_block_id,
                        list_item_id=from_list_item_id,
                        list_name=self.list_name,
                    ))
                unrelated_block_in_path = True
                unrelated_answer = self.answer_store.get_answer(
                    self.unrelated_answer_id, from_list_item_id)
                if (unrelated_answer and unrelated_answer.value
                        in self.unrelated_no_answer_values):
                    return path

            path.append(
                RelationshipLocation(
                    section_id=self.section_id,
                    block_id=self.relationships_block_id,
                    list_item_id=from_list_item_id,
                    to_list_item_id=to_item_id,
                    list_name=self.list_name,
                ))
            relationship_answer = self.relationship_store.get_relationship(
                from_list_item_id, to_item_id)
            if (relationship_answer and relationship_answer.relationship
                    == self.UNRELATED_RELATIONSHIP_VALUE):
                number_of_unrelated_relationships += 1

            number_of_relationships_left -= 1

        return path
コード例 #8
0
def get_block_handler(
    schema,
    block_id,
    list_item_id,
    questionnaire_store,
    language,
    list_name=None,
    to_list_item_id=None,
    request_args=None,
    form_data=None,
):
    block = schema.get_block(block_id)

    if not block:
        raise InvalidLocationException(
            f"block id {block_id} is not valid for this schema"
        )

    if schema.is_block_in_repeating_section(block_id=block["id"]) and not all(
        (list_name, list_item_id)
    ):
        raise InvalidLocationException(
            f"block id {block_id} is in a repeating section without valid list_name/list_item_id"
        )

    block_type = block["type"]
    block_class = BLOCK_MAPPINGS.get(block_type)
    if not block_class:
        raise ValueError(f"block type {block_type} is not valid")

    section_id = schema.get_section_id_for_block_id(block_id)

    if to_list_item_id:
        location = RelationshipLocation(
            section_id=section_id,
            block_id=block_id,
            list_item_id=list_item_id,
            to_list_item_id=to_list_item_id,
            list_name=list_name,
        )
    else:
        location = Location(
            section_id=section_id,
            block_id=block_id,
            list_name=list_name,
            list_item_id=list_item_id,
        )

    return block_class(
        schema, questionnaire_store, language, location, request_args, form_data
    )
コード例 #9
0
    def test_create_location_from_dict(self):
        location_dict = {
            "section_id": "household",
            "block_id": "relationships",
            "list_item_id": "id1",
            "to_list_item_id": "id2",
        }

        location = RelationshipLocation(**location_dict)

        self.assertEqual(location.section_id, "household")
        self.assertEqual(location.block_id, "relationships")
        self.assertEqual(location.list_item_id, "id1")
        self.assertEqual(location.to_list_item_id, "id2")
コード例 #10
0
    def _generate_relationships_routing_path(section_id, block_id,
                                             list_item_ids):
        path = []
        for from_item in list_item_ids:
            from_index = list_item_ids.index(from_item)
            for to_item in list_item_ids[from_index + 1:]:
                path.append(
                    RelationshipLocation(
                        section_id=section_id,
                        block_id=block_id,
                        list_item_id=from_item,
                        to_list_item_id=to_item,
                    ))

        return path
コード例 #11
0
    def test_get_mapped_relationship_answers(self):
        schema = load_schema_from_name("test_relationships")
        location = RelationshipLocation(
            section_id="section",
            block_id="relationships",
            list_item_id="id1",
            to_list_item_id="id2",
        )
        answer_store = AnswerStore([{
            "answer_id":
            "relationship-answer",
            "value": [{
                "list_item_id": "id1",
                "to_list_item_id": "id2",
                "relationship": "Husband or Wife",
            }],
        }])

        mapped_answers = get_mapped_answers(schema, answer_store, location)
        self.assertEqual(len(mapped_answers), 1)
        expected_answers = OrderedDict(
            {"relationship-answer": "Husband or Wife"})
        self.assertEqual(mapped_answers, expected_answers)