1
2
3
4
5
6
7
8
9
10
11
12
13 package memorydb
14
15 import (
16 "context"
17 "sort"
18 "testing"
19
20 "gitlab.com/flimzy/testy"
21
22 "github.com/go-kivik/kivik/v4/driver"
23 )
24
25 var d = &memDriver{}
26
27 func setup(t *testing.T, setup func(driver.Client)) driver.Client {
28 t.Helper()
29 c, err := d.NewClient("foo", nil)
30 if err != nil {
31 t.Fatal(err)
32 }
33 if setup != nil {
34 setup(c)
35 }
36 return c
37 }
38
39 func TestNewClient(t *testing.T) {
40 _, err := d.NewClient("foo", nil)
41 if err != nil {
42 t.Errorf("Unexpected error: %s", err)
43 }
44 }
45
46 func TestDBExists(t *testing.T) {
47 type deTest struct {
48 Name string
49 DBName string
50 Setup func(driver.Client)
51 Expected bool
52 Error string
53 }
54 tests := []deTest{
55 {
56 Name: "NoDBs",
57 DBName: "foo",
58 Expected: false,
59 },
60 {
61 Name: "ExistingDB",
62 DBName: "foo",
63 Setup: func(c driver.Client) {
64 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
65 panic(err)
66 }
67 },
68 Expected: true,
69 },
70 {
71 Name: "OtherDB",
72 DBName: "foo",
73 Setup: func(c driver.Client) {
74 if err := c.CreateDB(context.Background(), "bar", nil); err != nil {
75 panic(err)
76 }
77 },
78 Expected: false,
79 },
80 }
81 for _, test := range tests {
82 func(test deTest) {
83 t.Run(test.Name, func(t *testing.T) {
84 c := setup(t, test.Setup)
85 result, err := c.DBExists(context.Background(), test.DBName, nil)
86 if !testy.ErrorMatches(test.Error, err) {
87 t.Errorf("Unexpected error: %s", err)
88 }
89 if result != test.Expected {
90 t.Errorf("Expected: %t, Actual: %t", test.Expected, result)
91 }
92 })
93 }(test)
94 }
95 }
96
97 func TestCreateDB(t *testing.T) {
98 type cdTest struct {
99 Name string
100 DBName string
101 Error string
102 Setup func(driver.Client)
103 }
104 tests := []cdTest{
105 {
106 Name: "FirstDB",
107 DBName: "foo",
108 },
109 {
110 Name: "UsersDB",
111 DBName: "_users",
112 },
113 {
114 Name: "SystemDB",
115 DBName: "_foo",
116 Error: "invalid database name",
117 },
118 {
119 Name: "Duplicate",
120 DBName: "foo",
121 Setup: func(c driver.Client) {
122 if e := c.CreateDB(context.Background(), "foo", nil); e != nil {
123 panic(e)
124 }
125 },
126 Error: "database exists",
127 },
128 }
129 for _, test := range tests {
130 func(test cdTest) {
131 t.Run(test.Name, func(t *testing.T) {
132 c := setup(t, test.Setup)
133 var msg string
134 if e := c.CreateDB(context.Background(), test.DBName, nil); e != nil {
135 msg = e.Error()
136 }
137 if msg != test.Error {
138 t.Errorf("Unexpected error: %s", msg)
139 }
140 })
141 }(test)
142 }
143 }
144
145 func TestAllDBs(t *testing.T) {
146 type adTest struct {
147 Name string
148 Setup func(driver.Client)
149 Expected []string
150 Error string
151 }
152 tests := []adTest{
153 {
154 Name: "NoDBs",
155 Expected: []string{},
156 },
157 {
158 Name: "2DBs",
159 Setup: func(c driver.Client) {
160 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
161 panic(err)
162 }
163 if err := c.CreateDB(context.Background(), "bar", nil); err != nil {
164 panic(err)
165 }
166 },
167 Expected: []string{"foo", "bar"},
168 },
169 }
170 for _, test := range tests {
171 func(test adTest) {
172 t.Run(test.Name, func(t *testing.T) {
173 c := setup(t, test.Setup)
174 result, err := c.AllDBs(context.Background(), nil)
175 var msg string
176 if err != nil {
177 msg = err.Error()
178 }
179 if msg != test.Error {
180 t.Errorf("Unexpected error: %s", msg)
181 }
182 sort.Strings(test.Expected)
183 sort.Strings(result)
184 if d := testy.DiffInterface(test.Expected, result); d != nil {
185 t.Error(d)
186 }
187 })
188 }(test)
189 }
190 }
191
192 func TestDestroyDB(t *testing.T) {
193 type ddTest struct {
194 Name string
195 DBName string
196 Setup func(driver.Client)
197 Error string
198 }
199 tests := []ddTest{
200 {
201 Name: "NoDBs",
202 DBName: "foo",
203 Error: "database does not exist",
204 },
205 {
206 Name: "ExistingDB",
207 DBName: "foo",
208 Setup: func(c driver.Client) {
209 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
210 panic(err)
211 }
212 },
213 },
214 {
215 Name: "OtherDB",
216 DBName: "foo",
217 Setup: func(c driver.Client) {
218 if err := c.CreateDB(context.Background(), "bar", nil); err != nil {
219 panic(err)
220 }
221 },
222 Error: "database does not exist",
223 },
224 }
225 for _, test := range tests {
226 func(test ddTest) {
227 t.Run(test.Name, func(t *testing.T) {
228 c := setup(t, test.Setup)
229 var msg string
230 if e := c.DestroyDB(context.Background(), test.DBName, nil); e != nil {
231 msg = e.Error()
232 }
233 if msg != test.Error {
234 t.Errorf("Unexpected error: %s", msg)
235 }
236 })
237 }(test)
238 }
239 }
240
241 func TestDB(t *testing.T) {
242 type dbTest struct {
243 Name string
244 DBName string
245 Setup func(driver.Client)
246 Error string
247 }
248 tests := []dbTest{
249 {
250 Name: "ExistingDB",
251 DBName: "foo",
252 Setup: func(c driver.Client) {
253 if err := c.CreateDB(context.Background(), "foo", nil); err != nil {
254 panic(err)
255 }
256 },
257 },
258 }
259 for _, test := range tests {
260 func(test dbTest) {
261 t.Run(test.Name, func(t *testing.T) {
262 c := setup(t, test.Setup)
263 _, err := c.DB(test.DBName, nil)
264 var msg string
265 if err != nil {
266 msg = err.Error()
267 }
268 if msg != test.Error {
269 t.Errorf("Unexpected error: %s", msg)
270 }
271 })
272 }(test)
273 }
274 }
275
276 func TestClientVersion(t *testing.T) {
277 expected := &driver.Version{}
278 c := &client{version: expected}
279 result, err := c.Version(context.Background())
280 if err != nil {
281 t.Fatal(err)
282 }
283 if result != expected {
284 t.Errorf("Wrong version object returned")
285 }
286 }
287
View as plain text