-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
withdraw_test.go
127 lines (114 loc) · 3.11 KB
/
withdraw_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
package coincheck
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestClientGetBankAccounts(t *testing.T) {
t.Run("GetBankAccounts returns a list of bank accounts", func(t *testing.T) {
// Create a new test server
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wantMethod := http.MethodGet
if diff := cmp.Diff(wantMethod, r.Method); diff != "" {
printDiff(t, diff)
}
wantEndpoint := "/api/bank_accounts"
if diff := cmp.Diff(wantEndpoint, r.URL.Path); diff != "" {
printDiff(t, diff)
}
result := GetBankAccountsResponse{
Success: true,
Data: []BankAccount{
{
ID: 1,
BankName: "bank_name_1",
BranchName: "branch_name_1",
BankAccountType: "bank_account_type_1",
Number: "number_1",
Name: "name_1",
},
{
ID: 2,
BankName: "bank_name_2",
BranchName: "branch_name_2",
BankAccountType: "bank_account_type_2",
Number: "number_2",
Name: "name_2",
},
},
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Fatal(err)
}
}))
// Create a new client
client, err := NewClient(
WithBaseURL(testServer.URL),
WithCredentials("api_key", "api_secret"),
)
if err != nil {
t.Fatal(err)
}
// Start testing
got, err := client.GetBankAccounts(context.Background())
if err != nil {
t.Fatal(err)
}
// Check the result
want := &GetBankAccountsResponse{
Success: true,
Data: []BankAccount{
{
ID: 1,
BankName: "bank_name_1",
BranchName: "branch_name_1",
BankAccountType: "bank_account_type_1",
Number: "number_1",
Name: "name_1",
},
{
ID: 2,
BankName: "bank_name_2",
BranchName: "branch_name_2",
BankAccountType: "bank_account_type_2",
Number: "number_2",
Name: "name_2",
},
},
}
if diff := cmp.Diff(want, got); diff != "" {
printDiff(t, diff)
}
})
t.Run("GetBankAccounts returns an error if the server return internal server error", func(t *testing.T) {
// Create a new test server
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
// Create a new client
client, err := NewClient(WithBaseURL(testServer.URL))
if err != nil {
t.Fatal(err)
}
// Start testing
if _, err = client.GetBankAccounts(context.Background()); err == nil {
t.Error("want error, but got nil")
}
})
t.Run("GetBankAccounts returns an error if client does not set credentials", func(t *testing.T) {
// Create a new client
client, err := NewClient(WithBaseURL("https://example.com"))
if err != nil {
t.Fatal(err)
}
// Start testing
_, err = client.GetBankAccounts(context.Background())
if !errors.Is(err, ErrNoCredentials) {
t.Errorf("error is not ErrNoCredentials: %v", err)
}
})
}