/
get.go
104 lines (94 loc) · 2.43 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package db
import (
"context"
"github.com/flimzy/diff"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/test/kt"
)
func init() {
kt.Register("Get", get)
}
type testDoc struct {
ID string `json:"_id"`
Rev string `json:"_rev,omitempty"`
Name string `json:"name"`
Age int `json:"age"`
}
func get(ctx *kt.Context) {
ctx.RunRW(func(ctx *kt.Context) {
dbName := ctx.TestDB()
defer ctx.Admin.DestroyDB(context.Background(), dbName, ctx.Options("db"))
db, err := ctx.Admin.DB(context.Background(), dbName, ctx.Options("db"))
if err != nil {
ctx.Fatalf("Failed to connect to test db: %s", err)
}
doc := &testDoc{
ID: "bob",
Name: "Robert",
Age: 32,
}
rev, err := db.Put(context.Background(), doc.ID, doc)
if err != nil {
ctx.Fatalf("Failed to create doc in test db: %s", err)
}
doc.Rev = rev
ddoc := &testDoc{
ID: "_design/foo",
Name: "Designer",
}
rev, err = db.Put(context.Background(), ddoc.ID, ddoc)
if err != nil {
ctx.Fatalf("Failed to create design doc in test db: %s", err)
}
ddoc.Rev = rev
local := &testDoc{
ID: "_local/foo",
Name: "Designer",
}
rev, err = db.Put(context.Background(), local.ID, local)
if err != nil {
ctx.Fatalf("Failed to create local doc in test db: %s", err)
}
local.Rev = rev
ctx.Run("group", func(ctx *kt.Context) {
ctx.RunAdmin(func(ctx *kt.Context) {
ctx.Parallel()
db, err := ctx.Admin.DB(context.Background(), dbName, ctx.Options("db"))
if !ctx.IsExpectedSuccess(err) {
return
}
testGet(ctx, db, doc)
testGet(ctx, db, ddoc)
testGet(ctx, db, local)
testGet(ctx, db, &testDoc{ID: "bogus"})
})
ctx.RunNoAuth(func(ctx *kt.Context) {
ctx.Parallel()
db, err := ctx.NoAuth.DB(context.Background(), dbName, ctx.Options("db"))
if !ctx.IsExpectedSuccess(err) {
return
}
testGet(ctx, db, doc)
testGet(ctx, db, ddoc)
testGet(ctx, db, local)
testGet(ctx, db, &testDoc{ID: "bogus"})
})
})
})
}
func testGet(ctx *kt.Context, db *kivik.DB, expectedDoc *testDoc) {
ctx.Run(expectedDoc.ID, func(ctx *kt.Context) {
ctx.Parallel()
row, err := db.Get(context.Background(), expectedDoc.ID)
if !ctx.IsExpectedSuccess(err) {
return
}
doc := &testDoc{}
if err = row.ScanDoc(&doc); err != nil {
ctx.Fatalf("Failed to scan doc: %s", err)
}
if d := diff.AsJSON(expectedDoc, doc); d != "" {
ctx.Errorf("Fetched document not as expected:\n%s\n", d)
}
})
}