-
Notifications
You must be signed in to change notification settings - Fork 145
/
test_helper.go
87 lines (66 loc) · 1.91 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
package coinbasepro
import (
"fmt"
"reflect"
ws "github.com/gorilla/websocket"
)
func NewTestClient() *Client {
client := NewClient()
client.UpdateConfig(&ClientConfig{
BaseURL: "https://api-public.sandbox.pro.coinbase.com",
})
client.RetryCount = 2
return client
}
func NewTestWebsocketClient() (*ws.Conn, error) {
var wsDialer ws.Dialer
wsConn, _, err := wsDialer.Dial("wss://ws-feed-public.sandbox.pro.coinbase.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, fmt.Errorf(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 fmt.Errorf(fmt.Sprintf("Slice is zero"))
}
default:
if reflect.Zero(field.Type()).Interface() == field.Interface() {
return fmt.Errorf(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 fmt.Errorf(fmt.Sprintf("%s: %s", err.Error(), property))
}
}
return nil
}