github.com/go-kivik/kivik/v4@v4.3.2/x/fsdb/db_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package fs 14 15 import ( 16 "context" 17 "net/http" 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "gitlab.com/flimzy/testy" 22 23 "github.com/go-kivik/kivik/v4" 24 "github.com/go-kivik/kivik/v4/driver" 25 "github.com/go-kivik/kivik/v4/x/fsdb/filesystem" 26 ) 27 28 func Test_db_Stats(t *testing.T) { 29 tests := []struct { 30 name string 31 path, dbname string 32 wantStatus int 33 wantErr string 34 want *driver.DBStats 35 }{ 36 { 37 name: "success", 38 path: "testdata", 39 dbname: "db_att", 40 want: &driver.DBStats{ 41 Name: "db_att", 42 }, 43 }, 44 { 45 name: "not found", 46 path: "testdata", 47 dbname: "notfound", 48 wantStatus: http.StatusNotFound, 49 wantErr: `[Nn]o such file or directory`, 50 }, 51 } 52 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 c := &client{root: tt.path, fs: filesystem.Default()} 56 db, err := c.newDB(tt.dbname) 57 if err != nil { 58 t.Fatal(err) 59 } 60 61 stats, err := db.Stats(context.Background()) 62 if !testy.ErrorMatchesRE(tt.wantErr, err) { 63 t.Errorf("error = %v, wantErr %v", err, tt.wantErr) 64 return 65 } 66 if status := kivik.HTTPStatus(err); status != tt.wantStatus { 67 t.Errorf("status = %v, wantStatus %v", status, tt.wantStatus) 68 } 69 if d := cmp.Diff(tt.want, stats); d != "" { 70 t.Errorf("Unexpected stats:\n%s\n", d) 71 } 72 }) 73 } 74 }