-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransactions_test.go
266 lines (237 loc) · 9.03 KB
/
transactions_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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package fio
import (
"bytes"
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const transactionsResponse = `
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccountStatement>
<Info>
<accountId>2501201133</accountId>
<bankId>8330</bankId>
<currency>EUR</currency>
<iban>SK2383300000002501201133</iban>
<bic>FIOZSKBAXXX</bic>
<openingBalance>0.00</openingBalance>
<closingBalance>45.97</closingBalance>
<dateStart>2017-01-01+01:00</dateStart>
<dateEnd>2017-05-01+02:00</dateEnd>
<idFrom>13926601410</idFrom>
<idTo>13926601410</idTo>
</Info>
<TransactionList>
<Transaction>
<column_22 name="ID pohybu" id="22">13926601410</column_22>
<column_0 name="Datum" id="0">2017-04-11+02:00</column_0>
<column_1 name="Objem" id="1">45.97</column_1>
<column_14 name="Měna" id="14">EUR</column_14>
<column_2 name="Protiúčet" id="2">SK2183100000001100248431</column_2>
<column_3 id="3" name="Kód banky">2010</column_3>
<column_10 name="Název protiúčtu" id="10">john doe</column_10>
<column_12 name="Název banky" id="12">ZUNO BANK AG, pobočka zahraničnej banky</column_12>
<column_4 name="KS" id="4">0558</column_4>
<column_5 name="VS" id="5">0001</column_5>
<column_6 name="SS" id="6">0002</column_6>
<column_7 name="Uživatelská identifikace" id="7">john doe</column_7>
<column_16 name="Zpráva pro příjemce" id="16">/DO2017-04-10/SPPrevod zo zuno, john doe</column_16>
<column_8 name="Typ" id="8">Bezhotovostní příjem</column_8>
<column_25 name="Komentář" id="25">john doe</column_25>
<column_26 name="BIC" id="26">RIDBSKBXXXX</column_26>
<column_17 name="ID pokynu" id="17">15689512949</column_17>
<column_18 name="Upřesnění" id="18">45.97 EUR</column_18>
<column_27 name="Reference plátce" id="27">2000000003</column_27>
</Transaction>
</TransactionList>
</AccountStatement>
`
func TestByPeriod(t *testing.T) {
setup()
defer teardown()
dateFrom := time.Now()
dateTo := time.Now()
urlStr := fmt.Sprintf("/v1/rest/periods/%v/%v/%v/transactions.xml", testingToken, fmtDate(dateFrom), fmtDate(dateTo))
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, transactionsResponse)
})
opts := ByPeriodOptions{
DateFrom: dateFrom,
DateTo: dateTo,
}
resp, err := client.Transactions.ByPeriod(context.Background(), opts)
require.NoError(t, err)
openingBalance, _ := decimal.NewFromString("0.00")
closingBalance, _ := decimal.NewFromString("45.97")
want := &TransactionsResponse{
Info: StatementInfo{
AccountID: 2501201133,
BankID: "8330",
Currency: "EUR",
IBAN: "SK2383300000002501201133",
BIC: "FIOZSKBAXXX",
OpeningBalance: openingBalance,
ClosingBalance: closingBalance,
DateStart: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.FixedZone("+0100", 60*60)),
DateEnd: time.Date(2017, time.May, 1, 0, 0, 0, 0, time.FixedZone("+0200", 60*60*2)),
IDFrom: 13926601410,
IDTo: 13926601410,
IDLastDownload: 0,
IDList: 0,
},
Transactions: []Transaction{
{
ID: 13926601410,
Date: time.Date(2017, time.April, 11, 0, 0, 0, 0, time.FixedZone("+0200", 60*60*2)),
Amount: closingBalance,
Currency: "EUR",
Account: "SK2183100000001100248431",
AccountName: "john doe",
BankCode: "2010",
BankName: "ZUNO BANK AG, pobočka zahraničnej banky",
RecipientMessage: "/DO2017-04-10/SPPrevod zo zuno, john doe",
ConstantSymbol: "0558",
VariableSymbol: "0001",
SpecificSymbol: "0002",
BIC: "RIDBSKBXXXX",
OrderID: "15689512949",
Comment: "john doe",
Specification: "45.97 EUR",
UserIdentification: "john doe",
Type: "Bezhotovostní příjem",
PayerReference: "2000000003",
},
},
}
assertEqualTransactionsResp(t, want, resp)
}
func TestExport(t *testing.T) {
setup()
defer teardown()
testingFormat := JSONFormat
dateFrom := time.Now()
dateTo := time.Now()
urlStr := fmt.Sprintf("/v1/rest/periods/%v/%v/%v/transactions.%v", testingToken, fmtDate(dateFrom), fmtDate(dateTo), testingFormat)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
fmt.Fprint(w, transactionsResponse)
})
buf := new(bytes.Buffer)
opts := ExportOptions{
DateFrom: dateFrom,
DateTo: dateTo,
Format: testingFormat,
}
err := client.Transactions.Export(context.Background(), opts, buf)
require.NoError(t, err)
require.Equal(t, len(transactionsResponse), buf.Len())
}
func TestGetStatement(t *testing.T) {
setup()
defer teardown()
year := 2017
id := 1
urlStr := fmt.Sprintf("/v1/rest/by-id/%v/%v/%v/transactions.xml", testingToken, year, id)
mux.HandleFunc(urlStr, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
_, err := fmt.Fprint(w, transactionsResponse)
assert.NoError(t, err)
})
opts := GetStatementOptions{
Year: year,
ID: id,
}
resp, err := client.Transactions.GetStatement(context.Background(), opts)
require.NoError(t, err)
openingBalance, _ := decimal.NewFromString("0.00")
closingBalance, _ := decimal.NewFromString("45.97")
want := &TransactionsResponse{
Info: StatementInfo{
AccountID: 2501201133,
BankID: "8330",
Currency: "EUR",
IBAN: "SK2383300000002501201133",
BIC: "FIOZSKBAXXX",
OpeningBalance: openingBalance,
ClosingBalance: closingBalance,
DateStart: time.Date(2017, time.January, 1, 0, 0, 0, 0, time.FixedZone("+0100", 60*60)),
DateEnd: time.Date(2017, time.May, 1, 0, 0, 0, 0, time.FixedZone("+0200", 60*60*2)),
IDFrom: 13926601410,
IDTo: 13926601410,
IDLastDownload: 0,
IDList: 0,
},
Transactions: []Transaction{
{
ID: 13926601410,
Date: time.Date(2017, time.April, 11, 0, 0, 0, 0, time.FixedZone("+0200", 60*60*2)),
Amount: closingBalance,
Currency: "EUR",
Account: "SK2183100000001100248431",
AccountName: "john doe",
BankCode: "2010",
BankName: "ZUNO BANK AG, pobočka zahraničnej banky",
RecipientMessage: "/DO2017-04-10/SPPrevod zo zuno, john doe",
ConstantSymbol: "0558",
VariableSymbol: "0001",
SpecificSymbol: "0002",
BIC: "RIDBSKBXXXX",
OrderID: "15689512949",
Comment: "john doe",
Specification: "45.97 EUR",
UserIdentification: "john doe",
Type: "Bezhotovostní příjem",
PayerReference: "2000000003",
},
},
}
assertEqualTransactionsResp(t, want, resp)
}
func assertEqualTransactionsResp(t *testing.T, want *TransactionsResponse, resp *TransactionsResponse) {
t.Helper()
require.Equal(t, want.Info.AccountID, resp.Info.AccountID)
require.Equal(t, want.Info.BankID, resp.Info.BankID)
require.Equal(t, want.Info.Currency, resp.Info.Currency)
require.Equal(t, want.Info.IBAN, resp.Info.IBAN)
require.Equal(t, want.Info.BIC, resp.Info.BIC)
require.Equal(t, want.Info.OpeningBalance, resp.Info.OpeningBalance)
require.Equal(t, want.Info.ClosingBalance, resp.Info.ClosingBalance)
require.Equal(t, want.Info.IDFrom, resp.Info.IDFrom)
require.Equal(t, want.Info.IDTo, resp.Info.IDTo)
require.Equal(t, want.Info.IDLastDownload, resp.Info.IDLastDownload)
require.Equal(t, want.Info.IDList, resp.Info.IDList)
require.True(t, want.Info.DateStart.Equal(resp.Info.DateStart))
require.True(t, want.Info.DateEnd.Equal(resp.Info.DateEnd))
require.Equal(t, len(want.Transactions), len(resp.Transactions))
for i, tx := range want.Transactions {
assertEqualTransaction(t, tx, resp.Transactions[i])
}
}
func assertEqualTransaction(t *testing.T, want Transaction, got Transaction) {
t.Helper()
require.Equal(t, want.ID, got.ID)
require.Equal(t, want.Amount, got.Amount)
require.Equal(t, want.Currency, got.Currency)
require.Equal(t, want.Account, got.Account)
require.Equal(t, want.AccountName, got.AccountName)
require.Equal(t, want.BankCode, got.BankCode)
require.Equal(t, want.BankName, got.BankName)
require.Equal(t, want.RecipientMessage, got.RecipientMessage)
require.Equal(t, want.ConstantSymbol, got.ConstantSymbol)
require.Equal(t, want.VariableSymbol, got.VariableSymbol)
require.Equal(t, want.SpecificSymbol, got.SpecificSymbol)
require.Equal(t, want.BIC, got.BIC)
require.Equal(t, want.OrderID, got.OrderID)
require.Equal(t, want.Specification, got.Specification)
require.Equal(t, want.Comment, got.Comment)
require.Equal(t, want.UserIdentification, got.UserIdentification)
require.Equal(t, want.Type, got.Type)
require.Equal(t, want.PayerReference, got.PayerReference)
require.True(t, want.Date.Equal(got.Date))
}