Debian Codegraph @

iban/iban_test.go

raw
1 package iban
2
3 import (
4 "fmt"
5 "strings"
6 "testing"
7 )
8
9 func TestFrom(t *testing.T) {
10 t.Run("successful creation", func(t *testing.T) {
11 testCases := []struct {
12 country string
13 input string
14 result IBAN
15 }{
16 {"Germany", "DE02100500000024290661", "DE02100500000024290661"},
17 {"Great Britain", "GB11CITI18500811417983", "GB11CITI18500811417983"},
18 }
19
20 for _, tt := range testCases {
21 t.Run(tt.country, func(t *testing.T) {
22 result, err := From(tt.input)
23 if err != nil {
24 t.Logf("Expected no error, got %T:%v", err, err)
25 t.Fail()
26 }
27
28 if result != tt.result {
29 t.Logf("Expected result to equal %q, got %q", tt.result, result)
30 t.Fail()
31 }
32 })
33 }
34 })
35 t.Run("uncommon input", func(t *testing.T) {
36 testCases := []struct {
37 desc string
38 input string
39 result IBAN
40 }{
41 {
42 desc: "lowercase country code",
43 input: "de02100500000024290661",
44 result: "DE02100500000024290661",
45 },
46 {
47 desc: "mixed case countryCode",
48 input: "Gb11CITI18500811417983",
49 result: "GB11CITI18500811417983",
50 },
51 {
52 desc: "lowercase bban",
53 input: "GB11CITI18500811417983",
54 result: "GB11CITI18500811417983",
55 },
56 {
57 desc: "mixed case bban",
58 input: "GB11CiTi18500811417983",
59 result: "GB11CITI18500811417983",
60 },
61 {
62 desc: "mixed case countryCode and bban",
63 input: "gB11cItI18500811417983",
64 result: "GB11CITI18500811417983",
65 },
66 }
67
68 for _, tt := range testCases {
69 t.Run(tt.desc, func(t *testing.T) {
70 result, err := From(tt.input)
71 if err != nil {
72 t.Logf("Expected no error, got %T:%v", err, err)
73 t.Fail()
74 }
75
76 if result != tt.result {
77 t.Logf("Expected result to equal %q, got %q", tt.result, result)
78 t.Fail()
79 }
80 })
81 }
82 })
83 t.Run("errors", func(t *testing.T) {
84 testCases := []struct {
85 desc string
86 input string
87 }{
88 {
89 desc: "missing country code",
90 input: "1234567890123456789",
91 },
92 {
93 desc: "too long country code",
94 input: "ABCDE221234567890123456789",
95 },
96 {
97 desc: "too short country code",
98 input: "A221234567890123456789",
99 },
100 {
101 desc: "too long BBAN",
102 input: "AT331234567890123456789012345678901234567890",
103 },
104 {
105 desc: "proof number mismatch",
106 input: "GB99CITI18500811417983",
107 },
108 }
109
110 for _, tt := range testCases {
111 t.Run(tt.desc, func(t *testing.T) {
112 _, err := From(tt.input)
113 if err == nil {
114 t.Logf("Expected error, got nil")
115 t.Fail()
116 }
117 })
118 }
119 })
120 }
121
122 func TestNewGerman(t *testing.T) {
123 bankID := "10090044"
124 accountID := "532013018"
125 var result IBAN
126
127 result, err := NewGerman(bankID, accountID)
128
129 if err != nil {
130 t.Logf("Expected no error, got %T:%v", err, err)
131 t.Fail()
132 }
133
134 expectedResult := "DE10100900440532013018"
135
136 if string(result) != expectedResult {
137 t.Logf("Expected result to equal %q, got %q", expectedResult, result)
138 t.Fail()
139 }
140 }
141
142 func TestNew(t *testing.T) {
143 t.Run("successful creation", func(t *testing.T) {
144 testCases := []struct {
145 country string
146 countryCode string
147 bban string
148 result IBAN
149 }{
150 {"Albania", "AL", "202111090000000005012075", "AL06202111090000000005012075"},
151 {"Andorra", "AD", "00060004451247870930", "AD1000060004451247870930"},
152 {"Azerbaijan", "AZ", "UBAZ04003214540060AZN001", "AZ04UBAZ04003214540060AZN001"},
153 {"Bahrain", "BH", "CITI00001077181611", "BH02CITI00001077181611"},
154 {"Belgium", "BE", "096920886089", "BE45096920886089"},
155 {"Bosnia and Herzegovina", "BA", "1011606058553319", "BA391011606058553319"},
156 {"Brasilia", "BR", "00000000010670000117668C1", "BR0200000000010670000117668C1"},
157 {"British Virgin Islands", "VG", "NOSC0000000005002993", "VG48NOSC0000000005002993"},
158 {"Bulgaria", "BG", "RZBB91551002755190", "BG02RZBB91551002755190"},
159 {"Costa Rica", "CR", "015202220005614288", "CR79015202220005614288"},
160 {"Denmark", "DK", "20005036459478", "DK0220005036459478"},
161 {"Germany", "DE", "100500000024290661", "DE02100500000024290661"},
162 {"Dominican Republic", "DO", "BCBH00000000011003290022", "DO22BCBH00000000011003290022"},
163 {"El Salvador", "SV", "ACAT00000000000000123123", "SV43ACAT00000000000000123123"},
164 {"Estonia", "EE", "1700017000459042", "EE021700017000459042"},
165 {"Faeroe Islands", "FO", "91810001441878", "FO1291810001441878"},
166 {"Finland", "FI", "10403500314392", "FI0210403500314392"},
167 {"France", "FR", "20041000016219433J02076", "FR0220041000016219433J02076"},
168 {"Georgia", "GE", "TB7523045063700002", "GE02TB7523045063700002"},
169 {"Gibraltar", "GI", "BARC020452163087000", "GI04BARC020452163087000"},
170 {"Greece", "GR", "01102160000021661309175", "GR0201102160000021661309175"},
171 {"Greenland", "GL", "64710001504964", "GL2664710001504964"},
172 {"Great Britain", "GB", "CITI18500811417983", "GB11CITI18500811417983"},
173 {"Guatemala", "GT", "CITI01010000000004146026", "GT24CITI01010000000004146026"},
174 {"Iraq", "IQ", "CBIQ861800101010500", "IQ20CBIQ861800101010500"},
175 {"Ireland", "IE", "BOFI90008413113207", "IE02BOFI90008413113207"},
176 {"Iceland", "IS", "0116381002305610911109", "IS040116381002305610911109"},
177 {"Israel", "IL", "0108380000002149431", "IL020108380000002149431"},
178 {"Italy", "IT", "K0310412701000000820420", "IT43K0310412701000000820420"},
179 {"Jordan", "JO", "SCBL1260000000018525836101", "JO02SCBL1260000000018525836101"},
180 {"Kazakhstan", "KZ", "319C010005569698", "KZ04319C010005569698"},
181 {"Qatar", "QA", "QNBA000000000060565452001", "QA03QNBA000000000060565452001"},
182 {"Kosovo", "XK", "1301001002074155", "XK051301001002074155"},
183 {"Croatia", "HR", "23400093216312031", "HR0223400093216312031"},
184 {"Kuwait", "KW", "NBOK0000000000001000614589", "KW02NBOK0000000000001000614589"},
185 {"Latvia", "LV", "HABA0551007820897", "LV02HABA0551007820897"},
186 {"Lebanon", "LB", "001400000302300023018319", "LB02001400000302300023018319"},
187 {"Liechtenstein", "LI", "08800000022875748", "LI0308800000022875748"},
188 {"Lithuania", "LT", "7300010134441147", "LT027300010134441147"},
189 {"Luxembourg", "LU", "0019175546294000", "LU020019175546294000"},
190 {"Malta", "MT", "VALL22013000000040013752732", "MT02VALL22013000000040013752732"},
191 {"Mauritania", "MR", "00012000010000009880016", "MR1300012000010000009880016"},
192 {"Mauritius", "MU", "MCBL0901000001879025000USD", "MU03MCBL0901000001879025000USD"},
193 {"Macedonia", "MK", "200000625758632", "MK07200000625758632"},
194 {"Moldova", "MD", "MO2224ASV41884097100", "MD14MO2224ASV41884097100"},
195 {"Monaco", "MC", "12739000710075018000P14", "MC2412739000710075018000P14"},
196 {"Montenegro", "ME", "505120000000466170", "ME25505120000000466170"},
197 {"Netherlands", "NL", "ABNA0457180536", "NL02ABNA0457180536"},
198 {"Norway", "NO", "39916835985", "NO0239916835985"},
199 {"Austria", "AT", "1100000622888600", "AT021100000622888600"},
200 {"Pakistan", "PK", "SCBL0000001925518401", "PK02SCBL0000001925518401"},
201 {"Palestine", "PS", "ARAB000000009040781605610", "PS06ARAB000000009040781605610"},
202 {"Poland", "PL", "103000190109780401676562", "PL02103000190109780401676562"},
203 {"Portugal", "PT", "003600409911001102673", "PT50003600409911001102673"},
204 {"Romania", "RO", "BRDE445SV75163474450", "RO02BRDE445SV75163474450"},
205 {"Saint Lucia", "LC", "HEMM000100010012001200023015", "LC55HEMM000100010012001200023015"},
206 {"San Marino", "SM", "U0854009803000030174419", "SM07U0854009803000030174419"},
207 {"São Tomé and Príncipe", "ST", "000200000289355710148", "ST23000200000289355710148"},
208 {"Saudi Arabia", "SA", "20000002480647579940", "SA0220000002480647579940"},
209 {"Sweden", "SE", "30000000030301099952", "SE0230000000030301099952"},
210 {"Swiss", "CH", "0020720710117540C", "CH020020720710117540C"},
211 {"Serbia", "RS", "105008054113238018", "RS35105008054113238018"},
212 {"Seychelles", "SC", "NOVH00000021002035257028SCR", "SC74NOVH00000021002035257028SCR"},
213 {"Slovak Republic", "SK", "02000000003679748552", "SK0202000000003679748552"},
214 {"Slowenia", "SI", "011006000005649", "SI56011006000005649"},
215 {"Spain", "ES", "21000555370200853027", "ES1321000555370200853027"},
216 {"Timor-Leste", "TL", "0030000000025923744", "TL380030000000025923744"},
217 {"Turkey", "TR", "0001000201529153355002", "TR020001000201529153355002"},
218 {"Czech Republic", "CZ", "01000000199216760237", "CZ0201000000199216760237"},
219 {"Tunisia", "TN", "01026067111999766058", "TN5901026067111999766058"},
220 {"Ukraine", "UA", "3052990004149497803982794", "UA123052990004149497803982794"},
221 {"Hungary", "HU", "116000060000000064247067", "HU02116000060000000064247067"},
222 {"United Arab Emirates", "AE", "0090004001079346500", "AE020090004001079346500"},
223 {"Belarus", "BY", "AKBB10100000002966000000", "BY86AKBB10100000002966000000"},
224 {"Cyprus", "CY", "002001950000357009822416", "CY02002001950000357009822416"},
225 }
226
227 for _, tt := range testCases {
228 t.Run(tt.country, func(t *testing.T) {
229 result, err := New(tt.countryCode, tt.bban)
230 if err != nil {
231 t.Logf("Expected no error, got %T:%v", err, err)
232 t.Fail()
233 }
234
235 if result != tt.result {
236 t.Logf("Expected result to equal %q, got %q", tt.result, result)
237 t.Fail()
238 }
239 })
240 }
241 })
242 t.Run("uncommon input", func(t *testing.T) {
243 testCases := []struct {
244 desc string
245 countryCode string
246 bban string
247 result IBAN
248 }{
249 {
250 desc: "lowercase country code",
251 countryCode: "de",
252 bban: "100500000024290661",
253 result: "DE02100500000024290661",
254 },
255 {
256 desc: "mixed case countryCode",
257 countryCode: "Gb",
258 bban: "CITI18500811417983",
259 result: "GB11CITI18500811417983",
260 },
261 {
262 desc: "lowercase bban",
263 countryCode: "GB",
264 bban: "CITI18500811417983",
265 result: "GB11CITI18500811417983",
266 },
267 {
268 desc: "mixed case bban",
269 countryCode: "GB",
270 bban: "CiTi18500811417983",
271 result: "GB11CITI18500811417983",
272 },
273 {
274 desc: "mixed case countryCode and bban",
275 countryCode: "gB",
276 bban: "cItI18500811417983",
277 result: "GB11CITI18500811417983",
278 },
279 }
280
281 for _, tt := range testCases {
282 t.Run(tt.desc, func(t *testing.T) {
283 result, err := New(tt.countryCode, tt.bban)
284 if err != nil {
285 t.Logf("Expected no error, got %T:%v", err, err)
286 t.Fail()
287 }
288
289 if result != tt.result {
290 t.Logf("Expected result to equal %q, got %q", tt.result, result)
291 t.Fail()
292 }
293 })
294 }
295 })
296 t.Run("errors", func(t *testing.T) {
297 testCases := []struct {
298 desc string
299 countryCode string
300 bban string
301 }{
302 {
303 desc: "empty country code",
304 countryCode: "",
305 bban: "1234567890123456789",
306 },
307 {
308 desc: "too long country code",
309 countryCode: "ABCDE",
310 bban: "1234567890123456789",
311 },
312 {
313 desc: "too short country code",
314 countryCode: "A",
315 bban: "1234567890123456789",
316 },
317 {
318 desc: "too long BBAN",
319 countryCode: "AT",
320 bban: "1234567890123456789012345678901234567890",
321 },
322 {
323 desc: "missing BBAN",
324 countryCode: "AT",
325 bban: "",
326 },
327 }
328
329 for _, tt := range testCases {
330 t.Run(tt.desc, func(t *testing.T) {
331 _, err := New(tt.countryCode, tt.bban)
332 if err == nil {
333 t.Logf("Expected error, got nil")
334 t.Fail()
335 }
336 })
337 }
338 })
339 }
340
341 func TestIbanValid(t *testing.T) {
342 t.Run("valid german IBAN", func(t *testing.T) {
343 iban, _ := NewGerman("10090044", "0532013018")
344
345 ok := iban.Valid()
346 if !ok {
347 t.Logf("Expected iban to be valid")
348 t.Fail()
349 }
350 })
351 t.Run("valid british IBAN", func(t *testing.T) {
352 iban, _ := New("GB", "CITI18500811417983")
353
354 ok := iban.Valid()
355 if !ok {
356 t.Logf("Expected iban to be valid")
357 t.Fail()
358 }
359 })
360 t.Run("valid uncommon IBAN", func(t *testing.T) {
361 iban, _ := New("GB", "CITI18500811417983")
362 iban = IBAN(fmt.Sprintf(
363 "%s%s%s",
364 strings.ToLower(iban.CountryCode()),
365 iban.ProofNumber(),
366 strings.ToLower(iban.BBAN()),
367 ))
368
369 ok := iban.Valid()
370 if !ok {
371 t.Logf("Expected iban to be valid")
372 t.Fail()
373 }
374 })
375 t.Run("invalid german IBAN", func(t *testing.T) {
376 iban := IBAN("DE9910090044053201301812345678901234567890")
377
378 ok := iban.Valid()
379 if ok {
380 t.Logf("Expected iban to be invalid")
381 t.Fail()
382 }
383 })
384 t.Run("invalid german IBAN", func(t *testing.T) {
385 iban := IBAN("DE99100900440532013018")
386
387 ok := iban.Valid()
388 if ok {
389 t.Logf("Expected iban to be invalid")
390 t.Fail()
391 }
392 })
393 t.Run("invalid IBAN", func(t *testing.T) {
394 iban, _ := New("GB", "CITI18500811417983")
395 iban = IBAN(fmt.Sprintf(
396 "%s%s%s",
397 "12",
398 iban.ProofNumber(),
399 strings.ToLower(iban.BBAN()),
400 ))
401
402 ok := iban.Valid()
403 if ok {
404 t.Logf("Expected iban to be invalid")
405 t.Fail()
406 }
407 })
408 }
409
410 func TestIsValid(t *testing.T) {
411 t.Run("valid german IBAN", func(t *testing.T) {
412 iban, _ := NewGerman("10090044", "0532013018")
413
414 ok := IsValid(iban.String())
415 if !ok {
416 t.Logf("Expected iban to be valid")
417 t.Fail()
418 }
419 })
420 t.Run("valid british IBAN", func(t *testing.T) {
421 iban, _ := New("GB", "CITI18500811417983")
422
423 ok := IsValid(iban.String())
424 if !ok {
425 t.Logf("Expected iban to be valid")
426 t.Fail()
427 }
428 })
429 t.Run("valid uncommon IBAN", func(t *testing.T) {
430 iban, _ := New("GB", "CITI18500811417983")
431 iban = IBAN(fmt.Sprintf(
432 "%s%s%s",
433 strings.ToLower(iban.CountryCode()),
434 iban.ProofNumber(),
435 strings.ToLower(iban.BBAN()),
436 ))
437
438 ok := IsValid(iban.String())
439 if !ok {
440 t.Logf("Expected iban to be valid")
441 t.Fail()
442 }
443 })
444 t.Run("invalid german IBAN", func(t *testing.T) {
445 iban := IBAN("DE9910090044053201301812345678901234567890")
446
447 ok := IsValid(iban.String())
448 if ok {
449 t.Logf("Expected iban to be invalid")
450 t.Fail()
451 }
452 })
453 t.Run("invalid german IBAN", func(t *testing.T) {
454 iban := IBAN("DE99100900440532013018")
455
456 ok := IsValid(iban.String())
457 if ok {
458 t.Logf("Expected iban to be invalid")
459 t.Fail()
460 }
461 })
462 t.Run("invalid IBAN", func(t *testing.T) {
463 iban, _ := New("GB", "CITI18500811417983")
464 iban = IBAN(fmt.Sprintf(
465 "%s%s%s",
466 "12",
467 iban.ProofNumber(),
468 strings.ToLower(iban.BBAN()),
469 ))
470
471 ok := IsValid(iban.String())
472 if ok {
473 t.Logf("Expected iban to be invalid")
474 t.Fail()
475 }
476 })
477 }
478
479 func TestIbanBBAN(t *testing.T) {
480 iban := IBAN("DE10100900440532013018")
481
482 bban := iban.BBAN()
483
484 expectedBban := "100900440532013018"
485
486 if bban != expectedBban {
487 t.Logf("Expected bankId to equal %q, got %q\n", expectedBban, bban)
488 t.Fail()
489 }
490 }
491
492 func TestIbanBankId(t *testing.T) {
493 iban := IBAN("DE10100900440532013018")
494
495 bankID := iban.BankID()
496
497 expectedBankID := "10090044"
498
499 if bankID != expectedBankID {
500 t.Logf("Expected bankId to equal %q, got %q\n", expectedBankID, bankID)
501 t.Fail()
502 }
503 }
504
505 func TestIbanAccountId(t *testing.T) {
506 iban := IBAN("DE10100900440532013018")
507
508 accountID := iban.AccountID()
509
510 expectedAccountID := "532013018"
511
512 if accountID != expectedAccountID {
513 t.Logf("Expected accountId to equal %q, got %q\n", expectedAccountID, accountID)
514 t.Fail()
515 }
516 }
517
518 func TestIbanCountry(t *testing.T) {
519 iban := IBAN("DE10100900440532013018")
520
521 country := iban.CountryCode()
522
523 expectedCountry := "DE"
524
525 if country != expectedCountry {
526 t.Logf("Expected country to equal %q, got %q\n", expectedCountry, country)
527 t.Fail()
528 }
529 }
530
531 func TestIbanProofNumber(t *testing.T) {
532 iban := IBAN("DE10100900440532013018")
533
534 proofNumber := iban.ProofNumber()
535
536 expectedProofNumber := "10"
537
538 if proofNumber != expectedProofNumber {
539 t.Logf("Expected proofNumber to equal %q, got %q\n", expectedProofNumber, proofNumber)
540 t.Fail()
541 }
542 }
543
544 func TestIbanString(t *testing.T) {
545 iban := IBAN("GB11CITI18500811417983")
546
547 s := iban.String()
548
549 expected := "GB11CITI18500811417983"
550
551 if expected != s {
552 t.Logf("Expected iban.String() to equal %q, got %q", expected, s)
553 t.Fail()
554 }
555 }
556
557 func TestPrint(t *testing.T) {
558 iban := IBAN("GB11CITI18500811417983")
559
560 printed := Print(iban)
561
562 expected := "GB11 CITI 1850 0811 4179 83"
563
564 if expected != printed {
565 t.Logf("Expected printed iban to equal %q, got %q", expected, printed)
566 t.Fail()
567 }
568 }