forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.go
96 lines (74 loc) · 2.11 KB
/
test_helper.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
package gdax
import (
"errors"
"fmt"
"net/http"
"os"
"reflect"
"time"
ws "github.com/gorilla/websocket"
)
func NewTestClient() *Client {
secret := os.Getenv("TEST_COINBASE_SECRET")
key := os.Getenv("TEST_COINBASE_KEY")
passphrase := os.Getenv("TEST_COINBASE_PASSPHRASE")
client := NewClient(secret, key, passphrase)
client.BaseURL = "https://api-public.sandbox.gdax.com"
client.HttpClient = &http.Client{
Timeout: 15 * time.Second,
}
client.RetryCount = 2
return client
}
func NewTestWebsocketClient() (*ws.Conn, error) {
var wsDialer ws.Dialer
wsConn, _, err := wsDialer.Dial("wss://ws-feed-public.sandbox.gdax.com", nil)
return wsConn, err
}
func StructHasZeroValues(i interface{}) bool {
iv := reflect.ValueOf(i)
//values := make([]interface{}, v.NumField())
for i := 0; i < iv.NumField(); i++ {
field := iv.Field(i)
if reflect.Zero(field.Type()).Interface() == field.Interface() {
return true
}
}
return false
}
func CompareProperties(a, b interface{}, properties []string) (bool, error) {
aValueOf := reflect.ValueOf(a)
bValueOf := reflect.ValueOf(b)
for _, property := range properties {
aValue := reflect.Indirect(aValueOf).FieldByName(property).Interface()
bValue := reflect.Indirect(bValueOf).FieldByName(property).Interface()
if aValue != bValue {
return false, errors.New(fmt.Sprintf("%s not equal: %s - %s", property, aValue, bValue))
}
}
return true, nil
}
func Ensure(a interface{}) error {
field := reflect.Indirect(reflect.ValueOf(a))
switch field.Kind() {
case reflect.Slice:
if reflect.ValueOf(field.Interface()).Len() == 0 {
return errors.New(fmt.Sprintf("Slice is zero"))
}
default:
if reflect.Zero(field.Type()).Interface() == field.Interface() {
return errors.New(fmt.Sprintf("Property is zero"))
}
}
return nil
}
func EnsureProperties(a interface{}, properties []string) error {
valueOf := reflect.ValueOf(a)
for _, property := range properties {
field := reflect.Indirect(valueOf).FieldByName(property)
if err := Ensure(field.Interface()); err != nil {
return errors.New(fmt.Sprintf("%s: %s", err.Error(), property))
}
}
return nil
}