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 pathcouchdb_test.go
113 lines (107 loc) · 2.33 KB
/
couchdb_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
package couchdb
import (
"context"
"net/http"
"testing"
"time"
"gitlab.com/flimzy/testy"
kivik "github.com/go-kivik/kivik/v4"
)
func TestNewClient(t *testing.T) {
type ncTest struct {
name string
driver *Couch
dsn string
expectedUA []string
status int
err string
}
tests := []ncTest{
{
name: "invalid url",
dsn: "foo.com/%xxx",
status: http.StatusBadRequest,
err: `parse "?http://foo.com/%xxx"?: invalid URL escape "%xx"`,
},
{
name: "success",
dsn: "http://foo.com/",
expectedUA: []string{
"Kivik/" + kivik.KivikVersion,
"Kivik CouchDB driver/" + Version,
},
},
{
name: "User Agent",
dsn: "http://foo.com/",
driver: &Couch{UserAgent: "test/foo"},
expectedUA: []string{
"Kivik/" + kivik.KivikVersion,
"Kivik CouchDB driver/" + Version,
"test/foo",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
driver := test.driver
if driver == nil {
driver = &Couch{}
}
result, err := driver.NewClient(test.dsn)
testy.StatusErrorRE(t, test.err, test.status, err)
client, ok := result.(*client)
if !ok {
t.Errorf("Unexpected type returned: %t", result)
}
if d := testy.DiffInterface(test.expectedUA, client.Client.UserAgents); d != nil {
t.Error(d)
}
})
}
t.Run("custom HTTP client", func(t *testing.T) {
custom := &Couch{
HTTPClient: &http.Client{Timeout: time.Millisecond},
}
c, err := custom.NewClient("http://example.com/")
if err != nil {
t.Fatal(err)
}
if c.(*client).Client.Timeout != time.Millisecond {
t.Error("Unexpected *http.Client returned")
}
})
}
func TestDB(t *testing.T) {
tests := []struct {
name string
client *client
dbName string
options map[string]interface{}
expected *db
status int
err string
}{
{
name: "no dbname",
status: http.StatusBadRequest,
err: "kivik: dbName required",
},
{
name: "no full commit",
dbName: "foo",
expected: &db{
dbName: "foo",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := test.client.DB(context.Background(), test.dbName, test.options)
testy.StatusError(t, test.err, test.status, err)
if _, ok := result.(*db); !ok {
t.Errorf("Unexpected result type: %T", result)
}
})
}
}