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 pathchanges_test.go
168 lines (161 loc) · 4.4 KB
/
changes_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
package couchdb
import (
"context"
"errors"
"io/ioutil"
"net/http"
"testing"
"time"
"gitlab.com/flimzy/testy"
"github.com/go-kivik/kivik/v4/driver"
)
func TestChanges(t *testing.T) {
tests := []struct {
name string
options map[string]interface{}
db *db
status int
err string
etag string
}{
{
name: "invalid options",
options: map[string]interface{}{"foo": make(chan int)},
status: http.StatusBadRequest,
err: "kivik: invalid type chan int for options",
},
{
name: "eventsource",
options: map[string]interface{}{"feed": "eventsource"},
status: http.StatusBadRequest,
err: "kivik: eventsource feed not supported, use 'continuous'",
},
{
name: "network error",
db: newTestDB(nil, errors.New("net error")),
status: http.StatusBadGateway,
err: `Get "?http://example.com/testdb/_changes"?: net error`,
},
{
name: "continuous",
db: newTestDB(nil, errors.New("net error")),
options: map[string]interface{}{"feed": "continuous"},
status: http.StatusBadGateway,
err: `Get "?http://example.com/testdb/_changes\?feed=continuous"?: net error`,
},
{
name: "error response",
db: newTestDB(&http.Response{
StatusCode: http.StatusBadRequest,
Body: Body(""),
}, nil),
status: http.StatusBadRequest,
err: "Bad Request",
},
{
name: "success 1.6.1",
db: newTestDB(&http.Response{
StatusCode: 200,
Header: http.Header{
"Transfer-Encoding": {"chunked"},
"Server": {"CouchDB/1.6.1 (Erlang OTP/17)"},
"Date": {"Fri, 27 Oct 2017 14:43:57 GMT"},
"Content-Type": {"text/plain; charset=utf-8"},
"Cache-Control": {"must-revalidate"},
"ETag": {`"etag-foo"`},
},
Body: Body(`{"seq":3,"id":"43734cf3ce6d5a37050c050bb600006b","changes":[{"rev":"2-185ccf92154a9f24a4f4fd12233bf463"}],"deleted":true}
`),
}, nil),
etag: "etag-foo",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ch, err := test.db.Changes(context.Background(), test.options)
if ch != nil {
defer ch.Close()
}
testy.StatusErrorRE(t, test.err, test.status, err)
if etag := ch.ETag(); etag != test.etag {
t.Errorf("Unexpected ETag: %s", etag)
}
})
}
}
func TestChangesNext(t *testing.T) {
tests := []struct {
name string
changes *changesRows
status int
err string
expected *driver.Change
}{
{
name: "invalid json",
changes: newChangesRows(context.TODO(), "", Body("invalid json"), ""),
status: http.StatusBadGateway,
err: "invalid character 'i' looking for beginning of value",
},
{
name: "success",
changes: newChangesRows(context.TODO(), "", Body(`{"seq":3,"id":"43734cf3ce6d5a37050c050bb600006b","changes":[{"rev":"2-185ccf92154a9f24a4f4fd12233bf463"}],"deleted":true}
`), ""),
expected: &driver.Change{
ID: "43734cf3ce6d5a37050c050bb600006b",
Seq: "3",
Deleted: true,
Changes: []string{"2-185ccf92154a9f24a4f4fd12233bf463"},
},
},
{
name: "read error",
changes: newChangesRows(context.TODO(), "", ioutil.NopCloser(testy.ErrorReader("", errors.New("read error"))), ""),
status: http.StatusBadGateway,
err: "read error",
},
{
name: "end of input",
changes: newChangesRows(context.TODO(), "", Body(``), ""),
expected: &driver.Change{},
status: http.StatusInternalServerError,
err: "EOF",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
row := new(driver.Change)
err := test.changes.Next(row)
testy.StatusError(t, test.err, test.status, err)
if d := testy.DiffInterface(test.expected, row); d != nil {
t.Error(d)
}
})
}
}
func TestChangesClose(t *testing.T) {
t.Run("normal", func(t *testing.T) {
body := &closeTracker{ReadCloser: Body("foo")}
feed := newChangesRows(context.TODO(), "", body, "")
_ = feed.Close()
if !body.closed {
t.Errorf("Failed to close")
}
})
t.Run("next in progress", func(t *testing.T) {
body := &closeTracker{ReadCloser: ioutil.NopCloser(testy.NeverReader())}
feed := newChangesRows(context.TODO(), "", body, "")
row := new(driver.Change)
done := make(chan struct{})
go func() {
_ = feed.Next(row)
close(done)
}()
time.Sleep(50 * time.Millisecond)
_ = feed.Close()
<-done
if !body.closed {
t.Errorf("Failed to close")
}
})
}