github.com/go-kivik/kivik/v4@v4.3.2/x/fsdb/revsdiff_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 "encoding/json" 18 "io" 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/x/fsdb/filesystem" 27 ) 28 29 func TestRevsDiff(t *testing.T) { 30 type tt struct { 31 ctx context.Context 32 fs filesystem.Filesystem 33 path, dbname string 34 revMap interface{} 35 status int 36 err string 37 rowStatus int 38 rowErr string 39 } 40 tests := testy.NewTable() 41 tests.Add("invalid revMap", tt{ 42 dbname: "foo", 43 revMap: make(chan int), 44 status: http.StatusBadRequest, 45 err: "json: unsupported type: chan int", 46 }) 47 tests.Add("empty map", tt{ 48 dbname: "foo", 49 revMap: map[string][]string{}, 50 }) 51 tests.Add("real test", tt{ 52 path: "testdata", 53 dbname: "db_foo", 54 revMap: map[string][]string{ 55 "yamltest": {"3-", "2-xxx", "1-oink"}, 56 "autorev": {"6-", "5-", "4-"}, 57 "newdoc": {"1-asdf"}, 58 }, 59 }) 60 tests.Add("cancelled context", func(*testing.T) interface{} { 61 ctx, cancel := context.WithCancel(context.Background()) 62 cancel() 63 return tt{ 64 ctx: ctx, 65 path: "testdata", 66 dbname: "db_foo", 67 revMap: map[string][]string{ 68 "yamltest": {"3-", "2-xxx", "1-oink"}, 69 "autorev": {"6-", "5-", "4-"}, 70 "newdoc": {"1-asdf"}, 71 }, 72 rowStatus: http.StatusInternalServerError, 73 rowErr: "context canceled", 74 } 75 }) 76 77 tests.Run(t, func(t *testing.T, tt tt) { 78 dir := tt.path 79 if dir == "" { 80 dir = tempDir(t) 81 t.Cleanup(func() { 82 rmdir(t, dir) 83 }) 84 } 85 fs := tt.fs 86 if fs == nil { 87 fs = filesystem.Default() 88 } 89 c := &client{root: dir, fs: fs} 90 db, err := c.newDB(tt.dbname) 91 if err != nil { 92 t.Fatal(err) 93 } 94 ctx := tt.ctx 95 if ctx == nil { 96 ctx = context.Background() 97 } 98 rows, err := db.RevsDiff(ctx, tt.revMap) 99 if d := internal.StatusErrorDiffRE(tt.err, tt.status, err); d != "" { 100 t.Error(d) 101 } 102 if err != nil { 103 return 104 } 105 result := make(map[string]json.RawMessage) 106 var row driver.Row 107 var rowErr error 108 for { 109 err := rows.Next(&row) 110 if err == io.EOF { 111 break 112 } 113 if err != nil { 114 rowErr = err 115 break 116 } 117 var value json.RawMessage 118 _ = json.NewDecoder(row.Value).Decode(&value) 119 result[row.ID] = value 120 } 121 if d := internal.StatusErrorDiffRE(tt.rowErr, tt.rowStatus, rowErr); d != "" { 122 t.Error(d) 123 } 124 if rowErr != nil { 125 return 126 } 127 if d := testy.DiffAsJSON(testy.Snapshot(t), result); d != nil { 128 t.Error(d) 129 } 130 }) 131 }