forked from fabioberger/coinbase-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_api_test.go
255 lines (228 loc) · 7.27 KB
/
mock_api_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
package coinbase
import (
"log"
"os"
"testing"
)
// Initialize the client with mock mode enabled on rpc
// All calls return the corresponding json response from the test_data files
func initTestClient() Client {
return apiKeyClientTest(os.Getenv("COINBASE_KEY"), os.Getenv("COINBASE_SECRET"))
}
// About Mock Tests:
// All Mock Tests simulate requests to the coinbase API by returning the expected
// return values from a file under the test_data folder. The values received from
// the file are compared with the expected value given the marshaling of the JSON
// executed correctly.
func TestMockGetBalanceParse(t *testing.T) {
c := initTestClient()
amount, err := c.GetBalance()
if err != nil {
log.Fatal(err)
}
compareFloat(t, "GetBalanceParse", 36.62800000, amount)
}
func TestMockGetReceiveAddressParse(t *testing.T) {
c := initTestClient()
params := &AddressParams{}
address, err := c.GenerateReceiveAddress(params)
if err != nil {
log.Fatal(err)
}
compareString(t, "GetReceiveAddressParse", "muVu2JZo8PbewBHRp6bpqFvVD87qvqEHWA", address)
}
func TestMockGetAllAddressesParse(t *testing.T) {
c := initTestClient()
params := &AddressesParams{}
addresses, err := c.GetAllAddresses(params)
if err != nil {
log.Fatal(err)
}
compareString(t, "GetAllAddressesParse", "2013-05-09T23:07:08-07:00", addresses.Addresses[0].CreatedAt)
compareString(t, "GetAllAddressesParse", "mwigfecvyG4MZjb6R5jMbmNcs7TkzhUaCj", addresses.Addresses[1].Address)
compareInt(t, "GetAllAddressesParse", 1, int64(addresses.NumPages))
}
func TestMockCreateButtonParse(t *testing.T) {
c := initTestClient()
params := &Button{}
data, err := c.CreateButton(params)
if err != nil {
log.Fatal(err)
}
compareString(t, "CreateButtonParse", "93865b9cae83706ae59220c013bc0afd", data.Code)
compareString(t, "CreateButtonParse", "Sample description", data.Description)
}
func TestMockSendMoneyParse(t *testing.T) {
c := initTestClient()
params := &TransactionParams{}
data, err := c.SendMoney(params)
if err != nil {
log.Fatal(err)
}
compareString(t, "SendMoneyParse", "-1.23400000", data.Transaction.Amount.Amount)
compareString(t, "SendMoneyParse", "37muSN5ZrukVTvyVh3mT5Zc5ew9L9CBare", data.Transaction.RecipientAddress)
}
func TestMockRequestMoneyParse(t *testing.T) {
c := initTestClient()
params := &TransactionParams{}
data, err := c.RequestMoney(params)
if err != nil {
log.Fatal(err)
}
compareString(t, "RequestMoneyParse", "1.23400000", data.Transaction.Amount.Amount)
compareString(t, "RequestMoneyParse", "5011f33df8182b142400000e", data.Transaction.Recipient.Id)
}
func TestMockResendRequestParse(t *testing.T) {
c := initTestClient()
data, err := c.ResendRequest("ID")
if err != nil {
log.Fatal(err)
}
compareBool(t, "ResendRequestParse", true, data)
}
func TestMockCancelRequestParse(t *testing.T) {
c := initTestClient()
data, err := c.CancelRequest("ID")
if err != nil {
log.Fatal(err)
}
compareBool(t, "CancelRequestParse", false, data)
}
func TestMockCompleteRequestParse(t *testing.T) {
c := initTestClient()
data, err := c.CompleteRequest("ID")
if err != nil {
log.Fatal(err)
}
compareString(t, "CancelRequestParse", "503c46a3f8182b106500009b", data.Transaction.Id)
}
func TestMockCreateOrderFromButtonCodeParse(t *testing.T) {
c := initTestClient()
data, err := c.CreateOrderFromButtonCode("ID")
if err != nil {
log.Fatal(err)
}
compareString(t, "CreateOrderFromButtonCodeParse", "7RTTRDVP", data.Id)
compareString(t, "CreateOrderFromButtonCodeParse", "new", data.Status)
}
func TestMockCreateUserParse(t *testing.T) {
c := initTestClient()
data, err := c.CreateUser("[email protected]", "password")
if err != nil {
log.Fatal(err)
}
compareString(t, "CreateUser", "[email protected]", data.Email)
compareString(t, "CreateUser", "501a3d22f8182b2754000011", data.Id)
}
func TestMockBuyParse(t *testing.T) {
c := initTestClient()
data, err := c.Buy(1000.0, true)
if err != nil {
log.Fatal(err)
}
compareString(t, "Buys", "2013-01-28T16:08:58-08:00", data.CreatedAt)
compareString(t, "Buys", "USD", data.Fees.Bank.CurrencyIso)
compareString(t, "Buys", "13.55", data.Subtotal.Amount)
}
func TestMockSellParse(t *testing.T) {
c := initTestClient()
data, err := c.Sell(1000.0)
if err != nil {
log.Fatal(err)
}
compareString(t, "Sells", "2013-01-28T16:32:35-08:00", data.CreatedAt)
compareString(t, "Sells", "USD", data.Fees.Bank.CurrencyIso)
compareString(t, "Sells", "13.50", data.Subtotal.Amount)
}
func TestMockGetContactsParse(t *testing.T) {
c := initTestClient()
params := &ContactsParams{
Page: 1,
Limit: 5,
}
data, err := c.GetContacts(params)
if err != nil {
log.Fatal(err)
}
compareString(t, "GetContacts", "[email protected]", data.Emails[0])
}
func TestMockGetTransactionsParse(t *testing.T) {
c := initTestClient()
data, err := c.GetTransactions(1)
if err != nil {
log.Fatal(err)
}
compareInt(t, "GetTransactions", 2, data.TotalCount)
compareString(t, "GetTransactions", "5018f833f8182b129c00002f", data.Transactions[0].Id)
compareString(t, "GetTransactions", "-1.00000000", data.Transactions[1].Amount.Amount)
}
func TestMockGetOrdersParse(t *testing.T) {
c := initTestClient()
data, err := c.GetOrders(1)
if err != nil {
log.Fatal(err)
}
compareString(t, "GetOrders", "buy_now", data.Orders[0].Button.Type)
compareInt(t, "GetOrders", int64(0), int64(data.Orders[0].Transaction.Confirmations))
}
func TestMockGetTransfersParse(t *testing.T) {
c := initTestClient()
data, err := c.GetTransfers(1)
if err != nil {
log.Fatal(err)
}
compareString(t, "GetTransfers", "BTC", data.Transfers[0].Btc.Currency)
compareInt(t, "GetTransfers", int64(1), int64(data.NumPages))
}
func TestMockGetTransaction(t *testing.T) {
c := initTestClient()
data, err := c.GetTransaction("ID")
if err != nil {
log.Fatal(err)
}
compareString(t, "GetTransaction", "5018f833f8182b129c00002f", data.Id)
compareString(t, "GetTransaction", "BTC", data.Amount.Currency)
compareString(t, "GetTransaction", "User One", data.Recipient.Name)
}
func TestMockGetOrder(t *testing.T) {
c := initTestClient()
data, err := c.GetOrder("ID")
if err != nil {
log.Fatal(err)
}
compareString(t, "GetTransaction", "A7C52JQT", data.Id)
compareString(t, "GetTransaction", "BTC", data.TotalBtc.CurrencyIso)
compareString(t, "GetTransaction", "test", data.Button.Name)
}
func TestMockGetUser(t *testing.T) {
c := initTestClient()
data, err := c.GetUser()
if err != nil {
log.Fatal(err)
}
compareString(t, "GetTransaction", "512db383f8182bd24d000001", data.Id)
compareString(t, "GetTransaction", "49.76000000", data.Balance.Amount)
compareString(t, "GetTransaction", "Company Name, Inc.", data.Merchant.CompanyName)
}
func compareFloat(t *testing.T, prefix string, expected float64, got float64) {
if expected != got {
t.Errorf(`%s
Expected %0.32f
but got %0.32f`, prefix, expected, got)
}
}
func compareInt(t *testing.T, prefix string, expected int64, got int64) {
if expected != got {
t.Errorf("%s Expected %d but got %d", prefix, expected, got)
}
}
func compareString(t *testing.T, prefix string, expected string, got string) {
if expected != got {
t.Errorf("%s Expected '%s' but got '%s'", prefix, expected, got)
}
}
func compareBool(t *testing.T, prefix string, expected bool, got bool) {
if expected != got {
t.Errorf("%s Expected '%t' but got '%t'", prefix, expected, got)
}
}