1
2
3
4
5
6
7
8
9
10
11
12
13 package fs
14
15 import (
16 "context"
17 "net/http"
18 "testing"
19
20 "gitlab.com/flimzy/testy"
21
22 "github.com/go-kivik/kivik/v4"
23 internal "github.com/go-kivik/kivik/v4/int/errors"
24 "github.com/go-kivik/kivik/v4/int/mock"
25 )
26
27 func TestAllDBs(t *testing.T) {
28 type tt struct {
29 path string
30 options kivik.Option
31 status int
32 err string
33 expected []string
34 }
35 tests := testy.NewTable()
36 tests.Add("testdata", tt{
37 path: "testdata",
38 expected: []string{
39 "compact_extra_atts",
40 "compact_noatt",
41 "compact_nowinner_noatt",
42 "compact_oldrevs",
43 "compact_oldrevsatt",
44 "compact_split_atts",
45 "db_att",
46 "db_bar",
47 "db_foo",
48 "db_nonascii",
49 "db_put",
50 "get_nowinner",
51 "get_split_atts",
52 },
53 })
54 tests.Add("No root path", tt{
55 path: "",
56 status: http.StatusBadRequest,
57 err: "no root path provided",
58 })
59
60 d := &fsDriver{}
61 tests.Run(t, func(t *testing.T, tt tt) {
62 c, _ := d.NewClient(tt.path, nil)
63 opts := tt.options
64 if opts == nil {
65 opts = mock.NilOption
66 }
67 result, err := c.AllDBs(context.TODO(), opts)
68 if d := internal.StatusErrorDiff(tt.err, tt.status, err); d != "" {
69 t.Error(d)
70 }
71 if d := testy.DiffInterface(tt.expected, result); d != nil {
72 t.Error(d)
73 }
74 })
75 }
76
77 func TestClientdbPath(t *testing.T) {
78 type tt struct {
79 root string
80 dbname string
81 status int
82 err string
83 path string
84 name string
85 }
86 tests := testy.NewTable()
87 tests.Add("normal", tt{
88 root: "/foo/bar",
89 dbname: "baz",
90 path: "/foo/bar/baz",
91 name: "baz",
92 })
93 tests.Add("only db path", tt{
94 root: "",
95 dbname: "/foo/bar",
96 path: "/foo/bar",
97 name: "bar",
98 })
99 tests.Add("invalid file url", tt{
100 root: "",
101 dbname: "file:///%xxx",
102 status: http.StatusBadRequest,
103 err: `parse "?file:///%xxx"?: invalid URL escape "%xx"`,
104 })
105 tests.Add("file:// url for db", tt{
106 root: "",
107 dbname: "file:///foo/bar",
108 path: "/foo/bar",
109 name: "bar",
110 })
111 tests.Add("dot", tt{
112 root: "",
113 dbname: ".",
114 path: ".",
115 name: ".",
116 })
117
118 tests.Run(t, func(t *testing.T, tt tt) {
119 c := &client{root: tt.root}
120 path, name, err := c.dbPath(tt.dbname)
121 if d := internal.StatusErrorDiffRE(tt.err, tt.status, err); d != "" {
122 t.Error(d)
123 }
124 if path != tt.path {
125 t.Errorf("Unexpected path: %s", path)
126 }
127 if name != tt.name {
128 t.Errorf("Unexpected name: %s", name)
129 }
130 })
131 }
132
133 func TestClientnewDB(t *testing.T) {
134 if isGopherJS117 {
135 t.Skip("Tests broken for GopherJS 1.17")
136 }
137 type tt struct {
138 root string
139 dbname string
140 status int
141 err string
142 }
143 tests := testy.NewTable()
144 tests.Add("simple", tt{
145 root: "/foo",
146 dbname: "bar",
147 })
148 tests.Add("complete path", tt{
149 root: "",
150 dbname: "/foo/bar",
151 })
152
153 tests.Run(t, func(t *testing.T, tt tt) {
154 c := &client{root: tt.root}
155 result, err := c.newDB(tt.dbname)
156 if d := internal.StatusErrorDiff(tt.err, tt.status, err); d != "" {
157 t.Error(d)
158 }
159 if d := testy.DiffInterface(testy.Snapshot(t), result); d != nil {
160 t.Error(d)
161 }
162 })
163 }
164
View as plain text