Debian Codegraph @

swift/mt940_test.go

raw
1 package swift
2
3 import (
4 "reflect"
5 "strconv"
6 "strings"
7 "testing"
8 "time"
9
10 "github.com/kr/pretty"
11 "github.com/mitch000001/go-hbci/domain"
12 )
13
14 func TestAccountTagUnmarshal(t *testing.T) {
15 // normal german BankID
16 test := ":25:12345678/100000000"
17
18 expected := &AccountTag{
19 Tag: ":25:",
20 BankID: "12345678",
21 AccountID: "100000000",
22 }
23
24 tag := &AccountTag{}
25
26 err := tag.Unmarshal([]byte(test))
27
28 if err != nil {
29 t.Logf("Expected no error, got %T:%v\n", err, err)
30 t.Fail()
31 }
32
33 if !reflect.DeepEqual(expected, tag) {
34 t.Logf("Expected tag to equal\n%#v\n\tgot\n%#v\n", expected, tag)
35 t.Fail()
36 }
37 }
38
39 func TestTransactionTagUnmarshal(t *testing.T) {
40 tests := []struct {
41 description string
42 marshaledValue string
43 expectedTag *TransactionTag
44 }{
45 {
46 "All attributes set, booking in next month",
47 ":61:1511301202DR4,52N024NONREF//ABC\r\n/DEF",
48 &TransactionTag{
49 Tag: ":61:",
50 ValutaDate: domain.ShortDate{Time: domain.Date(2015, time.November, 30, time.Local).Truncate(24 * time.Hour)},
51 BookingDate: domain.ShortDate{Time: domain.Date(2015, time.December, 2, time.Local).Truncate(24 * time.Hour)},
52 DebitCreditIndicator: "D",
53 CurrencyKind: "R",
54 Amount: 4.52,
55 BookingKey: "024",
56 Reference: "NONREF",
57 BankReference: "ABC",
58 AdditionalInformation: "DEF",
59 },
60 },
61 {
62 "All attributes set, booking in new year",
63 ":61:1512300102DR4,52N024NONREF//ABC\r\n/DEF",
64 &TransactionTag{
65 Tag: ":61:",
66 ValutaDate: domain.ShortDate{Time: domain.Date(2015, time.December, 30, time.Local).Truncate(24 * time.Hour)},
67 BookingDate: domain.ShortDate{Time: domain.Date(2016, time.January, 2, time.Local).Truncate(24 * time.Hour)},
68 DebitCreditIndicator: "D",
69 CurrencyKind: "R",
70 Amount: 4.52,
71 BookingKey: "024",
72 Reference: "NONREF",
73 BankReference: "ABC",
74 AdditionalInformation: "DEF",
75 },
76 },
77 {
78 "All attributes set",
79 ":61:1508010803DR4,52N024NONREF//ABC\r\n/DEF",
80 &TransactionTag{
81 Tag: ":61:",
82 ValutaDate: domain.ShortDate{Time: domain.Date(2015, 8, 1, time.Local).Truncate(24 * time.Hour)},
83 BookingDate: domain.ShortDate{Time: domain.Date(2015, 8, 3, time.Local).Truncate(24 * time.Hour)},
84 DebitCreditIndicator: "D",
85 CurrencyKind: "R",
86 Amount: 4.52,
87 BookingKey: "024",
88 Reference: "NONREF",
89 BankReference: "ABC",
90 AdditionalInformation: "DEF",
91 },
92 },
93 {
94 "All attributes set except 'AdditionalInformation'",
95 ":61:1508010803DR4,52N024NONREF//ABC",
96 &TransactionTag{
97 Tag: ":61:",
98 ValutaDate: domain.ShortDate{Time: domain.Date(2015, 8, 1, time.Local).Truncate(24 * time.Hour)},
99 BookingDate: domain.ShortDate{Time: domain.Date(2015, 8, 3, time.Local).Truncate(24 * time.Hour)},
100 DebitCreditIndicator: "D",
101 CurrencyKind: "R",
102 Amount: 4.52,
103 BookingKey: "024",
104 Reference: "NONREF",
105 BankReference: "ABC",
106 },
107 },
108 {
109 "All attributes set except 'BankReference'",
110 ":61:1508010803DR4,52N024NONREF\r\n/DEF",
111 &TransactionTag{
112 Tag: ":61:",
113 ValutaDate: domain.ShortDate{Time: domain.Date(2015, 8, 1, time.Local).Truncate(24 * time.Hour)},
114 BookingDate: domain.ShortDate{Time: domain.Date(2015, 8, 3, time.Local).Truncate(24 * time.Hour)},
115 DebitCreditIndicator: "D",
116 CurrencyKind: "R",
117 Amount: 4.52,
118 BookingKey: "024",
119 Reference: "NONREF",
120 AdditionalInformation: "DEF",
121 },
122 },
123 {
124 "All attributes set except 'AdditionalInformation' and 'BankReference'",
125 ":61:1508010803DR4,52N024NONREF",
126 &TransactionTag{
127 Tag: ":61:",
128 ValutaDate: domain.ShortDate{Time: domain.Date(2015, 8, 1, time.Local).Truncate(24 * time.Hour)},
129 BookingDate: domain.ShortDate{Time: domain.Date(2015, 8, 3, time.Local).Truncate(24 * time.Hour)},
130 DebitCreditIndicator: "D",
131 CurrencyKind: "R",
132 Amount: 4.52,
133 BookingKey: "024",
134 Reference: "NONREF",
135 },
136 },
137 }
138
139 for _, test := range tests {
140 tag := &TransactionTag{}
141
142 err := tag.Unmarshal([]byte(test.marshaledValue))
143
144 if err != nil {
145 t.Logf("Expected no error, got %T:%v\n", err, err)
146 t.Fail()
147 }
148
149 if !reflect.DeepEqual(test.expectedTag, tag) {
150 pretty.Ldiff(t, test.expectedTag, tag)
151 t.Logf("Expected tag to equal\n%#v\n\tgot\n%#v\n", test.expectedTag, tag)
152 t.Fail()
153 }
154 }
155 }
156
157 func TestTransactionTagOrder(t *testing.T) {
158 testdata := "\r\n:20:HBCIKTOLST"
159 for i := 0; i < 10; i++ {
160 testdata += "\r\n:25:12345678/1234123456" +
161 "\r\n:28C:0" +
162 "\r\n:60F:C181105EUR1234,56" +
163 "\r\n:61:1811051105DR50,NMSCNONREF" +
164 "\r\n/OCMT/EUR50,//CHGS/ 0,/" +
165 "\r\n:86:177?00SB-SEPA-Ueberweisung?20" + strconv.Itoa(i+10) + " ?30?31?32Max Meier ?33 ?34000" +
166 "\r\n:62F:C190125EUR1234,56"
167
168 }
169 testdata += "\r\n-"
170 mt := &MT940{}
171 mt.Unmarshal([]byte(testdata))
172 for i, tr := range mt.Transactions {
173 if strings.TrimSpace(tr.Description.Purpose[0]) != strconv.Itoa(i+10) {
174 t.Logf("Purpose at index %d should be %d but is %s", i, i+10, tr.Description.Purpose[0])
175 t.Fail()
176 }
177
178 }
179
180 }
181
182 func TestTransactionListWithUnvalidData(t *testing.T) {
183 testdata := "\r\n:20:HBCIKTOLST"
184 testdata += "\r\n:25:12345678/1234123456" +
185 "\r\n:28C:0" +
186 "\r\n:60F:C181105EUR1234,56" +
187 "\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Meier ?33 ?34000" +
188 "\r\n:62F:C190125EUR1234,56"
189
190 testdata += "\r\n-"
191 mt := &MT940{}
192 error := mt.Unmarshal([]byte(testdata))
193 if error == nil {
194 t.Log("Error expected because of CustomTag without TransactionTag")
195 t.Fail()
196 }
197
198 }
199
200 func TestTransactionWithRedefineCustomDataTag(t *testing.T) {
201 testdata := "\r\n:20:HBCIKTOLST"
202 testdata += "\r\n:25:12345678/1234123456" +
203 "\r\n:28C:0" +
204 "\r\n:60F:C181105EUR1234,56" +
205
206 "\r\n:61:1811051105DR50,NMSCNONREF" +
207 "\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Meier ?33 ?34000" +
208 "\r\n:86:177?00SB-SEPA-Ueberweisung?20 ?30?31?32Max Meier ?33 ?34000" +
209 "\r\n:62F:C190125EUR1234,56"
210
211 testdata += "\r\n-"
212 mt := &MT940{}
213 error := mt.Unmarshal([]byte(testdata))
214 if error == nil {
215 t.Log("Error expected because of more than CustomTag after TransactionTag")
216 t.Fail()
217 }
218 }