Ejemplo n.º 1
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,
    )
Ejemplo n.º 2
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
Ejemplo n.º 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)
Ejemplo n.º 4
0
def test_answer_source_count(rule, expected_result):
    rule_evaluator = get_rule_evaluator(answer_store=AnswerStore([{
        "answer_id":
        "some-answer",
        "value": ["array element 1", "array element 2"],
    }]), )
    assert rule_evaluator.evaluate(rule=rule) is expected_result
Ejemplo n.º 5
0
def test_map_with_nested_date_operator(offset, expected_result):
    rule = {
        Operator.MAP: [
            {
                Operator.FORMAT_DATE: [
                    {
                        Operator.DATE: [
                            "self",
                            offset,
                        ]
                    },
                    "d MMMM yyyy",
                ]
            },
            {
                "source": "answers",
                "identifier": "checkbox-answer"
            },
        ]
    }

    rule_evaluator = get_rule_evaluator(answer_store=AnswerStore([{
        "answer_id":
        "checkbox-answer",
        "value": ["2021-01-01", "2021-01-02", "2021-01-03"],
    }]))

    assert rule_evaluator.evaluate(rule=rule) == expected_result
Ejemplo n.º 6
0
def test_answer_source_with_list_item_selector_location(
        answer_value, expected_result):
    rule_evaluator = get_rule_evaluator(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "list_item_id": "item-1",
            "value": answer_value,
        }]),
        location=Location(section_id="some-section",
                          block_id="some-block",
                          list_item_id="item-1"),
    )

    assert (rule_evaluator.evaluate(rule={
        Operator.EQUAL: [
            {
                "source": "answers",
                "identifier": "some-answer",
                "list_item_selector": {
                    "source": "location",
                    "identifier": "list_item_id",
                },
            },
            3,
        ]
    }, ) is expected_result)
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)
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,
    )
Ejemplo n.º 9
0
def test_answer_source_not_on_path_non_repeating_section(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": "block-on-path"})
        answer_id = "answer-on-path"
        expected_result = True
    else:
        schema.get_block_for_answer_id = Mock(
            return_value={"id": "block-not-on-path"})
        answer_id = "answer-not-on-path"
        expected_result = False

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

    rule_evaluator = get_rule_evaluator(
        schema=schema,
        answer_store=AnswerStore([answer.to_dict()]),
        location=location,
        routing_path_block_ids=["block-on-path"],
    )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                "Yes",
                {
                    "source": "answers",
                    "identifier": "answer-on-path"
                },
            ]
        }) == expected_result)
Ejemplo n.º 10
0
def test_format_date(rule, expected_result):
    rule_evaluator = get_rule_evaluator(answer_store=AnswerStore([{
        "answer_id":
        "some-answer",
        "value":
        "2021-01-01",
    }]), )
    assert rule_evaluator.evaluate(rule=rule) == expected_result
Ejemplo n.º 11
0
def test_date_value(rule, expected_result):
    rule_evaluator = get_rule_evaluator(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "value": current_date_as_yyyy_mm_dd,
        }]),
        metadata={"some-metadata": current_date_as_yyyy_mm_dd},
    )

    assert (rule_evaluator.evaluate(rule=rule, ) is expected_result)
Ejemplo n.º 12
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_answer_source():
    value_source_resolver = get_value_source_resolver(answer_store=AnswerStore(
        [{
            "answer_id": "some-answer",
            "value": "Yes"
        }]), )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer"
    }) == "Yes")
def test_answer_value_can_be_escaped(answer_value, escaped_value):
    value_source_resolver = get_value_source_resolver(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "value": answer_value,
        }]),
        escape_answer_values=True,
    )
    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer"
    }) == escaped_value)
def test_answer_source_with_list_item_id_no_list_item_selector():
    value_source_resolver = get_value_source_resolver(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "list_item_id": "item-1",
            "value": "Yes"
        }]),
        list_item_id="item-1",
    )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer"
    }) == "Yes")
Ejemplo n.º 16
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_answer_source_with_dict_answer_selector():
    value_source_resolver = get_value_source_resolver(answer_store=AnswerStore(
        [{
            "answer_id": "some-answer",
            "value": {
                "years": 1,
                "months": 10
            },
        }]), )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer",
        "selector": "years",
    }) == 1)
Ejemplo n.º 18
0
def test_answer_source(answer_value, expected_result):
    rule_evaluator = get_rule_evaluator(answer_store=AnswerStore([{
        "answer_id":
        "some-answer",
        "value":
        answer_value
    }]), )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [{
                "source": "answers",
                "identifier": "some-answer"
            }, 3]
        }) is expected_result)
def test_answer_value_with_selector_can_be_escaped():
    value_source_resolver = get_value_source_resolver(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "value": {
                "key_1": HTML_CONTENT,
                "key_2": 1
            },
        }]),
        escape_answer_values=True,
    )
    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer",
        "selector": "key_1"
    }) == ESCAPED_CONTENT)
def test_list_item_id_ignored_if_answer_not_in_list_collector_or_repeat():
    schema = get_mock_schema()
    schema.is_repeating_answer = Mock(return_value=False)

    value_source_resolver = get_value_source_resolver(
        schema=schema,
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "value": "Yes"
        }]),
        list_item_id="item-1",
    )

    assert (value_source_resolver.resolve({
        "source": "answers",
        "identifier": "some-answer"
    }) == "Yes")
def test_answer_source_with_list_item_selector_location_none():
    value_source_resolver = get_value_source_resolver(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "list_item_id": "item-1",
            "value": "Yes",
        }]),
        location=None,
    )
    with pytest.raises(InvalidLocationException):
        value_source_resolver.resolve({
            "source": "answers",
            "identifier": "some-answer",
            "list_item_selector": {
                "source": "location",
                "id": "list_item_id"
            },
        })
def test_answer_source_with_list_item_selector_location():
    value_source_resolver = get_value_source_resolver(
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "list_item_id": "item-1",
            "value": "Yes",
        }]),
        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",
        "list_item_selector": {
            "source": "location",
            "id": "list_item_id"
        },
    }) == "Yes")
Ejemplo n.º 23
0
def test_answer_source_with_dict_answer_selector(answer_value,
                                                 expected_result):
    rule_evaluator = get_rule_evaluator(answer_store=AnswerStore([{
        "answer_id": "some-answer",
        "value": {
            "years": answer_value,
            "months": 10
        },
    }]), )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "answers",
                    "identifier": "some-answer",
                    "selector": "years",
                },
                3,
            ]
        }) 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")
Ejemplo n.º 26
0
def test_answer_source_default_answer_used_when_no_answer(
        comparison_value, expected_result):
    schema = get_mock_schema()
    schema.get_default_answer = Mock(
        return_value=Answer(answer_id="answer-that-does-not-exist", value=3))

    rule_evaluator = get_rule_evaluator(
        schema=schema,
        answer_store=AnswerStore([{
            "answer_id": "some-answer",
            "value": "No"
        }]),
    )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "answers",
                    "identifier": "answer-that-does-not-exist"
                },
                comparison_value,
            ]
        }) is expected_result)
Ejemplo n.º 27
0
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"}])

    rule_evaluator = get_rule_evaluator(
        schema=schema,
        answer_store=answer_store,
        location=Location(section_id="some-section",
                          block_id="some-block",
                          list_item_id="item-1"),
    )

    assert (rule_evaluator.evaluate(
        rule={
            Operator.EQUAL: [
                {
                    "source": "answers",
                    "identifier": "some-answer"
                },
                "Yes",
            ]
        }) is True)