def test_add_relationship_that_already_exists():
    relationship = relationships[0]
    relationship_store = RelationshipStore([relationship])
    relationship_store.add_or_update(Relationship(**relationship))

    assert len(relationship_store) == 1
    assert not relationship_store.is_dirty
コード例 #2
0
 def relationship_store(self):
     answer = self._questionnaire_store.answer_store.get_answer(
         self.relationships_answer_id
     )
     if answer:
         return RelationshipStore(answer.value)
     return RelationshipStore()
def test_remove_id_in_single_relationship():
    relationship_store = RelationshipStore(relationships)
    relationship_store.remove_all_relationships_for_list_item_id("789101")
    remaining_relationship = Relationship(**relationships[1])

    assert len(relationship_store) == 1
    assert (relationship_store.get_relationship(
        remaining_relationship.list_item_id,
        remaining_relationship.to_list_item_id) == remaining_relationship)
    assert relationship_store.is_dirty
def test_add_relationship():
    relationship = Relationship(**relationships[0])

    relationship_store = RelationshipStore()
    relationship_store.add_or_update(relationship)

    assert (relationship_store.get_relationship(
        relationship.list_item_id,
        relationship.to_list_item_id) == relationship)
    assert len(relationship_store) == 1
    assert relationship_store.is_dirty
def test_update_existing_relationship():
    relationship_store = RelationshipStore(relationships)

    relationship = Relationship(**relationships[0])
    relationship.relationship = "test"

    relationship_store.add_or_update(relationship)

    assert len(relationship_store) == 2
    updated_relationship = relationship_store.get_relationship(
        relationship.list_item_id, relationship.to_list_item_id)
    assert updated_relationship.relationship == "test"
    assert relationship_store.is_dirty
コード例 #6
0
 def _get_answers_from_answer_store(self, answer_ids):
     """
     Maps the answers in an answer store to a dictionary of key, value answers.
     """
     answer = self._questionnaire_store.answer_store.get_answer(
         answer_ids[0])
     if answer:
         relationship_store = RelationshipStore(answer.value)
         relationship = relationship_store.get_relationship(
             self._current_location.list_item_id,
             self._current_location.to_list_item_id,
         )
         if relationship:
             return {answer.answer_id: relationship.relationship}
     return {}
コード例 #7
0
def add_relationships_unrelated_answers(answer_store, list_store, schema,
                                        section_id, relationships_block,
                                        answers_payload):
    relationships_answer_id = schema.get_first_answer_id_for_block(
        relationships_block["id"])
    relationships_answer = answer_store.get_answer(relationships_answer_id)
    if not relationships_answer:
        return None

    relationship_store = RelationshipStore(relationships_answer.value)
    list_name = relationships_block["for_list"]
    unrelated_block = relationships_block["unrelated_block"]
    unrelated_block_id = unrelated_block["id"]
    unrelated_answer_id = schema.get_first_answer_id_for_block(
        unrelated_block_id)
    unrelated_no_answer_values = schema.get_unrelated_block_no_answer_values(
        unrelated_answer_id)

    relationship_router = RelationshipRouter(
        answer_store=answer_store,
        relationship_store=relationship_store,
        section_id=section_id,
        list_name=list_name,
        list_item_ids=list_store[list_name],
        relationships_block_id=relationships_block["id"],
        unrelated_block_id=unrelated_block_id,
        unrelated_answer_id=unrelated_answer_id,
        unrelated_no_answer_values=unrelated_no_answer_values,
    )

    for location in relationship_router.path:
        if location.block_id == unrelated_block_id and (
                unrelated_answer := answer_store.get_answer(
                    unrelated_answer_id, list_item_id=location.list_item_id)):
            answers_payload.add_or_update(unrelated_answer)
コード例 #8
0
def relationship_router(answers=None, relationships=None):
    return RelationshipRouter(
        answer_store=AnswerStore(answers),
        relationship_store=RelationshipStore(relationships),
        section_id="relationships-section",
        list_name="people",
        list_item_ids=["abc123", "def123", "ghi123", "jkl123"],
        relationships_block_id="relationships",
    )
コード例 #9
0
def relationship_router(answers=None, relationships=None):
    return RelationshipRouter(
        answer_store=AnswerStore(answers),
        relationship_store=RelationshipStore(relationships),
        section_id="relationships-section",
        list_name="people",
        list_item_ids=[
            "abc123", "def123", "ghi123", "jkl123", "mno123", "pqr123"
        ],
        relationships_block_id="relationships",
        unrelated_block_id="related-to-anyone-else",
        unrelated_answer_id="anyone-else-answer",
        unrelated_no_answer_values=["No", "No, they are not related"],
    )
コード例 #10
0
    def _create_relationship_store_and_update_answer(self,
                                                     relationship_answer_id,
                                                     answer, form_data,
                                                     list_item_id,
                                                     to_list_item_id):
        try:
            relationship_store = RelationshipStore(answer.value)
        except AttributeError:
            relationship_store = RelationshipStore()

        relationship_answer = form_data.get(relationship_answer_id)
        relationship = Relationship(list_item_id, to_list_item_id,
                                    relationship_answer)
        relationship_store.add_or_update(relationship)
        self._answer_store.add_or_update(
            Answer(relationship_answer_id, relationship_store.serialize()))
コード例 #11
0
def add_relationships_unrelated_answers(
    answer_store: AnswerStore,
    list_store: ListStore,
    schema: QuestionnaireSchema,
    section_id: str,
    relationships_block: Mapping,
    answers_payload: AnswerStore,
) -> Optional[RelationshipStore]:
    relationships_answer_id = schema.get_first_answer_id_for_block(
        relationships_block["id"])
    relationships_answer = answer_store.get_answer(relationships_answer_id)
    if not relationships_answer:
        return None

    relationship_store = RelationshipStore(
        relationships_answer.value)  # type: ignore
    list_name = relationships_block["for_list"]
    unrelated_block = relationships_block["unrelated_block"]
    unrelated_block_id = unrelated_block["id"]
    unrelated_answer_id = schema.get_first_answer_id_for_block(
        unrelated_block_id)
    unrelated_no_answer_values = schema.get_unrelated_block_no_answer_values(
        unrelated_answer_id)

    relationship_router = RelationshipRouter(
        answer_store=answer_store,
        relationship_store=relationship_store,
        section_id=section_id,
        list_name=list_name,
        list_item_ids=list_store[list_name].items,
        relationships_block_id=relationships_block["id"],
        unrelated_block_id=unrelated_block_id,
        unrelated_answer_id=unrelated_answer_id,
        unrelated_no_answer_values=unrelated_no_answer_values,
    )

    for location in relationship_router.path:
        if location.block_id == unrelated_block_id and (
                unrelated_answer := answer_store.get_answer(
                    unrelated_answer_id, list_item_id=location.list_item_id)):
            answers_payload.add_or_update(unrelated_answer)
def test_clear():  # pylint: disable=redefined-outer-name
    relationship_store = RelationshipStore(relationships)
    relationship_store.clear()

    assert not relationship_store
    assert relationship_store.is_dirty
def test_remove_id_in_multiple_relationships():
    relationship_store = RelationshipStore(relationships)
    relationship_store.remove_all_relationships_for_list_item_id("123456")

    assert not relationship_store
    assert relationship_store.is_dirty
def test_remove_relationship_that_doesnt_exist():
    relationship_store = RelationshipStore(relationships)
    relationship_store.remove_relationship(list_item_id="123456",
                                           to_list_item_id="yyyyyy")
    assert not relationship_store.is_dirty
    assert len(relationship_store) == 2
def test_remove_relationship():
    relationship_store = RelationshipStore(relationships)
    relationship_store.remove_relationship(list_item_id="123456",
                                           to_list_item_id="789101")
    assert relationship_store.is_dirty
    assert len(relationship_store) == 1
def test_get_relationship_that_doesnt_exist():
    relationship_store = RelationshipStore(relationships)
    relationship = relationship_store.get_relationship(
        list_item_id="123456", to_list_item_id="yyyyyy")
    assert not relationship
def test_get_relationship():
    relationship_store = RelationshipStore(relationships)
    relationship = relationship_store.get_relationship(
        list_item_id="123456", to_list_item_id="789101")
    assert relationship
def test_serialisation():
    relationship_store = RelationshipStore(relationships)

    assert relationship_store.serialize() == relationships
def test_deserialisation():
    relationship_store = RelationshipStore(relationships)

    assert Relationship(**relationships[0]) in relationship_store
    assert len(relationship_store) == 2