1+ # pydifact - a python edifact library
2+ # Copyright (C) 2021 Karl Southern
3+ #
4+ # This program is free software: you can redistribute it and/or modify
5+ # it under the terms of the GNU Lesser General Public License as published
6+ # by the Free Software Foundation, either version 3 of the License, or
7+ # (at your option) any later version.
8+ #
9+ # This program is distributed in the hope that it will be useful,
10+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ # GNU Lesser General Public License for more details.
13+ #
14+ # You should have received a copy of the GNU Lesser General Public License
15+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16+ import unittest
117from pydifact .segmentcollection import Interchange
2- from pydifact import Segment , mapping , serializer
18+ from pydifact import Segment , mapping
319
420
521class BGM (Segment ):
@@ -14,42 +30,6 @@ class LIN(Segment):
1430 tag = "LIN"
1531
1632
17- interchange = Interchange .from_str (
18- """UNA:+.?*'
19- UNB+UNOA:4+5021376940009:14+1111111111111:14+200421:1000+0001+ORDERS'
20- UNH+1+ORDERS:D:01B:UN:EAN010'
21- BGM+220+123456+9'
22- DTM+137:20150410:102'
23- DTM+2:20150710:102'
24- FTX+DIN+++DELIVERY INSTRUCTIONS DESCRIPTION'
25- NAD+BY+5021376940009::9'
26- NAD+SU+1111111111111::9'
27- RFF+IA:123456'
28- NAD+ST+++Mr John Smith+The Bungalow:Hastings Road+Preston+:::Lancashire+SW1A 1AA'
29- CTA+LB+:Mr John Smith'
30- COM+01772 999999:TE'
31- 32- CUX+2:GBP:9'
33- TDT+20+++++SD'
34- LIN+1++121354654:BP'
35- IMD+F++:::TPRG item description'
36- QTY+21:2'
37- MOA+203:200.00'
38- PRI+AAA:100.00'
39- RFF+LI:1'
40- LIN+1++121354654:BP'
41- IMD+F++:::TPRG item description'
42- QTY+21:2'
43- MOA+203:200.00'
44- PRI+AAA:100.00'
45- RFF+LI:1'
46- UNS+S'
47- CNT+2:1'
48- UNT+1+27'
49- UNZ+1+0001'"""
50- )
51-
52-
5333class OrderLine (mapping .SegmentGroup ):
5434 line_id = mapping .Segment ("LIN" , mandatory = True )
5535 description = mapping .Segment ("IMD" , mandatory = True )
@@ -81,37 +61,65 @@ class Order(mapping.SegmentGroup):
8161 cnt = mapping .Segment ("CNT" , mandatory = True )
8262
8363
84- TYPE_TO_PARSER_DICT = {"ORDERS" : Order }
85-
86- message = next (interchange .get_messages ())
64+ SAMPLE = """UNA:+.?*'
65+ UNB+UNOA:4+5021376940009:14+1111111111111:14+200421:1000+0001+ORDERS'
66+ UNH+1+ORDERS:D:01B:UN:EAN010'
67+ BGM+220+123456+9'
68+ DTM+137:20150410:102'
69+ DTM+2:20150710:102'
70+ FTX+DIN+++DELIVERY INSTRUCTIONS DESCRIPTION'
71+ NAD+BY+5021376940009::9'
72+ NAD+SU+1111111111111::9'
73+ RFF+IA:123456'
74+ NAD+ST+++Mr John Smith+The Bungalow:Hastings Road+Preston+:::Lancashire+SW1A 1AA'
75+ CTA+LB+:Mr John Smith'
76+ COM+01772 999999:TE'
77+ 78+ CUX+2:GBP:9'
79+ TDT+20+++++SD'
80+ LIN+1++121354654:BP'
81+ IMD+F++:::TPRG item description'
82+ QTY+21:2'
83+ MOA+203:200.00'
84+ PRI+AAA:100.00'
85+ RFF+LI:1'
86+ LIN+1++121354654:BP'
87+ IMD+F++:::TPRG item description'
88+ QTY+21:2'
89+ MOA+203:200.00'
90+ PRI+AAA:100.00'
91+ RFF+LI:1'
92+ UNS+S'
93+ CNT+2:1'
94+ UNT+1+27'
95+ UNZ+1+0001'"""
8796
88- cls = TYPE_TO_PARSER_DICT .get (message .type )
89- if not cls :
90- raise NotImplementedError ("Unsupported message type '{}'" .format (message .type ))
9197
92- obj = cls ()
93- obj .from_message (message )
98+ class MappingTest (unittest .TestCase ):
9499
95- reconstituted = obj .to_message (message .reference_number , message .identifier )
100+ def test_read_interchange (self ):
101+ interchange = Interchange .from_str (SAMPLE )
102+ message = next (interchange .get_messages ())
96103
97- # print(str(obj))
98- # print(obj.purchase_order_id[0])
104+ try :
105+ obj = Order ()
106+ obj .from_message (message )
107+ except Exception as err :
108+ raise AssertionError (
109+ "Could not read Message into Order mapping! {}" .format (
110+ repr (err )
111+ )
112+ )
99113
100- assert isinstance (obj .purchase_order_id .to_segments (), BGM )
114+ def test_ensure_mapped_bgm_segment (self ):
115+ interchange = Interchange .from_str (SAMPLE )
116+ message = next (interchange .get_messages ())
101117
102- # print(message.segments)
118+ obj = Order ()
119+ obj .from_message (message )
103120
104- assert str (message ) == str (
105- reconstituted
106- ), "Original message should match reconstituted message"
121+ self .assertTrue (isinstance (obj .purchase_order_id .to_segments (), BGM ))
107122
108- reconstituted_interchange = Interchange (
109- interchange .sender , interchange .recipient ,
110- interchange .control_reference ,
111- interchange .syntax_identifier ,
112- interchange .delimiters ,
113- interchange .timestamp
114- )
115- reconstituted_interchange .add_message (reconstituted )
116123
117- assert reconstituted_interchange .serialize () == interchange .serialize (), "Original interchange should match reconstituted interchange"
124+ if __name__ == "__main__" :
125+ unittest .main ()
0 commit comments