diff --git a/db_live_test.go b/db_live_test.go index c32c81a0..a427c0e8 100644 --- a/db_live_test.go +++ b/db_live_test.go @@ -52,16 +52,51 @@ func TestQueries_1_x(t *testing.T) { } } -// func TestQueries_2_x(t *testing.T) { -// dsn := os.Getenv("KIVIK_TEST_DSN_COUCH23") -// if dsn == "" { -// dsn = os.Getenv("KIVIK_TEST_DSN_COUCH22") -// } -// if dsn == "" { -// t.Skip("Neither KIVIK_TEST_DSN_COUCH22 nor KIVIK_TEST_DSN_COUCH23 configured") -// } +func TestQueries_2_x(t *testing.T) { + dsn := os.Getenv("KIVIK_TEST_DSN_COUCH23") + if dsn == "" { + dsn = os.Getenv("KIVIK_TEST_DSN_COUCH22") + } + if dsn == "" { + t.Skip("Neither KIVIK_TEST_DSN_COUCH22 nor KIVIK_TEST_DSN_COUCH23 configured") + } -// } + client, err := kivik.New("couch", dsn) + if err != nil { + t.Fatal(err) + } + + db := client.DB(context.Background(), "_users") + rows, err := db.AllDocs(context.Background(), map[string]interface{}{ + "queries": []map[string]interface{}{ + {}, + {}, + }, + }) + if err != nil { + t.Fatal(err) + } + defer rows.Close() // nolint:errcheck + result := make([]interface{}, 0) + for rows.Next() { + if rows.EOQ() { + result = append(result, map[string]interface{}{ + "EOQ": true, + "total_rows": rows.TotalRows(), + }) + continue + } + result = append(result, map[string]interface{}{ + "_id": rows.ID(), + }) + } + if err := rows.Err(); err != nil { + t.Fatal(err) + } + if d := testy.DiffInterface(testy.Snapshot(t), result); d != nil { + t.Error(d) + } +} // func TestQueries_3_x(t *testing.T) { // dsn := os.Getenv("KIVIK_TEST_DSN_COUCH30") diff --git a/iter.go b/iter.go index 4260a923..45878725 100644 --- a/iter.go +++ b/iter.go @@ -262,6 +262,10 @@ func (i *iter) nextRow(row interface{}) error { func (i *iter) Close() error { atomic.StoreInt32(&i.closed, 1) + // body will be nil if we're iterating over a multi-query resultset. + if i.body == nil { + return nil + } return i.body.Close() } diff --git a/rows.go b/rows.go index 9f99e59b..1e749aac 100644 --- a/rows.go +++ b/rows.go @@ -215,6 +215,7 @@ func (r *multiQueriesRows) begin() error { Closer: r.r, } r.rows = newRows(r.ctx, in).(*rows) + r.rows.body = nil r.rows.dec = json.NewDecoder(in) return r.rows.begin() } @@ -223,6 +224,7 @@ func (r *multiQueriesRows) begin() error { return err } r.rows = newRows(r.ctx, r.r).(*rows) + r.rows.body = nil r.rows.iter.dec = r.dec return r.rows.iter.begin() } @@ -249,6 +251,7 @@ func (r *multiQueriesRows) nextQuery() error { } r.queryIndex++ r.rows = rows + r.rows.body = nil return nil } diff --git a/testdata/TestQueries_2_x b/testdata/TestQueries_2_x new file mode 100644 index 00000000..82ee1338 --- /dev/null +++ b/testdata/TestQueries_2_x @@ -0,0 +1,16 @@ +([]interface {}) (len=4) { + (map[string]interface {}) (len=1) { + (string) (len=3) "_id": (string) (len=13) "_design/_auth" + }, + (map[string]interface {}) (len=2) { + (string) (len=3) "EOQ": (bool) true, + (string) (len=10) "total_rows": (int64) 1 + }, + (map[string]interface {}) (len=1) { + (string) (len=3) "_id": (string) (len=13) "_design/_auth" + }, + (map[string]interface {}) (len=2) { + (string) (len=3) "EOQ": (bool) true, + (string) (len=10) "total_rows": (int64) 1 + } +}