This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alldocs_test.go
138 lines (130 loc) · 2.89 KB
/
alldocs_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
package memorydb
import (
"context"
"io"
"sort"
"testing"
"github.com/go-kivik/kivik/v4/driver"
"gitlab.com/flimzy/testy"
)
func TestAllDocsClose(t *testing.T) {
rs := &alldocsResults{
resultSet{
revs: []*revision{{}, {}}, // Two nil revisions
docIDs: []string{"a", "b"},
},
}
row := driver.Row{}
if err := rs.Next(&row); err != nil {
t.Fatal(err)
}
if err := rs.Close(); err != nil {
t.Errorf("Unexpected error: %s", err)
}
if err := rs.Next(&row); err != io.EOF {
t.Errorf("Unexpected Next() error after closing: %s", err)
}
}
func TestAllDocs(t *testing.T) {
type adTest struct {
Name string
ExpectedIDs []string
Error string
DB *db
RowsError string
}
tests := []adTest{
{
Name: "NoDocs",
},
{
Name: "OneDoc",
DB: func() *db {
db := setupDB(t)
if _, err := db.Put(context.Background(), "foo", map[string]string{"foo": "bar"}, nil); err != nil {
t.Fatal(err)
}
return db
}(),
ExpectedIDs: []string{"foo"},
},
{
Name: "Five Docs",
DB: func() *db {
db := setupDB(t)
for _, id := range []string{"a", "c", "z", "q", "chicken"} {
if _, err := db.Put(context.Background(), id, map[string]string{"value": id}, nil); err != nil {
t.Fatal(err)
}
}
return db
}(),
ExpectedIDs: []string{"a", "c", "chicken", "q", "z"},
},
}
for _, test := range tests {
func(test adTest) {
t.Run(test.Name, func(t *testing.T) {
db := test.DB
if db == nil {
db = setupDB(t)
}
rows, err := db.AllDocs(context.Background(), nil)
var msg string
if err != nil {
msg = err.Error()
}
if test.Error != msg {
t.Errorf("Unexpected error: %s", msg)
}
if err != nil {
return
}
checkRows(t, rows, test.ExpectedIDs, test.RowsError)
})
}(test)
}
}
func checkRows(t *testing.T, rows driver.Rows, expectedIDs []string, rowsErr string) {
var row driver.Row
var ids []string
msg := ""
for {
e := rows.Next(&row)
if e != nil {
if e != io.EOF {
msg = e.Error()
}
break
}
ids = append(ids, row.ID)
}
if rowsErr != msg {
t.Errorf("Unexpected rows error: %s", msg)
}
sort.Strings(ids)
if d := testy.DiffTextSlices(expectedIDs, ids); d != nil {
t.Error(d)
}
}
func TestAllDocsUpdateSeq(t *testing.T) {
expected := "12345"
rs := &alldocsResults{resultSet{updateSeq: expected}}
if result := rs.UpdateSeq(); result != expected {
t.Errorf("Unexpected upste seq: %s", result)
}
}
func TestAllDocsTotalRows(t *testing.T) {
expected := int64(123)
rs := &alldocsResults{resultSet{totalRows: expected}}
if result := rs.TotalRows(); result != expected {
t.Errorf("Unexpected upste seq: %d", result)
}
}
func TestAllDocsOffset(t *testing.T) {
expected := int64(123)
rs := &alldocsResults{resultSet{offset: expected}}
if result := rs.Offset(); result != expected {
t.Errorf("Unexpected upste seq: %d", result)
}
}