forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
140 lines (118 loc) · 3.43 KB
/
utils_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
package gocb
import (
"encoding/json"
"io"
"io/ioutil"
"testing"
"time"
"github.com/pkg/errors"
)
type testBeerDocument struct {
ABV float32 `json:"abv,omitempty"`
BreweryID string `json:"brewery_id,omitempty"`
Category string `json:"category,omitempty"`
Description string `json:"description,omitempty"`
IBU int `json:"IBU,omitempty"`
Name string `json:"name,omitempty"`
SRM int `json:"srm,omitempty"`
Style string `json:"style,omitempty"`
Type string `json:"type,omitempty"`
UPC int `json:"upc,omitempty"`
Updated string `json:"updated,omitempty"`
}
type testBreweryGeo struct {
Accuracy string `json:"accuracy,omitempty"`
Lat float32 `json:"lat,omitempty"`
Lon float32 `json:"lon,omitempty"`
}
type testBreweryDocument struct {
City string `json:"city,omitempty"`
Code string `json:"code,omitempty"`
Country string `json:"country,omitempty"`
Description string `json:"description,omitempty"`
Geo testBreweryGeo `json:"geo,omitempty"`
Name string `json:"name,omitempty"`
Phone string `json:"phone,omitempty"`
State string `json:"state,omitempty"`
Type string `json:"type,omitempty"`
Updated string `json:"updated,omitempty"`
Website string `json:"website,omitempty"`
}
type testMetadata struct {
}
func loadRawTestDataset(dataset string) ([]byte, error) {
return ioutil.ReadFile("testdata/" + dataset + ".json")
}
func loadJSONTestDataset(dataset string, valuePtr interface{}) error {
bytes, err := loadRawTestDataset(dataset)
if err != nil {
return err
}
err = json.Unmarshal(bytes, &valuePtr)
if err != nil {
return err
}
return nil
}
func loadSDKTestDataset(dataset string) (*testMetadata, []byte, error) {
var testdata map[string]interface{}
err := loadJSONTestDataset("sdk-testcases/"+dataset, &testdata)
if err != nil {
return nil, nil, err
}
_, ok := testdata["metadata"]
if !ok {
return nil, nil, errors.New("test dataset missing metadata")
}
data, ok := testdata["data"]
if !ok {
return nil, nil, errors.New("test dataset missing data")
}
b, err := json.Marshal(data)
if err != nil {
return nil, nil, errors.Wrap(err, "could not remarshal test data")
}
return nil, b, nil
}
func marshal(t *testing.T, value interface{}) []byte {
b, err := json.Marshal(value)
if err != nil {
t.Fatalf("Could not marshal value: %v", err)
}
return b
}
type testReadCloser struct {
io.Reader
closeErr error
}
func (trc *testReadCloser) Close() error {
return trc.closeErr
}
// Not a test, just gets a collection instance.
func testGetCollection(t *testing.T, provider *mockKvProvider) *Collection {
clients := make(map[string]client)
cli := &mockClient{
bucketName: "mock",
collectionId: 0,
scopeId: 0,
useMutationTokens: true,
mockKvProvider: provider,
}
clients["mock"] = cli
b := &Bucket{
sb: stateBlock{
clientStateBlock: clientStateBlock{
BucketName: "mock",
},
cachedClient: cli,
AnalyticsTimeout: 75000 * time.Millisecond,
QueryTimeout: 75000 * time.Millisecond,
SearchTimeout: 75000 * time.Millisecond,
ViewTimeout: 75000 * time.Millisecond,
KvTimeout: 2500 * time.Millisecond,
Transcoder: NewJSONTranscoder(&DefaultJSONSerializer{}),
},
}
col := b.DefaultCollection()
return col
}