This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
forked from onuryilmaz/go-wex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade_test.go
226 lines (174 loc) · 5.91 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
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
package wex
import (
"os"
"strconv"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
const SLEEP = 5
var tapi = TradeAPI{}
func CheckAPI(t *testing.T) {
time.Sleep(SLEEP * time.Second)
if os.Getenv("API_KEY_TEST") == "" || os.Getenv("API_SECRET_TEST") == "" {
t.SkipNow()
}
}
func TestAccountInfo(t *testing.T) {
CheckAPI(t)
Convey("Account information data", t, func() {
info, err := tapi.GetInfoAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"))
Convey("No error should occur", func() {
So(err, ShouldBeNil)
})
Convey("Account information fields should be returned", func() {
So(info, ShouldHaveSameTypeAs, AccountInfo{})
So(info.TransactionCount, ShouldEqual, 0)
So(info.ServerTime, ShouldBeGreaterThan, 0)
So(info.Funds["btc"], ShouldBeGreaterThanOrEqualTo, 0)
})
})
}
func TestActiveOrders(t *testing.T) {
CheckAPI(t)
Convey("Active orders data", t, func() {
orders, err := tapi.ActiveOrdersAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), "btc_usd")
if err != nil {
Convey("If error is returned, it should be 'no orders'", func() {
So(err, ShouldResemble, TradeError{msg: "no orders"})
})
} else {
Convey("If no error is returned, 'order' should have length", func() {
So(len(orders), ShouldBeGreaterThanOrEqualTo, 1)
})
}
})
}
func TestOrderTrade(t *testing.T) {
CheckAPI(t)
Convey("Trade new order", t, func() {
orderResponse, err := tapi.TradeAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), "btc_usd", "buy", 900, 1)
if err != nil {
Convey("If error is returned, it should be 'not enough USD'", func() {
So(err, ShouldResemble, TradeError{msg: "It is not enough USD for purchase"})
})
} else {
Convey("If no error is returned, 'btc_usd' amount should appear", func() {
So(orderResponse.OrderID, ShouldBeGreaterThan, 0)
})
}
})
}
func TestOrderInfo(t *testing.T) {
CheckAPI(t)
orderID := "1"
Convey("Get order info", t, func() {
orderResponse, err := tapi.OrderInfoAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), orderID)
if err != nil {
Convey("If error is returned, it should be 'invalid order'", func() {
So(err, ShouldResemble, TradeError{msg: "invalid order"})
})
} else {
Convey("If no error is returned, order information should be returned", func() {
So(orderResponse[orderID], ShouldNotBeNil)
So(orderResponse[orderID].Amount, ShouldBeGreaterThanOrEqualTo, 0)
})
}
})
}
func TestCancelOrder(t *testing.T) {
CheckAPI(t)
orderID := "1"
Convey("Cancel order", t, func() {
orderResponse, err := tapi.CancelOrderAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), orderID)
if err != nil {
Convey("If error is returned, it should be 'bad status'", func() {
So(err, ShouldResemble, TradeError{msg: "bad status"})
})
} else {
Convey("If no error is returned, same order id should be returned", func() {
So(strconv.Itoa(orderResponse.OrderID), ShouldEqual, orderID)
})
}
})
}
func TestTradeHistory(t *testing.T) {
CheckAPI(t)
Convey("Trade history data", t, func() {
filter := HistoryFilter{}
tradeHistory, err := tapi.TradeHistoryAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), filter, "btc_usd")
if err != nil {
Convey("If error is returned, it should be 'no trades'", func() {
So(err, ShouldResemble, TradeError{msg: "no trades"})
})
}
Convey("Trade history should be retrieved", func() {
So(tradeHistory, ShouldNotBeNil)
So(len(tradeHistory), ShouldBeGreaterThanOrEqualTo, 0)
})
})
}
func TestTransactionHistory(t *testing.T) {
CheckAPI(t)
Convey("Transaction history data", t, func() {
filter := HistoryFilter{}
transactionHistory, err := tapi.TransactionHistoryAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), filter)
if err != nil {
Convey("If error is returned, it should be 'no transactions'", func() {
So(err, ShouldResemble, TradeError{msg: "no transactions"})
})
}
Convey("Transaction history should be retrieved", func() {
So(transactionHistory, ShouldNotBeNil)
So(len(transactionHistory), ShouldBeGreaterThanOrEqualTo, 0)
})
})
}
func TestWithdrawCoin(t *testing.T) {
CheckAPI(t)
Convey("Withdraw coin", t, func() {
response, err := tapi.WithdrawCoinAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), "BTC", 0.001, "address")
if err != nil {
Convey("If error is returned, it should be 'api permission'", func() {
So(err, ShouldResemble, TradeError{msg: "api key dont have withdraw permission"})
})
} else {
Convey("If no error is returned, withdraw response should be returned", func() {
So(response.TransactionID, ShouldBeGreaterThan, 0)
So(response.AmountSent, ShouldBeGreaterThanOrEqualTo, 0)
})
}
})
}
func TestCreateCoupon(t *testing.T) {
CheckAPI(t)
Convey("Create coupon", t, func() {
response, err := tapi.CreateCouponAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), "BTC", 0.001)
if err != nil {
Convey("If error is returned, it should be 'api permission'", func() {
So(err, ShouldResemble, TradeError{msg: "api key dont have coupon permission"})
})
} else {
Convey("If no error is returned, withdraw response should be returned", func() {
So(response.Coupon, ShouldNotBeBlank)
So(response.TransactionID, ShouldBeGreaterThan, 0)
})
}
})
}
func TestRedeemCoupon(t *testing.T) {
CheckAPI(t)
Convey("Redeem coupon", t, func() {
response, err := tapi.RedeemCouponAuth(os.Getenv("API_KEY_TEST"), os.Getenv("API_SECRET_TEST"), "BTC-USD-XYZ")
if err != nil {
Convey("If error is returned, it should be 'api permission'", func() {
So(err, ShouldResemble, TradeError{msg: "api key dont have coupon permission"})
})
} else {
Convey("If no error is returned, withdraw response should be returned", func() {
So(response.CouponCurrency, ShouldNotBeBlank)
So(response.TransactionID, ShouldBeGreaterThan, 0)
})
}
})
}