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, )
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
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 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)
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, )
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_current_location_source(list_item_id, expected_result): rule_evaluator = get_rule_evaluator(location=Location( section_id="some-section", block_id="some-block", list_item_id=list_item_id), ) assert (rule_evaluator.evaluate( rule={ Operator.EQUAL: [ { "source": "location", "identifier": "list_item_id" }, "item-1", ] }) is expected_result)
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")
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_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)