-
Notifications
You must be signed in to change notification settings - Fork 39
/
amount_test.go
89 lines (69 loc) · 2.05 KB
/
amount_test.go
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
package wire
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// mockAmount creates an a Amount
func mockAmount() *Amount {
a := NewAmount()
a.Amount = "000001234567"
return a
}
// TestMockAmount validates mockAmount
func TestMockAmount(t *testing.T) {
a := mockAmount()
require.NoError(t, a.Validate(), "mockAmount does not validate and will break other tests")
}
func TestAmount_Validate(t *testing.T) {
tests := []struct {
inAmount string
wantErr error
}{
{mockAmount().Amount, nil},
{"", fieldError("Amount", ErrFieldRequired)},
{"X,", fieldError("Amount", ErrNonAmount, "X,")},
{"12.05", fieldError("Amount", ErrNonAmount, "12.05")},
{"1,000.39", fieldError("Amount", ErrNonAmount, "1,000.39")},
}
for _, tt := range tests {
t.Run(tt.inAmount, func(t *testing.T) {
amt := NewAmount()
amt.Amount = tt.inAmount
got := amt.Validate()
if tt.wantErr == nil {
require.NoError(t, got)
} else {
require.Error(t, got)
require.Equal(t, tt.wantErr, got)
}
})
}
}
// TestParseAmountWrongLength parses a wrong Amount record length
func TestParseAmountWrongLength(t *testing.T) {
var line = "{2000}00"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseAmount()
require.EqualError(t, err, r.parseError(NewTagWrongLengthErr(18, len(r.line))).Error())
}
// TestParseAmountReaderParseError parses a wrong Amount reader parse error
func TestParseAmountReaderParseError(t *testing.T) {
var line = "{2000}00000Z030022"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseAmount()
expected := r.parseError(fieldError("Amount", ErrNonAmount, "00000Z030022")).Error()
require.EqualError(t, err, expected)
_, err = r.Read()
expected = r.parseError(fieldError("Amount", ErrNonAmount, "00000Z030022")).Error()
require.EqualError(t, err, expected)
}
// TestAmountTagError validates Amount tag
func TestAmountTagError(t *testing.T) {
a := mockAmount()
a.tag = "{9999}"
err := a.Validate()
require.EqualError(t, err, fieldError("tag", ErrValidTagForType, a.tag).Error())
}