-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade_test.go
125 lines (113 loc) · 2.8 KB
/
trade_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
package coincheck
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestClient_GetTrades(t *testing.T) {
t.Run("In the case of a successful GET /api/trades request", func(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wantMethod := http.MethodGet
if got := r.Method; got != wantMethod {
t.Errorf("Method: got %v, want %v", got, wantMethod)
}
wantEndpoint := "/api/trades"
if got := r.URL.Path; got != wantEndpoint {
t.Errorf("Endpoint: got %v, want %v", got, wantEndpoint)
}
wantPair := PairETCJPY
if got := r.URL.Query().Get("pair"); got != wantPair.String() {
t.Errorf("Pair: got %v, want %v", got, wantPair)
}
result := GetTradesResponse{
Success: true,
Pagination: Pagination{
Limit: 1,
PaginationOrder: "desc",
StartingAfter: 0,
EndingBefore: 0,
},
Data: []Trade{
{
ID: 1,
Amount: 1,
Rate: 1000000,
Pair: PairETCJPY,
OrderType: OrderTypeBuy,
CreatedAt: "2021-01-01T00:00:00Z",
},
{
ID: 2,
Amount: 2,
Rate: 2000000,
Pair: PairETCJPY,
OrderType: OrderTypeSell,
CreatedAt: "2021-01-02T00:00:00Z",
},
},
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Fatal(err)
}
}))
client, err := NewClient(WithBaseURL(testServer.URL))
if err != nil {
t.Fatal(err)
}
input := GetTradesInput{
Pair: PairETCJPY,
}
got, err := client.GetTrades(context.Background(), input)
if err != nil {
t.Fatal(err)
}
want := &GetTradesResponse{
Success: true,
Pagination: Pagination{
Limit: 1,
PaginationOrder: PaginationOrderDesc,
StartingAfter: 0,
EndingBefore: 0,
},
Data: []Trade{
{
ID: 1,
Amount: 1,
Rate: 1000000,
Pair: PairETCJPY,
OrderType: OrderTypeBuy,
CreatedAt: "2021-01-01T00:00:00Z",
},
{
ID: 2,
Amount: 2,
Rate: 2000000,
Pair: PairETCJPY,
OrderType: OrderTypeSell,
CreatedAt: "2021-01-02T00:00:00Z",
},
},
}
if diff := cmp.Diff(want, got); diff != "" {
printDiff(t, diff)
}
})
t.Run("In the case of a failed GET /api/trades request", 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))
if err != nil {
t.Fatal(err)
}
input := GetTradesInput{
Pair: PairETCJPY,
}
if _, err = client.GetTrades(context.Background(), input); err == nil {
t.Fatal("err must not be nil")
}
})
}