-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_common.go
295 lines (268 loc) · 5.97 KB
/
test_common.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package arangomanager
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
driver "github.com/arangodb/go-driver"
)
const (
genderQ = `
FOR d IN @@collection
FILTER d.gender == @gender
RETURN d
`
genderQNoParam = `
FOR d IN %s
FILTER d.gender == '%s'
RETURN d
`
userQ = `
FOR d in @@collection
FILTER d.name.first == @first
FILTER d.name.last == @last
RETURN d
`
userIns = `
INSERT {
name: {
first: @first,
last: @last
},
gender: @gender,
contact: {
region: @region,
address: {
city: @city,
state: @state,
zip: @zip
}
}
} INTO %s
`
aPort = 8529
minLen = 10
maxLen = 15
)
func randomIntInRange(min, max int) (int, error) {
if min >= max {
return 0, fmt.Errorf("Invalid range")
}
// Calculate the number of possible values within the range
possibleValues := big.NewInt(int64(max - min))
// Generate a random number using crypto/rand
randomValue, err := rand.Int(rand.Reader, possibleValues)
if err != nil {
return 0, err
}
// Add the minimum value to the random number
return min + int(randomValue.Int64()), nil
}
// Generate a random number using crypto/rand.
func RandomInt(num int) (int, error) {
randomValue, err := rand.Int(rand.Reader, big.NewInt(int64(num)))
if err != nil {
return 0, err
}
return int(randomValue.Int64()), nil
}
func FixedLenRandomString(length int) string {
alphanum := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
byt := make([]byte, 0)
alen := len(alphanum)
for i := 0; i < length; i++ {
pos, _ := RandomInt(alen)
byt = append(byt, alphanum[pos])
}
return string(byt)
}
// Generates a random string between a range(min and max) of length.
func RandomString(min, max int) string {
alphanum := []byte("abcdefghijklmnopqrstuvwxyz")
size, _ := randomIntInRange(min, max)
byt := make([]byte, size)
alen := len(alphanum)
for i := 0; i < size; i++ {
pos, _ := RandomInt(alen)
byt[i] = alphanum[pos]
}
return string(byt)
}
type testArango struct {
*ConnectParams
*Session
}
type testUserDb struct {
driver.DocumentMeta
Birthday *time.Time `json:"birthday"`
Contact struct {
Address struct {
City string `json:"city"`
State string `json:"state"`
Street string `json:"street"`
Zip string `json:"zip"`
} `json:"address"`
Email []string `json:"email"`
Phone []string `json:"phone"`
Region string `json:"region"`
} `json:"contact"`
Gender string `json:"gender"`
Likes []string `json:"likes"`
MemberSince *time.Time `json:"memberSince"`
Name struct {
First string `json:"first"`
Last string `json:"last"`
} `json:"name"`
}
type testUser struct {
driver.DocumentMeta
Birthday *userDate `json:"birthday"`
Contact struct {
Address struct {
City string `json:"city"`
State string `json:"state"`
Street string `json:"street"`
Zip string `json:"zip"`
} `json:"address"`
Email []string `json:"email"`
Phone []string `json:"phone"`
Region string `json:"region"`
} `json:"contact"`
Gender string `json:"gender"`
Likes []string `json:"likes"`
MemberSince *userDate `json:"memberSince"`
Name struct {
First string `json:"first"`
Last string `json:"last"`
} `json:"name"`
}
type userDate struct {
time.Time
}
func (ud *userDate) UnmarshalJSON(in []byte) error {
t, err := time.Parse("2006-01-02", strings.Trim(string(in), `"`))
if err != nil {
return fmt.Errorf("error in parsing time %s", err)
}
ud.Time = t
return nil
}
func checkArangoEnv() error {
envs := []string{
"ARANGO_USER",
"ARANGO_HOST",
"ARANGO_PASS",
}
for _, e := range envs {
if len(os.Getenv(e)) == 0 {
return fmt.Errorf("env %s is not set", e)
}
}
return nil
}
func teardown(t *testing.T, c driver.Collection) {
t.Helper()
if err := c.Remove(context.Background()); err != nil {
t.Fatalf("unable to truncate collection %s %s", c.Name(), err)
}
}
func setup(t *testing.T, db *Database) driver.Collection {
t.Helper()
coll, err := db.FindOrCreateCollection(
RandomString(minLen, maxLen),
&driver.CreateCollectionOptions{},
)
if err != nil {
t.Fatal(err)
}
if err = loadTestData(coll); err != nil {
t.Fatal(err)
}
return coll
}
func newTestArangoFromEnv(isCreate bool) (*testArango, error) {
tra := new(testArango)
if err := checkArangoEnv(); err != nil {
return tra, err
}
tra.ConnectParams = &ConnectParams{
User: os.Getenv("ARANGO_USER"),
Pass: os.Getenv("ARANGO_PASS"),
Host: os.Getenv("ARANGO_HOST"),
Port: aPort,
}
if len(os.Getenv("ARANGO_PORT")) > 0 {
aport, _ := strconv.Atoi(os.Getenv("ARANGO_PORT"))
tra.ConnectParams.Port = aport
}
sess, err := Connect(
tra.ConnectParams.Host,
tra.ConnectParams.User,
tra.ConnectParams.Pass,
tra.ConnectParams.Port,
false,
)
if err != nil {
return tra, err
}
tra.Session = sess
tra.Database = RandomString(minLen, maxLen)
if isCreate {
if err := sess.CreateDB(tra.Database, &driver.CreateDatabaseOptions{}); err != nil {
return tra, err
}
}
return tra, nil
}
func getReader() (io.Reader, error) {
buff := bytes.NewBuffer(make([]byte, 0))
dir, err := os.Getwd()
if err != nil {
return buff, fmt.Errorf("unable to get current dir %s", err)
}
fhr, err := os.Open(
filepath.Join(
dir, "testdata", "names.json",
),
)
if err != nil {
return fhr, fmt.Errorf("error in opening file %s", err)
}
return fhr, nil
}
func loadTestData(coll driver.Collection) error {
reader, err := getReader()
if err != nil {
return err
}
dec := json.NewDecoder(reader)
var ausr []*testUser
for {
var usr *testUser
if err := dec.Decode(&usr); err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("error in decoding %s", err)
}
ausr = append(ausr, usr)
}
_, err = coll.ImportDocuments(
context.Background(),
ausr,
&driver.ImportDocumentOptions{Complete: true},
)
if err != nil {
return fmt.Errorf("error in importing document %s", err)
}
return nil
}