-
Notifications
You must be signed in to change notification settings - Fork 142
/
krakenapi_test.go
172 lines (140 loc) · 4.07 KB
/
krakenapi_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
package krakenapi
import (
"encoding/base64"
"net/url"
"reflect"
"testing"
)
var publicAPI = New("", "")
func TestKrakenApi(t *testing.T) {
var kk interface{} = KrakenApi{
key: "key",
secret: "secret",
}
name := reflect.TypeOf(kk).Name()
if name != "KrakenAPI" {
t.Errorf("Unexpected struct, got %s want %s", name, "KrakenAPI")
}
}
func TestCreateSignature(t *testing.T) {
expectedSig := "Uog0MyIKZmXZ4/VFOh0g1u2U+A0ohuK8oCh0HFUiHLE2Csm23CuPCDaPquh/hpnAg/pSQLeXyBELpJejgOftCQ=="
urlPath := "/0/private/"
secret, _ := base64.StdEncoding.DecodeString("SECRET")
values := url.Values{
"TestKey": {"TestValue"},
}
sig := createSignature(urlPath, values, secret)
if sig != expectedSig {
t.Errorf("Expected Signature to be %s, got: %s\n", expectedSig, sig)
}
}
func TestTime(t *testing.T) {
resp, err := publicAPI.Time()
if err != nil {
t.Errorf("Time() should not return an error, got %s", err)
}
if resp.Unixtime <= 0 {
t.Errorf("Time() should return valid Unixtime, got %d", resp.Unixtime)
}
}
func TestAssets(t *testing.T) {
_, err := publicAPI.Assets()
if err != nil {
t.Errorf("Assets() should not return an error, got %s", err)
}
}
func TestAssetPairs(t *testing.T) {
resp, err := publicAPI.AssetPairs()
if err != nil {
t.Errorf("AssetPairs() should not return an error, got %s", err)
}
if resp.XXBTZEUR.Base+resp.XXBTZEUR.Quote != XXBTZEUR {
t.Errorf("AssetPairs() should return valid response, got %+v", resp.XXBTZEUR)
}
}
func TestTicker(t *testing.T) {
resp, err := publicAPI.Ticker(XXBTZEUR, XXRPZEUR)
if err != nil {
t.Errorf("Ticker() should not return an error, got %s", err)
}
if resp.XXBTZEUR.OpeningPrice == 0 {
t.Errorf("Ticker() should return valid OpeningPrice, got %+v", resp.XXBTZEUR.OpeningPrice)
}
}
func TestOHLCWithInterval(t *testing.T) {
resp, err := publicAPI.OHLCWithInterval(XXBTZEUR, "15")
if err != nil {
t.Errorf("OHLCWithInterval() should not return an error, got %s", err)
}
if resp.Pair == "" {
t.Errorf("OHLCWithInterval() should return valid Pair, got %+v", resp.Pair)
}
}
func TestOHLC(t *testing.T) {
resp, err := publicAPI.OHLC(XXBTZEUR)
if err != nil {
t.Errorf("OHLC() should not return an error, got %s", err)
}
if resp.Pair == "" {
t.Errorf("OHLC() should return valid Pair, got %+v", resp.Pair)
}
}
func TestQueryTime(t *testing.T) {
result, err := publicAPI.Query("Time", map[string]string{})
resultKind := reflect.TypeOf(result).Kind()
if err != nil {
t.Errorf("Query should not return an error, got %s", err)
}
if resultKind != reflect.Map {
t.Errorf("Query `Time` should return a Map, got: %s", resultKind)
}
}
func TestQueryTicker(t *testing.T) {
result, err := publicAPI.Query("Ticker", map[string]string{
"pair": "XXBTZEUR",
})
resultKind := reflect.TypeOf(result).Kind()
if err != nil {
t.Errorf("Query should not return an error, got %s", err)
}
if resultKind != reflect.Map {
t.Errorf("Query `Ticker` should return a Map, got: %s", resultKind)
}
}
func TestQueryTrades(t *testing.T) {
result, err := publicAPI.Trades(XXBTZEUR, 1495777604391411290)
if err != nil {
t.Errorf("Trades should not return an error, got %s", err)
}
if result.Last == 0 {
t.Errorf("Returned parameter `last` should always have a value...")
}
if len(result.Trades) > 0 {
for _, trade := range result.Trades {
if trade.Buy == trade.Sell {
t.Errorf("Trade should be buy or sell")
}
if trade.Market == trade.Limit {
t.Errorf("Trade type should be market or limit")
}
}
}
}
func TestQueryDepth(t *testing.T) {
pair := "XETHZEUR"
count := 10
result, err := publicAPI.Depth(pair, count)
if err != nil {
t.Errorf("Depth should not return an error, got %s", err)
}
resultType := reflect.TypeOf(result)
if resultType != reflect.TypeOf(&OrderBook{}) {
t.Errorf("Depth should return an OrderBook, got %s", resultType)
}
if len(result.Asks) > count {
t.Errorf("Asks length must be less than count , got %d > %d", len(result.Asks), count)
}
if len(result.Bids) > count {
t.Errorf("Bids length must be less than count , got %d > %d", len(result.Bids), count)
}
}