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
/
Copy pathrevsdiff_test.go
118 lines (112 loc) · 2.77 KB
/
revsdiff_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
// 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 fs
import (
"context"
"encoding/json"
"io"
"net/http"
"testing"
"gitlab.com/flimzy/testy"
"github.com/go-kivik/fsdb/v4/filesystem"
"github.com/go-kivik/kivik/v4/driver"
)
func TestRevsDiff(t *testing.T) {
type tt struct {
ctx context.Context
fs filesystem.Filesystem
path, dbname string
revMap interface{}
status int
err string
rowStatus int
rowErr string
}
tests := testy.NewTable()
tests.Add("invalid revMap", tt{
dbname: "foo",
revMap: make(chan int),
status: http.StatusBadRequest,
err: "json: unsupported type: chan int",
})
tests.Add("empty map", tt{
dbname: "foo",
revMap: map[string][]string{},
})
tests.Add("real test", tt{
path: "testdata",
dbname: "db_foo",
revMap: map[string][]string{
"yamltest": {"3-", "2-xxx", "1-oink"},
"autorev": {"6-", "5-", "4-"},
"newdoc": {"1-asdf"},
},
})
tests.Add("cancelled context", func(t *testing.T) interface{} {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return tt{
ctx: ctx,
path: "testdata",
dbname: "db_foo",
revMap: map[string][]string{
"yamltest": {"3-", "2-xxx", "1-oink"},
"autorev": {"6-", "5-", "4-"},
"newdoc": {"1-asdf"},
},
rowStatus: http.StatusInternalServerError,
rowErr: "context canceled",
}
})
tests.Run(t, func(t *testing.T, tt tt) {
dir := tt.path
if dir == "" {
dir = tempDir(t)
defer rmdir(t, dir)
}
fs := tt.fs
if fs == nil {
fs = filesystem.Default()
}
c := &client{root: dir, fs: fs}
db, err := c.newDB(tt.dbname)
if err != nil {
t.Fatal(err)
}
ctx := tt.ctx
if ctx == nil {
ctx = context.Background()
}
rows, err := db.RevsDiff(ctx, tt.revMap)
testy.StatusErrorRE(t, tt.err, tt.status, err)
result := make(map[string]json.RawMessage)
var row driver.Row
var rowErr error
for {
err := rows.Next(&row)
if err == io.EOF {
break
}
if err != nil {
rowErr = err
break
}
var value json.RawMessage
_ = json.NewDecoder(row.Value).Decode(&value)
result[row.ID] = value
}
testy.StatusErrorRE(t, tt.rowErr, tt.rowStatus, rowErr)
if d := testy.DiffAsJSON(testy.Snapshot(t), result); d != nil {
t.Error(d)
}
})
}