-
Notifications
You must be signed in to change notification settings - Fork 40
/
exchangeRate_test.go
119 lines (87 loc) · 3.29 KB
/
exchangeRate_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
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
package wire
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// mockExchangeRate creates a ExchangeRate
func mockExchangeRate() *ExchangeRate {
eRate := NewExchangeRate()
eRate.ExchangeRate = "1,2345"
return eRate
}
// TestMockExchangeRate validates mockExchangeRate
func TestMockExchangeRate(t *testing.T) {
eRate := mockExchangeRate()
require.NoError(t, eRate.Validate(), "mockExchangeRate does not validate and will break other tests")
}
// TestExchangeRate validates ExchangeRate
func TestExchangeRateNumeric(t *testing.T) {
eRate := mockExchangeRate()
eRate.ExchangeRate = "1,--0.00"
err := eRate.Validate()
require.EqualError(t, err, fieldError("ExchangeRate", ErrNonAmount, eRate.ExchangeRate).Error())
}
// TestParseExchangeRateWrongLength parses a wrong ExchangeRate record length
func TestParseExchangeRateWrongLength(t *testing.T) {
var line = "{3720}1,2345"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseExchangeRate()
require.EqualError(t, err, r.parseError(fieldError("ExchangeRate", ErrRequireDelimiter)).Error())
_, err = r.Read()
require.EqualError(t, err, r.parseError(fieldError("ExchangeRate", ErrRequireDelimiter)).Error())
}
// TestParseExchangeRateReaderParseError parses a wrong ExchangeRate reader parse error
func TestParseExchangeRateReaderParseError(t *testing.T) {
var line = "{3720}1,2345Z *"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseExchangeRate()
require.EqualError(t, err, r.parseError(fieldError("ExchangeRate", ErrNonAmount, "1,2345Z")).Error())
_, err = r.Read()
require.EqualError(t, err, r.parseError(fieldError("ExchangeRate", ErrNonAmount, "1,2345Z")).Error())
}
// TestExchangeRateTagError validates a ExchangeRate tag
func TestExchangeRateTagError(t *testing.T) {
eRate := mockCurrencyInstructedAmount()
eRate.tag = "{9999}"
err := eRate.Validate()
require.EqualError(t, err, fieldError("tag", ErrValidTagForType, eRate.tag).Error())
}
// TestStringErrorExchangeRateVariableLength parses using variable length
func TestStringErrorExchangeRateVariableLength(t *testing.T) {
var line = "{3720}"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseExchangeRate()
require.Nil(t, err)
line = "{3720}123 NNN"
r = NewReader(strings.NewReader(line))
r.line = line
err = r.parseExchangeRate()
require.ErrorContains(t, err, ErrRequireDelimiter.Error())
line = "{3720}123***"
r = NewReader(strings.NewReader(line))
r.line = line
err = r.parseExchangeRate()
require.ErrorContains(t, err, r.parseError(NewTagMaxLengthErr(errors.New(""))).Error())
line = "{3720}123*"
r = NewReader(strings.NewReader(line))
r.line = line
err = r.parseExchangeRate()
require.Equal(t, err, nil)
}
// TestStringExchangeRateOptions validates Format() formatted according to the FormatOptions
func TestStringExchangeRateOptions(t *testing.T) {
var line = "{3720}123*"
r := NewReader(strings.NewReader(line))
r.line = line
err := r.parseExchangeRate()
require.Equal(t, err, nil)
record := r.currentFEDWireMessage.ExchangeRate
require.Equal(t, record.String(), "{3720}123 *")
require.Equal(t, record.Format(FormatOptions{VariableLengthFields: true}), "{3720}123*")
require.Equal(t, record.String(), record.Format(FormatOptions{VariableLengthFields: false}))
}