-
Notifications
You must be signed in to change notification settings - Fork 0
/
banking_test.go
183 lines (149 loc) · 4.89 KB
/
banking_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
package inter
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestApiTransactionTypeMap(t *testing.T) {
t.Run("returns unknow if type not found", func(t *testing.T) {
transactionTypeStr := "wix"
transactionType := apiTransactionTypeMap[transactionTypeStr]
require.Equal(t, transactionType.String(), "unknow")
})
t.Run("returns the type correctly", func(t *testing.T) {
transactionTypeStr := "PIX"
transactionType := apiTransactionTypeMap[transactionTypeStr]
require.Equal(t, transactionType, PixTransactionType)
})
}
func TestApiTransactionOperationMap(t *testing.T) {
t.Run("returns invalid if type not found", func(t *testing.T) {
transactionOperationStr := "x"
transactionOperation := apiTransactionOperationMap[transactionOperationStr]
require.Equal(t, transactionOperation.String(), "invalid")
})
t.Run("returns the type correctly", func(t *testing.T) {
transactionOperationStr := "C"
transactionOperation := apiTransactionOperationMap[transactionOperationStr]
require.Equal(t, transactionOperation, CreditTransactionOperation)
})
}
func TestParseApiTransactions(t *testing.T) {
t.Run("returns an error if data is invalid", func(t *testing.T) {
data := []byte(`{"transacoes": {}}`)
_, err := parseApiTransactions(data)
require.Error(t, err)
})
t.Run("correctly parses input data", func(t *testing.T) {
var (
transactionDate = "2022-02-02"
transactionType = "PIX"
transactionOperation = "C"
transactionValue = "123.45"
transactionTitle = "title"
transactionDescription = "description"
)
data := []byte(fmt.Sprintf(`{
"transacoes": [{
"dataEntrada": "%s",
"tipoTransacao": "%s",
"tipoOperacao": "%s",
"valor": "%s",
"titulo": "%s",
"descricao": "%s"
}]
}`, transactionDate, transactionType, transactionOperation, transactionValue,
transactionTitle, transactionDescription))
transactionDateTime, err := time.Parse(time.DateOnly, transactionDate)
require.NoError(t, err)
transactionFloatValue, err := strconv.ParseFloat(transactionValue, 32)
require.NoError(t, err)
want := []Transaction{
Transaction{
Date: transactionDateTime,
Type: apiTransactionTypeMap[transactionType],
Operation: apiTransactionOperationMap[transactionOperation],
Value: float32(transactionFloatValue),
Title: transactionTitle,
Description: transactionDescription,
},
}
got, err := parseApiTransactions(data)
require.NoError(t, err)
require.Equal(t, got, want)
})
}
func TestBankingTransactions(t *testing.T) {
t.Run("returns an error on context cancelation", func(t *testing.T) {
client := NewClient(tls.Certificate{})
banking := NewBanking(client, Token{})
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := banking.Transactions(ctx, time.Now(), time.Now())
require.ErrorIs(t, err, context.Canceled)
})
var response string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, response)
}))
defer ts.Close()
t.Run("returns an error if data is invalid", func(t *testing.T) {
response = `{"transactions": "}`
client := NewClient(tls.Certificate{})
client.apiBaseUrl = ts.URL
oauth := NewOAuth(client)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := oauth.Authorize(ctx, "client-id", "client-secret")
require.Error(t, err)
})
t.Run("returns the created token", func(t *testing.T) {
var (
transactionDate = "2022-02-02"
transactionType = "PIX"
transactionOperation = "C"
transactionValue = "123.45"
transactionTitle = "title"
transactionDescription = "description"
)
response = fmt.Sprintf(`{
"transacoes": [{
"dataEntrada": "%s",
"tipoTransacao": "%s",
"tipoOperacao": "%s",
"valor": "%s",
"titulo": "%s",
"descricao": "%s"
}]
}`, transactionDate, transactionType, transactionOperation, transactionValue,
transactionTitle, transactionDescription)
transactionDateTime, err := time.Parse(time.DateOnly, transactionDate)
require.NoError(t, err)
transactionFloatValue, err := strconv.ParseFloat(transactionValue, 32)
require.NoError(t, err)
want := []Transaction{
Transaction{
Date: transactionDateTime,
Type: apiTransactionTypeMap[transactionType],
Operation: apiTransactionOperationMap[transactionOperation],
Value: float32(transactionFloatValue),
Title: transactionTitle,
Description: transactionDescription,
},
}
client := NewClient(tls.Certificate{})
client.apiBaseUrl = ts.URL
banking := NewBanking(client, Token{})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
got, err := banking.Transactions(ctx, time.Now(), time.Now())
require.NoError(t, err)
require.Equal(t, got, want)
})
}