1
2
3
4
5
6
7
8
9
10
11
12
13 package couchdb
14
15 import (
16 "context"
17 "encoding/json"
18 "errors"
19 "fmt"
20 "io"
21 "net/http"
22 "strings"
23 "testing"
24
25 "gitlab.com/flimzy/testy"
26
27 "github.com/go-kivik/kivik/v4/driver"
28 internal "github.com/go-kivik/kivik/v4/int/errors"
29 )
30
31 func TestConfig(t *testing.T) {
32 type tst struct {
33 client *client
34 node string
35 expected driver.Config
36 status int
37 err string
38 }
39 tests := testy.NewTable()
40 tests.Add("network error", tst{
41 client: newTestClient(nil, errors.New("net error")),
42 node: "local",
43 status: http.StatusBadGateway,
44 err: `^Get "?http://example.com/_node/local/_config"?: net error$`,
45 })
46 tests.Add("Couch 1.x path", tst{
47 client: newTestClient(nil, errors.New("net error")),
48 node: couch1ConfigNode,
49 status: http.StatusBadGateway,
50 err: `^Get "?http://example.com/_config"?: net error$`,
51 })
52 tests.Add("success", tst{
53 client: newTestClient(&http.Response{
54 StatusCode: http.StatusOK,
55 Body: io.NopCloser(strings.NewReader(`{"foo":{"asd":"baz"}}`)),
56 }, nil),
57 node: "local",
58 expected: driver.Config{
59 "foo": driver.ConfigSection{"asd": "baz"},
60 },
61 })
62
63 tests.Run(t, func(t *testing.T, test tst) {
64 result, err := test.client.Config(context.Background(), test.node)
65 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
66 t.Error(d)
67 }
68 if err != nil {
69 return
70 }
71 if d := testy.DiffInterface(test.expected, result); d != nil {
72 t.Error(d)
73 }
74 })
75 }
76
77 func TestConfigSection(t *testing.T) {
78 type tst struct {
79 client *client
80 node, section string
81 expected driver.ConfigSection
82 status int
83 err string
84 }
85 tests := testy.NewTable()
86 tests.Add("network error", tst{
87 client: newTestClient(nil, errors.New("net error")),
88 node: "local",
89 section: "foo",
90 status: http.StatusBadGateway,
91 err: `^Get "?http://example.com/_node/local/_config/foo"?: net error$`,
92 })
93 tests.Add("Couch 1.x path", tst{
94 client: newTestClient(nil, errors.New("net error")),
95 node: couch1ConfigNode,
96 section: "foo",
97 status: http.StatusBadGateway,
98 err: `^Get "?http://example.com/_config/foo"?: net error$`,
99 })
100 tests.Add("success", tst{
101 client: newTestClient(&http.Response{
102 StatusCode: http.StatusOK,
103 Body: io.NopCloser(strings.NewReader(`{"fds":"baz"}`)),
104 }, nil),
105 node: "local",
106 section: "foo",
107 expected: driver.ConfigSection{"fds": "baz"},
108 })
109
110 tests.Run(t, func(t *testing.T, test tst) {
111 result, err := test.client.ConfigSection(context.Background(), test.node, test.section)
112 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
113 t.Error(d)
114 }
115 if err != nil {
116 return
117 }
118 if d := testy.DiffInterface(test.expected, result); d != nil {
119 t.Error(d)
120 }
121 })
122 }
123
124 func TestConfigValue(t *testing.T) {
125 type tst struct {
126 client *client
127 node, section, key string
128 expected string
129 status int
130 err string
131 }
132 tests := testy.NewTable()
133 tests.Add("network error", tst{
134 client: newTestClient(nil, errors.New("net error")),
135 node: "local",
136 section: "foo",
137 key: "tre",
138 status: http.StatusBadGateway,
139 err: `Get "?http://example.com/_node/local/_config/foo/tre"?: net error`,
140 })
141 tests.Add("Couch 1.x path", tst{
142 client: newTestClient(nil, errors.New("net error")),
143 node: couch1ConfigNode,
144 section: "foo",
145 key: "bar",
146 status: http.StatusBadGateway,
147 err: `Get "?http://example.com/_config/foo/bar"?: net error`,
148 })
149 tests.Add("success", tst{
150 client: newTestClient(&http.Response{
151 StatusCode: http.StatusOK,
152 Body: io.NopCloser(strings.NewReader(`"baz"`)),
153 }, nil),
154 node: "local",
155 section: "foo",
156 key: "bar",
157 expected: "baz",
158 })
159
160 tests.Run(t, func(t *testing.T, test tst) {
161 result, err := test.client.ConfigValue(context.Background(), test.node, test.section, test.key)
162 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
163 t.Error(d)
164 }
165 if d := testy.DiffInterface(test.expected, result); d != nil {
166 t.Error(d)
167 }
168 })
169 }
170
171 func TestSetConfigValue(t *testing.T) {
172 type tst struct {
173 client *client
174 node, section, key, value string
175 expected string
176 status int
177 err string
178 }
179 tests := testy.NewTable()
180 tests.Add("network error", tst{
181 client: newTestClient(nil, errors.New("net error")),
182 node: "local",
183 section: "foo",
184 key: "bar",
185 status: http.StatusBadGateway,
186 err: `Put "?http://example.com/_node/local/_config/foo/bar"?: net error`,
187 })
188 tests.Add("Couch 1.x path", tst{
189 client: newTestClient(nil, errors.New("net error")),
190 node: couch1ConfigNode,
191 section: "foo",
192 key: "bar",
193 status: http.StatusBadGateway,
194 err: `^Put "?http://example.com/_config/foo/bar"?: net error$`,
195 })
196 tests.Add("success", tst{
197 client: newCustomClient(func(r *http.Request) (*http.Response, error) {
198 var val string
199 defer r.Body.Close()
200 if err := json.NewDecoder(r.Body).Decode(&val); err != nil {
201 return nil, err
202 }
203 if val != "baz" {
204 return nil, fmt.Errorf("Unexpected value: %s", val)
205 }
206
207 return &http.Response{
208 StatusCode: http.StatusOK,
209 Body: io.NopCloser(strings.NewReader(`"old"`)),
210 }, nil
211 }),
212 node: "local",
213 section: "foo",
214 key: "bar",
215 value: "baz",
216 expected: "old",
217 })
218
219 tests.Run(t, func(t *testing.T, test tst) {
220 result, err := test.client.SetConfigValue(context.Background(), test.node, test.section, test.key, test.value)
221 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
222 t.Error(d)
223 }
224 if d := testy.DiffInterface(test.expected, result); d != nil {
225 t.Error(d)
226 }
227 })
228 }
229
230 func TestDeleteConfigKey(t *testing.T) {
231 type tst struct {
232 client *client
233 node, section, key string
234 expected string
235 status int
236 err string
237 }
238 tests := testy.NewTable()
239 tests.Add("network error", tst{
240 client: newTestClient(nil, errors.New("net error")),
241 node: "local",
242 section: "foo",
243 key: "bar",
244 status: http.StatusBadGateway,
245 err: `Delete "?http://example.com/_node/local/_config/foo/bar"?: net error`,
246 })
247 tests.Add("Couch 1.x path", tst{
248 client: newTestClient(nil, errors.New("net error")),
249 node: couch1ConfigNode,
250 section: "foo",
251 key: "bar",
252 status: http.StatusBadGateway,
253 err: `Delete "?http://example.com/_config/foo/bar"?: net error`,
254 })
255 tests.Add("success", tst{
256 client: newTestClient(&http.Response{
257 StatusCode: http.StatusOK,
258 Body: io.NopCloser(strings.NewReader(`"old"`)),
259 }, nil),
260 node: "local",
261 section: "foo",
262 key: "bar",
263 expected: "old",
264 })
265
266 tests.Run(t, func(t *testing.T, test tst) {
267 result, err := test.client.DeleteConfigKey(context.Background(), test.node, test.section, test.key)
268 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
269 t.Error(d)
270 }
271 if d := testy.DiffInterface(test.expected, result); d != nil {
272 t.Error(d)
273 }
274 })
275 }
276
View as plain text