-
Notifications
You must be signed in to change notification settings - Fork 1
/
viewquery_options_test.go
204 lines (171 loc) · 4.68 KB
/
viewquery_options_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
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
package gocb
import (
"fmt"
"math/rand"
"net/url"
"testing"
)
func TestViewQueryOptionsToURLValues(t *testing.T) {
for i := 0; i < 50; i++ {
opts := testCreateViewQueryOptions(int64(i))
optValues, err := opts.toURLValues()
if opts.ScanConsistency > ViewScanConsistencyUpdateAfter || opts.ScanConsistency < 0 {
if err == nil {
t.Fatalf("Expected an error for invalid stale value")
} else {
continue
}
}
if opts.Order > ViewOrderingDescending || opts.Order < 0 {
if err == nil {
t.Fatalf("Expected an error for invalid order value")
} else {
continue
}
}
if err != nil {
t.Fatalf("Expected no error but was %v", err)
}
if opts.ScanConsistency == 0 {
testAssertViewOption(t, "", "stale", optValues)
} else if opts.ScanConsistency == ViewScanConsistencyRequestPlus {
testAssertViewOption(t, "false", "stale", optValues)
} else if opts.ScanConsistency == ViewScanConsistencyNotBounded {
testAssertViewOption(t, "ok", "stale", optValues)
} else if opts.ScanConsistency == ViewScanConsistencyUpdateAfter {
testAssertViewOption(t, "update_after", "stale", optValues)
}
if opts.Skip == 0 {
testAssertViewOption(t, "", "skip", optValues)
} else {
testAssertViewOption(t, fmt.Sprintf("%d", opts.Skip), "skip", optValues)
}
if opts.Limit == 0 {
testAssertViewOption(t, "", "limit", optValues)
} else {
testAssertViewOption(t, fmt.Sprintf("%d", opts.Limit), "limit", optValues)
}
if opts.Order == 0 {
testAssertViewOption(t, "", "descending", optValues)
} else if opts.Order == ViewOrderingAscending {
testAssertViewOption(t, "false", "descending", optValues)
} else if opts.Order == ViewOrderingDescending {
testAssertViewOption(t, "true", "descending", optValues)
}
if opts.Reduce {
testAssertViewOption(t, "true", "reduce", optValues)
if opts.Group {
testAssertViewOption(t, "true", "group", optValues)
} else {
testAssertViewOption(t, "false", "group", optValues)
}
if opts.GroupLevel == 0 {
testAssertViewOption(t, "", "group_level", optValues)
} else {
testAssertViewOption(t, fmt.Sprintf("%d", opts.GroupLevel), "group_level", optValues)
}
} else {
testAssertViewOption(t, "false", "reduce", optValues)
testAssertViewOption(t, "", "group", optValues)
testAssertViewOption(t, "", "group_level", optValues)
}
if opts.Key == nil {
testAssertViewOption(t, "", "key", optValues)
} else {
testAssertViewOption(t, "[\"key1\"]\n", "key", optValues)
}
if len(opts.Keys) == 0 {
testAssertViewOption(t, "", "keys", optValues)
} else {
testAssertViewOption(t, "[\"key2\",\"key3\",{\"key\":\"key4\"}]\n", "keys", optValues)
}
}
}
func testAssertViewOption(t *testing.T, expected string, key string, optValues *url.Values) {
val := optValues.Get(key)
if val != expected {
t.Fatalf("Values had incorrect %s, expected %s but was %s", key, expected, val)
}
}
func testCreateViewQueryOptions(seed int64) *ViewOptions {
opts := &ViewOptions{}
rand.Seed(seed)
randVal := rand.Intn(6)
if randVal == 1 {
opts.ScanConsistency = ViewScanConsistencyRequestPlus
} else if randVal == 2 {
opts.ScanConsistency = ViewScanConsistencyNotBounded
} else if randVal == 3 {
opts.ScanConsistency = ViewScanConsistencyUpdateAfter
} else if randVal == 4 {
opts.ScanConsistency = 5
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Skip = uint(rand.Intn(10))
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Limit = uint(rand.Intn(10))
}
randVal = rand.Intn(4)
if randVal == 1 {
opts.Order = ViewOrderingAscending
} else if randVal == 2 {
opts.Order = ViewOrderingDescending
} else if randVal == 3 {
opts.Order = 3
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Reduce = true
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Group = true
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.GroupLevel = uint(rand.Intn(5))
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Key = []string{"key1"}
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Keys = []interface{}{"key2", "key3", struct {
Key string `json:"key"`
}{"key4"}}
}
randVal = rand.Intn(2)
if randVal == 1 {
randVal = rand.Intn(2)
if randVal == 1 {
opts.StartKey = "keystart"
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.EndKey = "keyend"
}
randVal = rand.Intn(3)
if randVal == 1 {
opts.InclusiveEnd = true
} else if randVal == 2 {
opts.InclusiveEnd = false
}
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.StartKeyDocID = "rangeStart"
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.EndKeyDocID = "rangeEnd"
}
randVal = rand.Intn(2)
if randVal == 1 {
opts.Raw = map[string]string{"key1": "param1", "$key2": "param2"}
}
return opts
}