def test_answer_source_with_routing_path_block_ids_outside_repeat(
        is_answer_on_path):
    schema = get_mock_schema()

    location = Location(section_id="test-section", block_id="test-block")

    if is_answer_on_path:
        schema.get_block_for_answer_id = Mock(
            return_value={"id": f"block-on-path"})
        answer_id = "answer-on-path"
        expected_result = "Yes"
    else:
        schema.get_block_for_answer_id = Mock(
            return_value={"id": f"block-not-on-path"})
        answer_id = "answer-not-on-path"
        expected_result = None

    answer = Answer(answer_id=answer_id, value="Yes")

    value_source_resolver = get_value_source_resolver(
        schema=schema,
        answer_store=AnswerStore([answer.to_dict()]),
        list_store=ListStore([{
            "name": "some-list",
            "items": get_list_items(3)
        }]),
        location=location,
        list_item_id=location.list_item_id,
        routing_path_block_ids=["block-on-path"],
    )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "answer-on-path"
    }) == expected_result)
Example #2
0
def get_rule_evaluator(
    *,
    language="en",
    schema: QuestionnaireSchema = None,
    answer_store: AnswerStore = AnswerStore(),
    list_store: ListStore = ListStore(),
    metadata: Optional[dict] = None,
    response_metadata: Mapping = None,
    location: Union[Location,
                    RelationshipLocation] = Location(section_id="test-section",
                                                     block_id="test-block"),
    routing_path_block_ids: Optional[list] = None,
):
    if not schema:
        schema = get_mock_schema()
        schema.is_repeating_answer = Mock(return_value=True)
        schema.get_default_answer = Mock(return_value=None)

    return RuleEvaluator(
        language=language,
        schema=schema,
        metadata=metadata or {},
        response_metadata=response_metadata,
        answer_store=answer_store,
        list_store=list_store,
        location=location,
        routing_path_block_ids=routing_path_block_ids,
    )
Example #3
0
def test_answer_source_with_list_item_selector_list_first_item(
        answer_value, expected_result):
    rule_evaluator = get_rule_evaluator(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "list_item_id": "item-1",
            "value": answer_value,
        }]),
        list_store=ListStore([{
            "name": "some-list",
            "items": get_list_items(3)
        }]),
    )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "answers",
                    "identifier": "some-answer",
                    "list_item_selector": {
                        "source": "list",
                        "identifier": "some-list",
                        "selector": "first",
                    },
                },
                3,
            ]
        }) is expected_result)
def get_value_source_resolver(
    schema: QuestionnaireSchema = None,
    answer_store: AnswerStore = AnswerStore(),
    list_store: ListStore = ListStore(),
    metadata: Optional[dict] = None,
    location: Union[Location,
                    RelationshipLocation] = Location(section_id="test-section",
                                                     block_id="test-block"),
    list_item_id: Optional[str] = None,
    routing_path_block_ids: Optional[list] = None,
    use_default_answer=False,
    escape_answer_values=False,
):
    if not schema:
        schema = get_mock_schema()
        schema.is_repeating_answer = Mock(return_value=bool(list_item_id))

    if not use_default_answer:
        schema.get_default_answer = Mock(return_value=None)

    return ValueSourceResolver(
        answer_store=answer_store,
        list_store=list_store,
        metadata=metadata,
        schema=schema,
        location=location,
        list_item_id=list_item_id,
        routing_path_block_ids=routing_path_block_ids,
        use_default_answer=use_default_answer,
        escape_answer_values=escape_answer_values,
    )
Example #5
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)
Example #6
0
def test_nested_rules(operator, operands, expected_result):
    rule_evaluator = get_rule_evaluator(
        answer_store=AnswerStore([
            {
                "answer_id": "answer-1",
                "list_item_id": "item-1",
                "value": "Yes, I do",
            },
            {
                "answer_id": "answer-2",
                "list_item_id": "item-1",
                "value": 10,
            },
        ]),
        metadata={
            "region_code": "GB-NIR",
            "language_code": "en"
        },
        list_store=ListStore([{
            "name": "some-list",
            "items": get_list_items(5),
            "same_name_items": get_list_items(3),
        }], ),
        location=Location(section_id="some-section",
                          block_id="some-block",
                          list_item_id="item-1"),
    )

    assert rule_evaluator.evaluate(
        rule={operator: operands}) is expected_result
Example #7
0
 def setUp(self):
     super().setUp()
     self.answer_schema = MagicMock()
     self.answer_store = AnswerStore()
     self.list_store = ListStore()
     self.schema = MagicMock()
     self.metadata = {}
     self.response_metadata = {}
Example #8
0
def rule_evaluator(mock_schema, response_metadata):
    evaluator = RuleEvaluator(
        answer_store=AnswerStore(),
        list_store=ListStore(),
        metadata={},
        response_metadata=response_metadata,
        schema=mock_schema,
        location=None,
    )

    return evaluator
def test_list_source(list_count):
    value_source_resolver = get_value_source_resolver(list_store=ListStore([{
        "name":
        "some-list",
        "items":
        get_list_items(list_count)
    }]), )

    assert (value_source_resolver.resolve({
        "source": "list",
        "identifier": "some-list"
    }) == list_count)
def test_list_source_with_id_selector_first():
    value_source_resolver = get_value_source_resolver(list_store=ListStore([{
        "name":
        "some-list",
        "items":
        get_list_items(3)
    }]), )

    assert (value_source_resolver.resolve({
        "source": "list",
        "identifier": "some-list",
        "id_selector": "first"
    }) == "item-1")
Example #11
0
def value_source_resolver(mock_schema, response_metadata):
    resolver = ValueSourceResolver(
        answer_store=AnswerStore(),
        list_store=ListStore(),
        metadata={},
        response_metadata=response_metadata,
        schema=mock_schema,
        location=None,
        list_item_id=None,
        routing_path_block_ids=None,
        use_default_answer=True,
    )

    return resolver
def test_list_source_id_selector_primary_person(primary_person_list_item_id):
    value_source_resolver = get_value_source_resolver(list_store=ListStore([{
        "name":
        "some-list",
        "primary_person":
        primary_person_list_item_id,
        "items":
        get_list_items(3),
    }]), )

    assert (value_source_resolver.resolve({
        "source": "list",
        "identifier": "some-list",
        "id_selector": "primary_person",
    }) == primary_person_list_item_id)
def test_list_source_with_id_selector_same_name_items():
    value_source_resolver = get_value_source_resolver(list_store=ListStore([{
        "name":
        "some-list",
        "items":
        get_list_items(5),
        "same_name_items":
        get_list_items(3),
    }]), )

    assert (value_source_resolver.resolve({
        "source": "list",
        "identifier": "some-list",
        "id_selector": "same_name_items",
    }) == get_list_items(3))
Example #14
0
def test_list_source(list_count, expected_result):
    rule_evaluator = get_rule_evaluator(list_store=ListStore([{
        "name":
        "some-list",
        "items":
        get_list_items(list_count)
    }]), )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "list",
                    "identifier": "some-list",
                    "selector": "count"
                },
                3,
            ]
        }) is expected_result)
Example #15
0
def test_list_source_with_id_selector_first(list_item_id, expected_result):
    rule_evaluator = get_rule_evaluator(list_store=ListStore([{
        "name":
        "some-list",
        "items":
        get_list_items(1)
    }]), )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "list",
                    "identifier": "some-list",
                    "selector": "first",
                },
                list_item_id,
            ]
        }) is expected_result)
def test_answer_source_outside_of_repeating_section():
    schema = get_mock_schema()

    schema.is_repeating_answer = Mock(return_value=False)
    answer_store = AnswerStore([{"answer_id": "some-answer", "value": "Yes"}])

    value_source_resolver = get_value_source_resolver(
        schema=schema,
        answer_store=answer_store,
        list_store=ListStore([{
            "name": "some-list",
            "items": get_list_items(3)
        }]),
        location=Location(section_id="some-section",
                          block_id="some-block",
                          list_item_id="item-1"),
    )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer"
    }) == "Yes")
def test_answer_source_with_list_item_selector_list_first_item():
    value_source_resolver = get_value_source_resolver(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "list_item_id": "item-1",
            "value": "Yes",
        }]),
        list_store=ListStore([{
            "name": "some-list",
            "items": get_list_items(3)
        }]),
    )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer",
        "list_item_selector": {
            "source": "list",
            "id": "some-list",
            "id_selector": "first",
        },
    }) == "Yes")
Example #18
0
def test_list_source_with_id_selector_same_name_items(list_item_id,
                                                      expected_result):
    rule_evaluator = get_rule_evaluator(list_store=ListStore([{
        "name":
        "some-list",
        "items":
        get_list_items(5),
        "same_name_items":
        get_list_items(3),
    }]), )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.IN: [
                list_item_id,
                {
                    "source": "list",
                    "identifier": "some-list",
                    "selector": "same_name_items",
                },
            ]
        }) is expected_result)