-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_test.go
135 lines (112 loc) · 2.99 KB
/
view_test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package sofa
import (
"fmt"
"testing"
"github.com/nbio/st"
"github.com/h2non/gock"
)
const TestViewFunc = `function(doc) {
if (doc.type !== undefined && doc.name !== undefined) {
emit(doc.type, doc.name)
}
}`
func TestTemporaryView(t *testing.T) {
defer gock.Off()
// TODO: Check the data which gets sent
gock.New(fmt.Sprintf("https://%s", globalTestConnections.Version1MockHost)).
Post("/view_test_db/_temp_view").
BodyString(`{"map":"function(doc) {\n if (doc.type !== undefined \u0026\u0026 doc.name !== undefined) {\n emit(doc.type, doc.name)\n }\n}"}`).
Reply(200).
JSON(map[string]interface{}{
"total_rows": 2,
"offset": 0,
"rows": []map[string]interface{}{
{
"id": "fruit1",
"key": "fruit",
"value": "apple",
},
{
"id": "fruit2",
"key": "fruit",
"value": "apple",
},
},
})
con := globalTestConnections.Version1(t, true)
db := con.Database("view_test_db")
view := db.TemporaryView(TestViewFunc)
result, err := view.Execute(ViewParams{})
st.Assert(t, err, nil)
st.Assert(t, result.TotalRows, float64(2))
st.Assert(t, result.Offset, float64(0))
}
func TestNamedView(t *testing.T) {
defer gock.Off()
gock.New(fmt.Sprintf("https://%s", globalTestConnections.Version1MockHost)).
Get("/view_test_db/_design/things/_view/byType").
MatchParam("reduce", "false").
Reply(200).
JSON(map[string]interface{}{
"total_rows": 2,
"offset": 0,
"rows": []map[string]interface{}{
{
"id": "fruit1",
"key": "fruit",
"value": "apple",
},
{
"id": "fruit2",
"key": "fruit",
"value": "apple",
},
},
})
con := globalTestConnections.Version1(t, true)
db := con.Database("view_test_db")
view := db.NamedView("things", "byType")
result, err := view.Execute(ViewParams{Reduce: False})
st.Assert(t, err, nil)
st.Assert(t, result.TotalRows, float64(2))
st.Assert(t, result.Offset, float64(0))
}
// TODO: Currently only tests a temporary view. Would be nice to also create a
// named view and test that too.
func TestViewReal(t *testing.T) {
con := globalTestConnections.Version1(t, false)
// Delete the DB if it currently exists
if err := con.DeleteDatabase("feed_test_db"); err != nil {
if !ErrorStatus(err, 404) {
t.Fatal(err)
}
}
db, err := con.CreateDatabase("feed_test_db")
st.Assert(t, err, nil)
defer func() {
if err := con.DeleteDatabase("feed_test_db"); err != nil {
t.Fatal(err)
}
}()
appleDoc := getDefaultTestDoc()
kiwiDoc := &struct {
DocumentMetadata
Name string `json:"name"`
Type string `json:"type"`
}{
DocumentMetadata: DocumentMetadata{
ID: "fruit2",
},
Name: "kiwi",
Type: "fruit",
}
_, err = db.Put(appleDoc)
st.Assert(t, err, nil)
_, err = db.Put(kiwiDoc)
st.Assert(t, err, nil)
view := db.TemporaryView(TestViewFunc)
result, err := view.Execute(ViewParams{})
st.Assert(t, err, nil)
st.Assert(t, result.TotalRows, float64(2))
st.Assert(t, result.Offset, float64(0))
}