forked from dustin/go-couch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
views_test.go
135 lines (117 loc) · 3.31 KB
/
views_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 couch
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
)
func TestQueryNoView(t *testing.T) {
d := Database{}
err := d.Query("", nil, nil)
if err != errEmptyView {
t.Errorf("Expected empty view error, got %v", err)
}
}
func TestQueryIdsNoView(t *testing.T) {
d := Database{}
x, err := d.QueryIds("", nil)
if err != errEmptyView {
t.Errorf("Expected empty view error, got %v/%v", x, err)
}
}
func TestViewURL(t *testing.T) {
vpath := "_design/testview/_view/v"
// Unmarshallable parameter
d := Database{Host: "localhost", Port: "5984"}
u, err := d.ViewURL(vpath, map[string]interface{}{"ch": make(chan bool)})
if err == nil {
t.Errorf("Expected error on unmarshalable param, got %v", u)
}
tests := []struct {
params map[string]interface{}
exp map[string]string
}{
{map[string]interface{}{"i": 1, "b": true, "s": "ess"},
map[string]string{"i": "1", "b": "true", "s": `"ess"`}},
{map[string]interface{}{"unk": DocID("le"), "startkey_docid": "ess"},
map[string]string{"unk": "le", "startkey_docid": "ess"}},
{map[string]interface{}{"stale": "update_after"},
map[string]string{"stale": "update_after"}},
{map[string]interface{}{"startkey": []string{"a"}},
map[string]string{"startkey": `["a"]`}},
}
for _, test := range tests {
us, err := d.ViewURL(vpath, test.params)
if err != nil {
t.Errorf("Failed on %v: %v", test, err)
continue
}
u, err := url.Parse(us)
if err != nil {
t.Errorf("Failed on %v", test)
continue
}
got := u.Query()
if len(got) != len(test.exp) {
t.Errorf("Expected %v, got %v", test.exp, got)
continue
}
for k, v := range test.exp {
if len(got[k]) != 1 || got.Get(k) != v {
t.Errorf("Expected param %v to be %q on %v, was %#q",
k, v, test, got[k])
}
}
}
}
func TestBadViewParam(t *testing.T) {
d := Database{Host: "localhost", Port: "5984"}
thing, err := d.ViewURL("aview", map[string]interface{}{
"aparam": make(chan bool),
})
if err == nil {
t.Errorf("Failed to build a view with a bad param, got %v",
thing)
}
}
func TestQueryBadViewParam(t *testing.T) {
d := Database{Host: "localhost", Port: "5984"}
ob := map[string]interface{}{}
err := d.Query("aview", map[string]interface{}{
"aparam": make(chan bool),
}, &ob)
if err == nil {
t.Errorf("Failed to build a view with a bad param, got %v", ob)
}
}
func TestQuerySuccess(t *testing.T) {
defer uninstallFakeHTTP(installFakeHTTP(oneFake(http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`{"k": "v"}`)),
})))
d := Database{Host: "localhost", Port: "5984"}
ob := map[string]interface{}{}
err := d.Query("aview", map[string]interface{}{}, &ob)
if err != nil {
t.Errorf("Failed to execute a view: %v", err)
}
if ob["k"] != "v" {
t.Fatalf("Expected v, got %q", ob["k"])
}
}
func TestQueryIDsSuccess(t *testing.T) {
hres := `{"rows": [{"id": "one"}, {}, {"id": "three"}]}`
defer uninstallFakeHTTP(installFakeHTTP(oneFake(http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(hres)),
})))
d := Database{Host: "localhost", Port: "5984"}
ids, err := d.QueryIds("aview", map[string]interface{}{})
if err != nil {
t.Errorf("Failed to execute a view: %v", err)
}
if len(ids) != 2 || ids[0] != "one" || ids[1] != "three" {
t.Fatalf("Didn't get expected IDs: %v", ids)
}
}