github.com/go-kivik/kivik/v4@v4.3.2/couchdb/db_live_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 couchdb_test 14 15 import ( 16 "context" 17 "os" 18 "runtime" 19 "testing" 20 21 "gitlab.com/flimzy/testy" 22 23 "github.com/go-kivik/kivik/v4" 24 ) 25 26 const isGopherJS = runtime.GOOS == "js" || runtime.GOARCH == "js" 27 28 func TestQueries_2_x(t *testing.T) { 29 if isGopherJS { 30 t.Skip("Network tests skipped for GopherJS") 31 } 32 dsn := os.Getenv("KIVIK_TEST_DSN_COUCH23") 33 if dsn == "" { 34 dsn = os.Getenv("KIVIK_TEST_DSN_COUCH22") 35 } 36 if dsn == "" { 37 t.Skip("Neither KIVIK_TEST_DSN_COUCH22 nor KIVIK_TEST_DSN_COUCH23 configured") 38 } 39 40 client, err := kivik.New("couch", dsn) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 db := client.DB("_users") 46 rows := db.AllDocs(context.Background(), kivik.Params(map[string]interface{}{ 47 "queries": []map[string]interface{}{ 48 {}, 49 {}, 50 }, 51 })) 52 if err := rows.Err(); err != nil { 53 t.Fatal(err) 54 } 55 t.Cleanup(func() { 56 _ = rows.Close() 57 }) 58 result := make([]interface{}, 0) 59 for rows.Next() { 60 id, _ := rows.ID() 61 result = append(result, map[string]interface{}{ 62 "_id": id, 63 }) 64 } 65 meta, err := rows.Metadata() 66 if err != nil { 67 t.Fatal(err) 68 } 69 wantMeta := &kivik.ResultMetadata{ 70 TotalRows: 1, 71 } 72 if d := testy.DiffInterface(wantMeta, meta); d != nil { 73 t.Error(d) 74 } 75 if err := rows.Err(); err != nil { 76 t.Fatal(err) 77 } 78 if d := testy.DiffInterface(testy.Snapshot(t), result); d != nil { 79 t.Error(d) 80 } 81 } 82 83 func TestQueries_3_x(t *testing.T) { 84 dsn := os.Getenv("KIVIK_TEST_DSN_COUCH30") 85 if dsn == "" { 86 t.Skip("KIVIK_TEST_DSN_COUCH30 not configured") 87 } 88 89 client, err := kivik.New("couch", dsn) 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 db := client.DB("_users") 95 rows := db.AllDocs(context.Background(), kivik.Params(map[string]interface{}{ 96 "queries": []map[string]interface{}{ 97 {}, 98 {}, 99 }, 100 })) 101 if err := rows.Err(); err != nil { 102 t.Fatal(err) 103 } 104 t.Cleanup(func() { 105 _ = rows.Close() 106 }) 107 result := make([]interface{}, 0) 108 for rows.Next() { 109 id, _ := rows.ID() 110 result = append(result, map[string]interface{}{ 111 "_id": id, 112 }) 113 } 114 meta, err := rows.Metadata() 115 if err != nil { 116 t.Fatal(err) 117 } 118 wantMeta := &kivik.ResultMetadata{ 119 TotalRows: 1, 120 } 121 if d := testy.DiffInterface(wantMeta, meta); d != nil { 122 t.Error(d) 123 } 124 if err := rows.Err(); err != nil { 125 t.Fatal(err) 126 } 127 if d := testy.DiffInterface(testy.Snapshot(t), result); d != nil { 128 t.Error(d) 129 } 130 } 131 132 // https://github.com/go-kivik/kivik/issues/509 133 func Test_bug509(t *testing.T) { 134 if isGopherJS { 135 t.Skip("Network tests skipped for GopherJS") 136 } 137 dsn := os.Getenv("KIVIK_TEST_DSN_COUCH23") 138 if dsn == "" { 139 t.Skip("KIVIK_TEST_DSN_COUCH23 not configured") 140 } 141 142 client, err := kivik.New("couch", dsn) 143 if err != nil { 144 t.Fatal(err) 145 } 146 t.Cleanup(func() { 147 _ = client.DestroyDB(context.Background(), "bug509") 148 _ = client.Close() 149 }) 150 if err := client.CreateDB(context.Background(), "bug509"); err != nil { 151 t.Fatal(err) 152 } 153 154 db := client.DB("bug509") 155 if _, err := db.Put(context.Background(), "x", map[string]string{ 156 "_id": "x", 157 }); err != nil { 158 t.Fatal(err) 159 } 160 row := db.Get(context.Background(), "x", kivik.Param("revs_info", true)) 161 162 var doc map[string]interface{} 163 if err := row.ScanDoc(&doc); err != nil { 164 t.Fatal(err) 165 } 166 }