Beispiel #1
0
 def toEdi(self):
     assert (self.tag is not None)
     message = PMessage()
     tag, elements = self.tag, self.toList()
     segment = PSegment(tag, None)
     if elements is not None and len(elements) > 0:
         segment = PSegment(tag, *elements)
     message.add_segment(segment)
     return message.serialize()
    def test_edifact_parser2(self):
        tags = []
        elements = []

        for edifact in self.edifacts2.split("\n"):
            collection = Message.from_str(edifact)

            for segment in collection.segments:
                tags.append(segment.tag.strip())
                elements.append(segment.elements)
                # print('Segment tag: {}, content: {}'.format(segment.tag, segment.elements))

        self.assertEqual(tags[0], "UNA")
        self.assertEqual(elements[0], [":+.? '"])

        self.assertEqual(tags[1], "UNB")
        self.assertEqual(elements[1], [['UNOA', '4'], 'APIS*ABE', 'USADHS', ['070429', '0900'], '000000001', '', 'USADHS'])

        self.assertEqual(tags[2], "UNG")
        self.assertEqual(elements[2], ['PAXLST', 'XYZ AIRLINES', 'USADHS', ['070429', '0900'], '100', 'UN', ['D', '05B']])

        self.assertEqual(tags[3], "UNH")
        self.assertEqual(elements[3], ['PAX001', ['PAXLST', 'D', '05B', 'UN', 'IATA'], 'API01', '01'])

        self.assertEqual(tags[4], "BGM")
        self.assertEqual(elements[4], ['745'])

        self.assertEqual(tags[5], "RFF")
        self.assertEqual(elements[5], [['TN', 'BA123456789', '', '', '1']])
Beispiel #3
0
    def test_get_segments(self):

        message = Message.from_segments(
            [Segment("36CF", 1), Segment("CPD"), Segment("36CF", 2)]
        )
        segments = list(message.get_segments("36CF"))
        self.assertEqual([Segment("36CF", 1), Segment("36CF", 2)], segments)
    def test_single_text_with_one_leg(self):
        apis = self.header + """TDT+20+UA123+++UA'
            LOC+125+YVR'
            DTM+189:0704291230:201'
            LOC+87+JFK'
            DTM+232:0704291600:201'""" + self.trailer

        flights = []

        for api in apis.split("\n"):
            collection = Message.from_str(api)

            for segment in collection.segments:
                if segment.tag.strip() == "LOC":

                    vo = ApisMessageVo(segment.elements)

                    if vo.get_flights().get_origin() is None:
                        pass
                    else:
                        flights.append(vo.get_flights().get_origin())

                    if vo.get_flights().get_destination() is None:
                        pass
                    else:
                        flights.append(vo.get_flights().get_destination())

        flight = FlightVo(flights)

        self.assertEqual("YVR", flight.get_origin())
        self.assertEqual("JFK", flight.get_destination())
Beispiel #5
0
 def parse_edi(self, payload=None):
     payload = self.payload if payload is None else payload
     message = PMessage.from_str(payload)
     segments = Group(self.format).structure(
         *map(self.load_segment, message.segments)
     )
     return segments
    def test_edifact_parser(self):
        tags = []
        elements = []

        for edifact in self.edifacts1.split("\n"):
            collection = Message.from_str(edifact)

            for segment in collection.segments:
                tags.append(segment.tag.strip())
                elements.append(segment.elements)
                # print('Segment tag: {}, content: {}'.format(segment.tag, segment.elements))

        self.assertEqual(tags[0], "UNB")
        self.assertEqual(elements[0], [['UNOA', '4'], ['SAMPLE CARRIER NAME', 'ZZ'], ['HDQCH2X', 'ZZ'], ['200506', '1700'], '123456789', '', 'PAXLST'])

        self.assertEqual(tags[1], "UNH")
        self.assertEqual(elements[1], ['53371718146010', ['PAXLST', 'D', '02B', 'UN', 'IATA']])

        self.assertEqual(tags[2], "BGM")
        self.assertEqual(elements[2], ['745'])

        self.assertEqual(tags[3], "NAD")
        self.assertEqual(elements[3], ['MS', '', '', 'WORLD CUSTOMS ORGANIZATION BRU'])

        self.assertEqual(tags[4], "COM")
        self.assertEqual(elements[4], [['17037919383', 'TE']])

        self.assertEqual(tags[5], "TDT")
        self.assertEqual(elements[5], ['20', 'YY123'])
Beispiel #7
0
    def test_get_segment(self):

        message = Message.from_segments([
            Segment("36CF", 1),
            Segment("36CF", 2),
        ])
        segment = message.get_segment("36CF")
        self.assertEqual(Segment("36CF", 1), segment)
Beispiel #8
0
    def _test_file_read(self, file_name: str, encoding: str = 'iso8859-1'):

        # read in a complete message from a file
        message = Message.from_file(file_name)
        output = message.serialize()
        with open(file_name, encoding) as file:
            expected = file.read()  # .replace("\n", "")
            self.assertEqual(expected, output)
Beispiel #9
0
def test_wikipedia_file():
    message = Message.from_file("{}/wikipedia.edi".format(path))
    # make some checks
    assert message.get_segment("UNB") == Segment("UNB", ["IATB", "1"], "6XPPC",
                                                 "LHPPC", ["940101", "0950"],
                                                 "1")
    assert message.get_segment("IFT") == Segment("IFT", "3",
                                                 "XYZCOMPANY AVAILABILITY")
    assert message.get_segment("TVL") == Segment(
        "TVL", ["240493", "1000", "", "1220"], "FRA", "JFK", "DL", "400", "C")
Beispiel #10
0
    def get_parsed_message(self, filename):
        parsed_message = []
        messages = self.message_input(filename)
        for message in messages:
            collection = Message.from_str(message)

            for segment in collection.segments:
                elements = []
                temp = {}
                for element in segment.elements:
                    elements.append(element)

                temp["tag"] = segment.tag
                temp["elements"] = elements

                parsed_message.append(temp)
        return parsed_message
Beispiel #11
0
 def test_huge_message(self):
     """tests parsing a huge message"""
     message = Message.from_file('data/huge_file2.edi')
Beispiel #12
0
    def test_get_segment_doesnt_exist(self):

        message = Message()
        segment = message.get_segment("36CF")
        self.assertIsNone(segment)
Beispiel #13
0
    def test_get_segments_doesnt_exist(self):

        message = Message()
        segments = list(message.get_segments("36CF"))
        self.assertEqual([], segments)
Beispiel #14
0
 def test_serialize(self):
     message = Message.from_segments(
         [Segment("36CF", "1"), Segment("36CF", "2")])
     string = str(message)
     self.assertEqual("36CF+1'36CF+2'", string)
Beispiel #15
0
def test_empty_segment_list():
    m = Message()
    assert m.serialize() == ""
Beispiel #16
0
def test_empty_segment():
    m = Message()
    with pytest.raises(ValueError):
        m.add_segment(Segment("", []))
Beispiel #17
0
def test_UNA_integrity2():
    m = Message()
    initstring = ":+.? '"
    m.add_segment(Segment("UNA", initstring))
    assert m.serialize() == "UNA" + initstring
Beispiel #18
0
    def test_create_with_segments(self):

        message = Message.from_segments([Segment("36CF")])
        self.assertEqual([
            Segment("36CF"),
        ], message.segments)
Beispiel #19
0
    def test_from_file(self):

        with self.assertRaises(FileNotFoundError):
            Message.from_file("/no/such/file")
Beispiel #20
0
from pydifact.message import Message
message = Message.from_file("../input/20200506 0432_API_50157_YY123_IAD_BRU_20200506 1700.txt")

for segment in message.segments:
    print('Segment tag: {}, content: {}'.format(
        segment.tag, segment.elements))