-
Notifications
You must be signed in to change notification settings - Fork 44
/
bulk_test.go
233 lines (225 loc) · 6.4 KB
/
bulk_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
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
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package kivik
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"testing"
"gitlab.com/flimzy/testy"
"github.com/go-kivik/kivik/v4/driver"
internal "github.com/go-kivik/kivik/v4/int/errors"
"github.com/go-kivik/kivik/v4/int/mock"
)
func TestDocsInterfaceSlice(t *testing.T) {
type diTest struct {
name string
input []interface{}
expected interface{}
status int
err string
}
tests := []diTest{
{
name: "InterfaceSlice",
input: []interface{}{map[string]string{"foo": "bar"}},
expected: []interface{}{map[string]string{"foo": "bar"}},
},
{
name: "JSONDoc",
input: []interface{}{
map[string]string{"foo": "bar"},
json.RawMessage(`{"foo":"bar"}`),
},
expected: []interface{}{
map[string]string{"foo": "bar"},
map[string]string{"foo": "bar"},
},
},
}
for _, test := range tests {
func(test diTest) {
t.Run(test.name, func(t *testing.T) {
result, err := docsInterfaceSlice(test.input)
if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
t.Error(d)
}
if d := testy.DiffAsJSON(test.expected, result); d != nil {
t.Errorf("%s", d)
}
})
}(test)
}
}
func TestBulkDocs(t *testing.T) { // nolint: gocyclo
type tt struct {
db *DB
docs []interface{}
options Option
expected []BulkResult
status int
err string
}
tests := testy.NewTable()
tests.Add("invalid JSON", tt{
db: &DB{
client: &Client{},
driverDB: &mock.BulkDocer{
BulkDocsFunc: func(_ context.Context, docs []interface{}, _ driver.Options) ([]driver.BulkResult, error) {
_, err := json.Marshal(docs)
return nil, err
},
},
},
docs: []interface{}{json.RawMessage("invalid json")},
status: http.StatusInternalServerError,
err: "json: error calling MarshalJSON for type json.RawMessage: invalid character 'i' looking for beginning of value",
})
tests.Add("emulated BulkDocs support", tt{
db: &DB{
client: &Client{},
driverDB: &mock.DocCreator{
DB: mock.DB{
PutFunc: func(_ context.Context, docID string, doc interface{}, options driver.Options) (string, error) {
if docID == "error" {
return "", errors.New("error")
}
if docID != "foo" { // nolint: goconst
return "", fmt.Errorf("Unexpected docID: %s", docID)
}
expectedDoc := map[string]string{"_id": "foo"}
if d := testy.DiffInterface(expectedDoc, doc); d != nil {
return "", fmt.Errorf("Unexpected doc:\n%s", d)
}
gotOpts := map[string]interface{}{}
options.Apply(gotOpts)
if d := testy.DiffInterface(testOptions, gotOpts); d != nil {
return "", fmt.Errorf("Unexpected opts:\n%s", d)
}
return "2-xxx", nil // nolint: goconst
},
},
CreateDocFunc: func(_ context.Context, doc interface{}, options driver.Options) (string, string, error) {
gotOpts := map[string]interface{}{}
options.Apply(gotOpts)
expectedDoc := int(123)
if d := testy.DiffInterface(expectedDoc, doc); d != nil {
return "", "", fmt.Errorf("Unexpected doc:\n%s", d)
}
if d := testy.DiffInterface(testOptions, gotOpts); d != nil {
return "", "", fmt.Errorf("Unexpected opts:\n%s", d)
}
return "newDocID", "1-xxx", nil // nolint: goconst
},
},
},
docs: []interface{}{
map[string]string{"_id": "foo"},
123,
map[string]string{"_id": "error"},
},
options: Params(testOptions),
expected: []BulkResult{
{ID: "foo", Rev: "2-xxx"},
{ID: "newDocID", Rev: "1-xxx"},
{ID: "error", Error: errors.New("error")},
},
})
tests.Add("new_edits", tt{
db: &DB{
client: &Client{},
driverDB: &mock.BulkDocer{
BulkDocsFunc: func(_ context.Context, docs []interface{}, options driver.Options) ([]driver.BulkResult, error) {
expectedDocs := []interface{}{map[string]string{"_id": "foo"}, 123}
wantOpts := map[string]interface{}{"new_edits": true}
gotOpts := map[string]interface{}{}
options.Apply(gotOpts)
if d := testy.DiffInterface(expectedDocs, docs); d != nil {
return nil, fmt.Errorf("Unexpected docs:\n%s", d)
}
if d := testy.DiffInterface(wantOpts, gotOpts); d != nil {
return nil, fmt.Errorf("Unexpected opts:\n%s", d)
}
return []driver.BulkResult{
{ID: "foo"},
}, nil
},
},
},
docs: []interface{}{
map[string]string{"_id": "foo"},
123,
},
options: Param("new_edits", true),
expected: []BulkResult{
{ID: "foo"},
},
})
tests.Add("client closed", tt{
db: &DB{
client: &Client{
closed: true,
},
},
docs: []interface{}{
map[string]string{"_id": "foo"},
},
status: http.StatusServiceUnavailable,
err: "kivik: client closed",
})
tests.Add("db error", tt{
db: &DB{
err: errors.New("db error"),
},
status: http.StatusInternalServerError,
err: "db error",
})
tests.Add("unreadable doc", tt{
db: &DB{
client: &Client{},
driverDB: &mock.BulkDocer{
BulkDocsFunc: func(_ context.Context, docs []interface{}, _ driver.Options) ([]driver.BulkResult, error) {
_, err := json.Marshal(docs)
return nil, err
},
},
},
docs: []interface{}{testy.ErrorReader("", errors.New("read error"))},
status: http.StatusBadRequest,
err: "read error",
})
tests.Add("no docs", tt{
db: &DB{
client: &Client{},
driverDB: &mock.BulkDocer{
BulkDocsFunc: func(_ context.Context, docs []interface{}, _ driver.Options) ([]driver.BulkResult, error) {
_, err := json.Marshal(docs)
return nil, err
},
},
},
docs: []interface{}{},
status: http.StatusBadRequest,
err: "kivik: no documents provided",
})
tests.Run(t, func(t *testing.T, tt tt) {
result, err := tt.db.BulkDocs(context.Background(), tt.docs, tt.options)
if d := internal.StatusErrorDiff(tt.err, tt.status, err); d != "" {
t.Error(d)
}
if d := testy.DiffInterface(tt.expected, result); d != nil {
t.Error(d)
}
})
}