This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows_test.go
184 lines (174 loc) · 4.16 KB
/
rows_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
package couchdb
import (
"context"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"gitlab.com/flimzy/testy"
"github.com/go-kivik/kivik/v4/driver"
)
var input = `
{
"offset": 6,
"rows": [
{
"id": "SpaghettiWithMeatballs",
"key": "meatballs",
"value": 1
},
{
"id": "SpaghettiWithMeatballs",
"key": "spaghetti",
"value": 1
},
{
"id": "SpaghettiWithMeatballs",
"key": "tomato sauce",
"value": 1
}
],
"total_rows": 3
}
`
var expectedKeys = []string{`"meatballs"`, `"spaghetti"`, `"tomato sauce"`}
func TestRowsIterator(t *testing.T) {
rows := newRows(context.TODO(), ioutil.NopCloser(strings.NewReader(input)))
var count int
for {
row := &driver.Row{}
err := rows.Next(row)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Next() failed: %s", err)
}
if string(row.Key) != expectedKeys[count] {
t.Errorf("Expected key #%d to be %s, got %s", count, expectedKeys[count], string(row.Key))
}
if count++; count > 10 {
t.Fatalf("Ran too many iterations.")
}
}
if count != 3 {
t.Errorf("Expected 3 rows, got %d", count)
}
if rows.TotalRows() != 3 {
t.Errorf("Expected TotalRows of 3, got %d", rows.TotalRows())
}
if rows.Offset() != 6 {
t.Errorf("Expected Offset of 6, got %d", rows.Offset())
}
if err := rows.Next(&driver.Row{}); err != io.EOF {
t.Errorf("Calling Next() after end returned unexpected error: %s", err)
}
if err := rows.Close(); err != nil {
t.Errorf("Error closing rows iterator: %s", err)
}
}
func TestRowsIteratorErrors(t *testing.T) {
tests := []struct {
name string
input string
status int
err string
}{
{
name: "empty input",
input: "",
status: http.StatusBadGateway,
err: "EOF",
},
{
name: "unexpected delimiter",
input: "[]",
status: http.StatusBadGateway,
err: "Unexpected JSON delimiter: [",
},
{
name: "unexpected input",
input: `"foo"`,
status: http.StatusBadGateway,
err: "Unexpected token string: foo",
},
{
name: "missing closing delimiter",
input: `{"rows":[{"id":"1","key":"1","value":1}`,
status: http.StatusBadGateway,
err: "EOF",
},
{
name: "unexpected key",
input: `{"foo":"bar"}`,
status: http.StatusBadGateway,
err: "Unexpected key: foo",
},
{
name: "unexpected key after valid row",
input: `{"rows":[{"id":"1","key":"1","value":1}],"foo":"bar"}`,
status: http.StatusBadGateway,
err: "Unexpected key: foo",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rows := newRows(context.TODO(), ioutil.NopCloser(strings.NewReader(test.input)))
for i := 0; i < 10; i++ {
err := rows.Next(&driver.Row{})
if err == nil {
continue
}
testy.StatusError(t, test.err, test.status, err)
}
})
}
}
var findInput = `
{"warning":"no matching index found, create an index to optimize query time",
"docs":[
{"id":"SpaghettiWithMeatballs","key":"meatballs","value":1},
{"id":"SpaghettiWithMeatballs","key":"spaghetti","value":1},
{"id":"SpaghettiWithMeatballs","key":"tomato sauce","value":1}
],
"bookmark": "nil"
}
`
type fullRows interface {
driver.Rows
driver.RowsWarner
driver.Bookmarker
}
func TestFindRowsIterator(t *testing.T) {
rows := newFindRows(context.TODO(), ioutil.NopCloser(strings.NewReader(findInput))).(fullRows)
var count int
for {
row := &driver.Row{}
err := rows.Next(row)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Next() failed: %s", err)
}
if count++; count > 10 {
t.Fatalf("Ran too many iterations.")
}
}
if count != 3 {
t.Errorf("Expected 3 rows, got %d", count)
}
if err := rows.Next(&driver.Row{}); err != io.EOF {
t.Errorf("Calling Next() after end returned unexpected error: %s", err)
}
if err := rows.Close(); err != nil {
t.Errorf("Error closing rows iterator: %s", err)
}
if rows.Warning() != "no matching index found, create an index to optimize query time" {
t.Errorf("Unexpected warning: %s", rows.Warning())
}
if rows.Bookmark() != "nil" {
t.Errorf("Unexpected bookmark: %s", rows.Bookmark())
}
}