-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_serializer.py
More file actions
152 lines (108 loc) · 4.43 KB
/
test_serializer.py
File metadata and controls
152 lines (108 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# pydifact - a python edifact library
# Copyright (C) 2017-2024 Christian González
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import copy
import datetime
import pytest
from pydifact.segmentcollection import Interchange, RawSegmentCollection
from pydifact.segments import Segment
from pydifact.serializer import Serializer
@pytest.fixture
def serializer():
return Serializer()
@pytest.fixture
def interchange():
return Interchange(
sender="1234",
recipient="3333",
timestamp=datetime.datetime(2020, 1, 2, 22, 12),
control_reference="42",
syntax_identifier=("UNOC", 1),
)
def assert_segments(serializer, expected: str, segments: list):
"""Helper function add default UNA header and terminator, and compare string to segment"""
expected = "UNA:+,? '" + expected + "'"
collection = serializer.serialize(segments, with_una_header=True)
assert expected == collection
def test_una_breakline(interchange):
initstring = ":+,? '"
interchange.add_segment(Segment("UNA", initstring))
assert interchange.serialize(break_lines=True).startswith("UNA" + initstring + "\n")
def test_una_integrity1(interchange):
initstring = ":+,? '"
interchange.add_segment(Segment("UNA", initstring))
assert interchange.serialize().startswith("UNA" + initstring)
def test_UNA_integrity2(interchange):
initstring = ":+.? '"
interchange.add_segment(Segment("UNA", initstring))
assert interchange.serialize().startswith("UNA" + initstring)
def test_empty_segment_list():
m = RawSegmentCollection()
assert m.serialize() == ""
def test_basic1(serializer):
assert_segments(serializer, "RFF+PD:50515", [Segment("RFF", ["PD", "50515"])])
def test_basic2(serializer):
assert_segments(serializer, "RFF+PD+50515", [Segment("RFF", "PD", "50515")])
def test_empty_segment(serializer):
assert_segments(serializer, "RFF+PD++50515", [Segment("RFF", "PD", None, "50515")])
def test_with_una_in_segments(serializer):
assert_segments(
serializer,
"RFF+PD+45761",
[Segment("UNA", ":+,? '"), Segment("RFF", "PD", "45761")],
)
def test_escape_character(serializer):
assert_segments(
serializer,
"ERC+10:The message does not make sense??",
[Segment("ERC", ["10", "The message does not make sense?"])],
)
def test_escape_component_separator(serializer):
assert_segments(
serializer, "ERC+10:Name?: Craig", [Segment("ERC", ["10", "Name: Craig"])]
)
def test_escape_data_separator(serializer):
assert_segments(
serializer, "DTM+735:?+0000:406", [Segment("DTM", ["735", "+0000", "406"])]
)
def test_escape_decimal_point(serializer):
assert_segments(serializer, "QTY+136:12,235", [Segment("QTY", ["136", "12,235"])])
def test_escape_segment_terminator(serializer):
assert_segments(serializer, "ERC+10:Craig?'s", [Segment("ERC", ["10", "Craig's"])])
def test_escape_sequence(serializer):
assert_segments(
serializer,
"ERC+10:?:?+???' - ?:?+???' - ?:?+???'",
[Segment("ERC", ["10", ":+?' - :+?' - :+?'"])],
)
def test_no_mutation(serializer):
segments1 = [Segment("ERC", [":+?'"])]
segments2 = copy.deepcopy(segments1)
serializer.serialize(segments1, with_una_header=True)
assert segments1 == segments2
@pytest.fixture
def interchange_str():
return (
"UNB+UNOC:1+1234+3333+200102:2212+42'"
"UNH+42z42+PAORES:93:1:IA'"
"UNT+2+42z42'"
"UNZ+1+42'"
)
def test_interchange_serialization_with_una(interchange_str):
i = Interchange.from_str("UNA:+.? '" + interchange_str)
assert i.serialize() == "UNA:+.? '" + interchange_str
def test_interchange_serialization_with_out_una(interchange_str):
i = Interchange.from_str(interchange_str)
assert i.serialize() == interchange_str