-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance_test.go
111 lines (99 loc) · 2.65 KB
/
balance_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
package coincheck
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestClient_GetBalance(t *testing.T) {
t.Run("GetBalance returns the balance", func(t *testing.T) {
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/accounts/balance"
if diff := cmp.Diff(wantEndpoint, r.URL.Path); diff != "" {
printDiff(t, diff)
}
result := GetAccountsBalanceResponse{
Success: true,
JPY: "0.8401",
BTC: "7.75052654",
JPYReserved: "3000.0",
BTCReserved: "3.5002",
JPYLendInUse: "1.1",
BTCLendInUse: "0.3",
JPYLent: "0",
BTCLent: "1.2",
JPYDebt: "0",
BTCDebt: "0",
JPYTsumitate: "10000.0",
BTCTsumitate: "0.43034",
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Fatal(err)
}
}))
client, err := NewClient(
WithBaseURL(testServer.URL),
WithCredentials("api_key", "api_secret"),
)
if err != nil {
t.Fatal(err)
}
got, err := client.GetAccountsBalance(context.Background())
if err != nil {
t.Fatal(err)
}
want := &GetAccountsBalanceResponse{
Success: true,
JPY: "0.8401",
BTC: "7.75052654",
JPYReserved: "3000.0",
BTCReserved: "3.5002",
JPYLendInUse: "1.1",
BTCLendInUse: "0.3",
JPYLent: "0",
BTCLent: "1.2",
JPYDebt: "0",
BTCDebt: "0",
JPYTsumitate: "10000.0",
BTCTsumitate: "0.43034",
}
if diff := cmp.Diff(want, got); diff != "" {
printDiff(t, diff)
}
})
t.Run("GetBalance returns an error if the server returns an error", func(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
client, err := NewClient(
WithBaseURL(testServer.URL),
WithCredentials("api_key", "api_secret"),
)
if err != nil {
t.Fatal(err)
}
_, err = client.GetAccountsBalance(context.Background())
if err == nil {
t.Fatal("GetBalance did not return an error")
}
})
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.GetAccountsBalance(context.Background())
if !errors.Is(err, ErrNoCredentials) {
t.Errorf("error is not ErrNoCredentials: %v", err)
}
})
}