1
2
3
4
5
6
7
8
9
10
11
12
13 package kivik
14
15 import (
16 "context"
17 "errors"
18 "fmt"
19 "net/http"
20 "testing"
21
22 "gitlab.com/flimzy/testy"
23
24 "github.com/go-kivik/kivik/v4/driver"
25 internal "github.com/go-kivik/kivik/v4/int/errors"
26 "github.com/go-kivik/kivik/v4/int/mock"
27 )
28
29 func TestConfig(t *testing.T) {
30 type tst struct {
31 client *Client
32 node string
33 expected Config
34 status int
35 err string
36 }
37 tests := testy.NewTable()
38 tests.Add("non-configer", tst{
39 client: &Client{driverClient: &mock.Client{}},
40 status: http.StatusNotImplemented,
41 err: "kivik: driver does not support Config interface",
42 })
43 tests.Add("error", tst{
44 client: &Client{driverClient: &mock.Configer{
45 ConfigFunc: func(context.Context, string) (driver.Config, error) {
46 return nil, errors.New("conf error")
47 },
48 }},
49 status: http.StatusInternalServerError,
50 err: "conf error",
51 })
52 tests.Add("success", tst{
53 client: &Client{driverClient: &mock.Configer{
54 ConfigFunc: func(_ context.Context, node string) (driver.Config, error) {
55 if node != "foo" {
56 return nil, fmt.Errorf("Unexpected node: %s", node)
57 }
58 return driver.Config{
59 "foo": driver.ConfigSection{"asd": "rew"},
60 }, nil
61 },
62 }},
63 node: "foo",
64 expected: Config{
65 "foo": ConfigSection{"asd": "rew"},
66 },
67 })
68 tests.Add("closed", tst{
69 client: &Client{
70 closed: true,
71 },
72 status: http.StatusServiceUnavailable,
73 err: "kivik: client closed",
74 })
75
76 tests.Run(t, func(t *testing.T, test tst) {
77 result, err := test.client.Config(context.Background(), test.node)
78 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
79 t.Error(d)
80 }
81 if d := testy.DiffInterface(test.expected, result); d != nil {
82 t.Error(d)
83 }
84 })
85 }
86
87 func TestConfigSection(t *testing.T) {
88 type tst struct {
89 client *Client
90 node, section string
91 expected ConfigSection
92 status int
93 err string
94 }
95 tests := testy.NewTable()
96 tests.Add("non-configer", tst{
97 client: &Client{driverClient: &mock.Client{}},
98 status: http.StatusNotImplemented,
99 err: "kivik: driver does not support Config interface",
100 })
101 tests.Add("error", tst{
102 client: &Client{driverClient: &mock.Configer{
103 ConfigSectionFunc: func(context.Context, string, string) (driver.ConfigSection, error) {
104 return nil, errors.New("conf error")
105 },
106 }},
107 status: http.StatusInternalServerError,
108 err: "conf error",
109 })
110 tests.Add("success", tst{
111 client: &Client{driverClient: &mock.Configer{
112 ConfigSectionFunc: func(_ context.Context, node, section string) (driver.ConfigSection, error) {
113 if node != "foo" {
114 return nil, fmt.Errorf("Unexpected node: %s", node)
115 }
116 if section != "foo" {
117 return nil, fmt.Errorf("Unexpected section: %s", section)
118 }
119 return driver.ConfigSection{"lkj": "ghj"}, nil
120 },
121 }},
122 node: "foo",
123 section: "foo",
124 expected: ConfigSection{"lkj": "ghj"},
125 })
126 tests.Add("closed", tst{
127 client: &Client{
128 closed: true,
129 },
130 status: http.StatusServiceUnavailable,
131 err: "kivik: client closed",
132 })
133
134 tests.Run(t, func(t *testing.T, test tst) {
135 result, err := test.client.ConfigSection(context.Background(), test.node, test.section)
136 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
137 t.Error(d)
138 }
139 if d := testy.DiffInterface(test.expected, result); d != nil {
140 t.Error(d)
141 }
142 })
143 }
144
145 func TestConfigValue(t *testing.T) {
146 type tst struct {
147 client *Client
148 node, section, key string
149 expected string
150 status int
151 err string
152 }
153 tests := testy.NewTable()
154 tests.Add("non-configer", tst{
155 client: &Client{driverClient: &mock.Client{}},
156 status: http.StatusNotImplemented,
157 err: "kivik: driver does not support Config interface",
158 })
159 tests.Add("error", tst{
160 client: &Client{driverClient: &mock.Configer{
161 ConfigValueFunc: func(context.Context, string, string, string) (string, error) {
162 return "", errors.New("conf error")
163 },
164 }},
165 status: http.StatusInternalServerError,
166 err: "conf error",
167 })
168 tests.Add("success", tst{
169 client: &Client{driverClient: &mock.Configer{
170 ConfigValueFunc: func(_ context.Context, node, section, key string) (string, error) {
171 if node != "foo" {
172 return "", fmt.Errorf("Unexpected node: %s", node)
173 }
174 if section != "foo" {
175 return "", fmt.Errorf("Unexpected section: %s", section)
176 }
177 if key != "asd" {
178 return "", fmt.Errorf("Unexpected key: %s", key)
179 }
180 return "jkl", nil
181 },
182 }},
183 node: "foo",
184 section: "foo",
185 key: "asd",
186 expected: "jkl",
187 })
188 tests.Add("closed", tst{
189 client: &Client{
190 closed: true,
191 },
192 status: http.StatusServiceUnavailable,
193 err: "kivik: client closed",
194 })
195
196 tests.Run(t, func(t *testing.T, test tst) {
197 result, err := test.client.ConfigValue(context.Background(), test.node, test.section, test.key)
198 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
199 t.Error(d)
200 }
201 if d := testy.DiffInterface(test.expected, result); d != nil {
202 t.Error(d)
203 }
204 })
205 }
206
207 func TestSetConfigValue(t *testing.T) {
208 type tst struct {
209 client *Client
210 node, section, key, value string
211 expected string
212 status int
213 err string
214 }
215 tests := testy.NewTable()
216 tests.Add("non-configer", tst{
217 client: &Client{driverClient: &mock.Client{}},
218 status: http.StatusNotImplemented,
219 err: "kivik: driver does not support Config interface",
220 })
221 tests.Add("error", tst{
222 client: &Client{driverClient: &mock.Configer{
223 SetConfigValueFunc: func(context.Context, string, string, string, string) (string, error) {
224 return "", errors.New("conf error")
225 },
226 }},
227 status: http.StatusInternalServerError,
228 err: "conf error",
229 })
230 tests.Add("success", tst{
231 client: &Client{driverClient: &mock.Configer{
232 SetConfigValueFunc: func(_ context.Context, node, section, key, value string) (string, error) {
233 if node != "foo" {
234 return "", fmt.Errorf("Unexpected node: %s", node)
235 }
236 if section != "foo" {
237 return "", fmt.Errorf("Unexpected section: %s", section)
238 }
239 if key != "vbn" {
240 return "", fmt.Errorf("Unexpected key: %s", key)
241 }
242 if value != "baz" {
243 return "", fmt.Errorf("Unexpected value: %s", value)
244 }
245 return "old", nil
246 },
247 }},
248 node: "foo",
249 section: "foo",
250 key: "vbn",
251 value: "baz",
252 expected: "old",
253 })
254 tests.Add("closed", tst{
255 client: &Client{
256 closed: true,
257 },
258 status: http.StatusServiceUnavailable,
259 err: "kivik: client closed",
260 })
261
262 tests.Run(t, func(t *testing.T, test tst) {
263 result, err := test.client.SetConfigValue(context.Background(), test.node, test.section, test.key, test.value)
264 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
265 t.Error(d)
266 }
267 if d := testy.DiffInterface(test.expected, result); d != nil {
268 t.Error(d)
269 }
270 })
271 }
272
273 func TestDeleteConfigKey(t *testing.T) {
274 type tst struct {
275 client *Client
276 node, section, key string
277 expected string
278 status int
279 err string
280 }
281 tests := testy.NewTable()
282 tests.Add("non-configer", tst{
283 client: &Client{driverClient: &mock.Client{}},
284 status: http.StatusNotImplemented,
285 err: "kivik: driver does not support Config interface",
286 })
287 tests.Add("error", tst{
288 client: &Client{driverClient: &mock.Configer{
289 DeleteConfigKeyFunc: func(context.Context, string, string, string) (string, error) {
290 return "", errors.New("conf error")
291 },
292 }},
293 status: http.StatusInternalServerError,
294 err: "conf error",
295 })
296 tests.Add("success", tst{
297 client: &Client{driverClient: &mock.Configer{
298 DeleteConfigKeyFunc: func(_ context.Context, node, section, key string) (string, error) {
299 if node != "foo" {
300 return "", fmt.Errorf("Unexpected node: %s", node)
301 }
302 if section != "foo" {
303 return "", fmt.Errorf("Unexpected section: %s", section)
304 }
305 if key != "baz" {
306 return "", fmt.Errorf("Unexpected key: %s", key)
307 }
308 return "old", nil
309 },
310 }},
311 node: "foo",
312 section: "foo",
313 key: "baz",
314 expected: "old",
315 })
316 tests.Add("closed", tst{
317 client: &Client{
318 closed: true,
319 },
320 status: http.StatusServiceUnavailable,
321 err: "kivik: client closed",
322 })
323
324 tests.Run(t, func(t *testing.T, test tst) {
325 result, err := test.client.DeleteConfigKey(context.Background(), test.node, test.section, test.key)
326 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
327 t.Error(d)
328 }
329 if d := testy.DiffInterface(test.expected, result); d != nil {
330 t.Error(d)
331 }
332 })
333 }
334
View as plain text