def deserialise_message_beginning(
        original_dict: EdifactDict,
        index: int) -> MessageSegmentBeginningDetails:
    msg_bgn_lines = EdifactDict(
        helpers.extract_relevant_lines(original_dict, index,
                                       [MESSAGE_REGISTRATION_KEY]))
    msg_bgn_details = creators.create_message_segment_beginning(msg_bgn_lines)
    return msg_bgn_details
def deserialise_interchange_header(original_dict: EdifactDict,
                                   index: int) -> InterchangeHeader:
    interchange_header_line = EdifactDict(
        helpers.extract_relevant_lines(original_dict, index,
                                       [MESSAGE_HEADER_KEY]))
    interchange_header = creators.create_interchange_header(
        interchange_header_line)
    return interchange_header
def deserialise_registration(
        transaction_lines: EdifactDict) -> TransactionRegistrationDetails:
    registration_lines = EdifactDict(
        helpers.extract_relevant_lines(transaction_lines, 0, [
            MESSAGE_REGISTRATION_KEY, MESSAGE_PATIENT_KEY, MESSAGE_TRAILER_KEY
        ]))
    transaction_reg = creators.create_transaction_registration(
        registration_lines)
    return transaction_reg
def deserialise_patient_if_applicable(
        transaction_lines: EdifactDict) -> TransactionPatientDetails:
    transaction_pat = None
    patient_segment_index = find_index_of_patient_segment(transaction_lines)
    if patient_segment_index != -1:
        patient_lines = EdifactDict(
            helpers.extract_relevant_lines(
                transaction_lines, patient_segment_index,
                [MESSAGE_REGISTRATION_KEY, MESSAGE_TRAILER_KEY]))
        transaction_pat = creators.create_transaction_patient(patient_lines)

    return transaction_pat
def get_transaction_lines(original_dict: EdifactDict,
                          index: int) -> EdifactDict:
    """
    From the original dict provided get the lines that represent a transaction within a message.
    This can be when another MESSAGE_REGISTRATION_KEY is found representing a new transaction or
    when the MESSAGE_TRAILER_KEY is found.
    In order to skip the first SO1 in the original_dict provided here the index is started at +1
    """
    transaction_lines = EdifactDict(
        helpers.extract_relevant_lines(
            original_dict, index + 1,
            [MESSAGE_REGISTRATION_KEY, MESSAGE_TRAILER_KEY]))
    return transaction_lines
    def test_extract_relevant_lines(self):
        with self.subTest(
                "When the original dict is just 1 line and terminating keys not found "
                "return the original dict"):
            expected = EdifactDict([("AAA", "VALUE FOR AAA")])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=0,
                                                    terminator_keys=["BBB"])

            compare(result, expected)

        with self.subTest("When the terminating key is found on the first line"
                          "return an empty Edifact dict"):
            expected = EdifactDict([])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA"),
                                         ("BBB", "VALUE FOR BBB")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=0,
                                                    terminator_keys=["AAA"])

            compare(result, expected)

        with self.subTest(
                "When the original dict is multiple lines and terminating keys not found "
                "return the original dict"):
            expected = EdifactDict([("AAA", "VALUE FOR AAA"),
                                    ("BBB", "VALUE FOR BBB")])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA"),
                                         ("BBB", "VALUE FOR BBB")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=0,
                                                    terminator_keys=["CCC"])

            compare(result, expected)

        with self.subTest(
                "When the starting pos is 0 and terminating key is found "
                "return a new dict from the first line to the terminating key is found"
        ):
            expected = EdifactDict([("AAA", "VALUE FOR AAA"),
                                    ("BBB", "VALUE FOR BBB")])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA"),
                                         ("BBB", "VALUE FOR BBB"),
                                         ("CCC", "VALUE FOR CCC")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=0,
                                                    terminator_keys=["CCC"])

            compare(result, expected)

        with self.subTest(
                "When the starting pos is 1 and terminating key is found "
                "return a new dict from the second line to the terminating key is found"
        ):
            expected = EdifactDict([("BBB", "VALUE FOR BBB")])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA"),
                                         ("BBB", "VALUE FOR BBB"),
                                         ("CCC", "VALUE FOR CCC")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=1,
                                                    terminator_keys=["CCC"])

            compare(result, expected)

        with self.subTest(
                "When the starting pos is 1 and terminating key is not found "
                "return a new dict from the second line to the end of the original dict"
        ):
            expected = EdifactDict([("BBB", "VALUE FOR BBB"),
                                    ("CCC", "VALUE FOR CCC")])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA"),
                                         ("BBB", "VALUE FOR BBB"),
                                         ("CCC", "VALUE FOR CCC")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=1,
                                                    terminator_keys=["DDD"])

            compare(result, expected)

        with self.subTest(
                "The terminator key exists on the first line but the starting pos is 1 "
                "return a new dict from the second line to the terminating key is found"
        ):
            expected = EdifactDict([("BBB", "VALUE FOR BBB")])

            original_dict = EdifactDict([("CCC", "VALUE FOR CCC"),
                                         ("BBB", "VALUE FOR BBB"),
                                         ("CCC", "VALUE FOR CCC")])
            result = helpers.extract_relevant_lines(original_dict,
                                                    starting_pos=1,
                                                    terminator_keys=["CCC"])

            compare(result, expected)

        with self.subTest(
                "When more than one terminator key is provided "
                "return a new dict from the starting pos to one of the terminating key is found"
        ):
            expected = EdifactDict([("AAA", "VALUE FOR AAA"),
                                    ("BBB", "VALUE FOR BBB")])

            original_dict = EdifactDict([("AAA", "VALUE FOR AAA"),
                                         ("BBB", "VALUE FOR BBB"),
                                         ("CCC", "VALUE FOR CCC")])
            result = helpers.extract_relevant_lines(
                original_dict, starting_pos=0, terminator_keys=["CCC", "DDD"])

            compare(result, expected)