-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_test.go
128 lines (109 loc) · 5.03 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
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rosmar
import (
"errors"
"testing"
sgbucket "github.com/couchbase/sg-bucket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetDDocs(t *testing.T) {
ctx := testCtx(t)
ensureNoLeakedFeeds(t)
ddoc := sgbucket.DesignDoc{Views: sgbucket.ViewMap{"view1": sgbucket.ViewDef{Map: `function(doc){if (doc.key) emit(doc.key,doc.value)}`}}}
coll := makeTestBucket(t).DefaultDataStore().(*Collection)
err := coll.PutDDoc(ctx, "docname", &ddoc)
assert.NoError(t, err, "PutDDoc docnamefailed")
err = coll.PutDDoc(ctx, "docname2", &ddoc)
assert.NoError(t, err, "PutDDoc docname2failed")
ddocs, getErr := coll.GetDDocs()
assert.NoError(t, getErr, "GetDDocs failed")
assert.Equal(t, len(ddocs), 2)
}
// Create a simple view and run it on some documents
func TestView(t *testing.T) {
ctx := testCtx(t)
ensureNoLeakedFeeds(t)
coll := makeTestBucket(t).DefaultDataStore().(*Collection)
ddoc := sgbucket.DesignDoc{Language: "javascript", Views: sgbucket.ViewMap{"view1": sgbucket.ViewDef{Map: `function(doc){if (doc.key) emit(doc.key,doc.value)}`}}}
err := coll.PutDDoc(ctx, "docname", &ddoc)
assert.NoError(t, err, "PutDDoc failed")
var echo sgbucket.DesignDoc
echo, err = coll.GetDDoc("docname")
require.NoError(t, err, "GetDDoc failed")
assert.Equal(t, ddoc, echo)
require.NoError(t, setJSON(coll, "doc1", `{"key": "k1", "value": "v1"}`))
require.NoError(t, setJSON(coll, "doc2", `{"key": "k2", "value": "v2"}`))
require.NoError(t, setJSON(coll, "doc3", `{"key": 17, "value": ["v3"]}`))
require.NoError(t, setJSON(coll, "doc4", `{"key": [17, false], "value": null}`))
require.NoError(t, setJSON(coll, "doc5", `{"key": [17, true], "value": null}`))
// raw docs and counters should not be indexed by views
_, err = coll.AddRaw("rawdoc", 0, []byte("this is raw data"))
require.NoError(t, err)
_, err = coll.Incr("counter", 1, 0, 0)
require.NoError(t, err)
options := map[string]interface{}{"stale": false}
result, err := coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
require.Equal(t, 5, result.TotalRows)
assert.Equal(t, &sgbucket.ViewRow{ID: "doc3", Key: 17.0, Value: []interface{}{"v3"}}, result.Rows[0])
assert.Equal(t, &sgbucket.ViewRow{ID: "doc1", Key: "k1", Value: "v1"}, result.Rows[1])
assert.Equal(t, &sgbucket.ViewRow{ID: "doc2", Key: "k2", Value: "v2"}, result.Rows[2])
assert.Equal(t, &sgbucket.ViewRow{ID: "doc4", Key: []interface{}{17.0, false}}, result.Rows[3])
assert.Equal(t, &sgbucket.ViewRow{ID: "doc5", Key: []interface{}{17.0, true}}, result.Rows[4])
// Try a startkey:
options["startkey"] = "k2"
options["include_docs"] = true
result, err = coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
assert.Equal(t, 3, result.TotalRows)
var expectedDoc interface{} = map[string]interface{}{"key": "k2", "value": "v2"}
assert.Equal(t, &sgbucket.ViewRow{ID: "doc2", Key: "k2", Value: "v2",
Doc: &expectedDoc}, result.Rows[0])
// Try descending:
options["descending"] = true
result, err = coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
assert.Equal(t, 3, result.TotalRows)
assert.Equal(t, "doc2", result.Rows[0].ID)
assert.Equal(t, "doc1", result.Rows[1].ID)
assert.Equal(t, "doc3", result.Rows[2].ID)
delete(options, "descending")
// Try an endkey:
options["endkey"] = "k2"
result, err = coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
assert.Equal(t, 1, result.TotalRows)
assert.Equal(t, &sgbucket.ViewRow{ID: "doc2", Key: "k2", Value: "v2",
Doc: &expectedDoc}, result.Rows[0])
// Try an endkey out of range:
options["endkey"] = "k999"
result, err = coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
assert.Equal(t, 1, result.TotalRows)
assert.Equal(t, &sgbucket.ViewRow{ID: "doc2", Key: "k2", Value: "v2",
Doc: &expectedDoc}, result.Rows[0])
// Try without inclusive_end:
options["endkey"] = "k2"
options["inclusive_end"] = false
result, err = coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
assert.Equal(t, 0, result.TotalRows)
// Try a single key:
options = map[string]interface{}{"stale": false, "key": "k2", "include_docs": true}
result, err = coll.View(ctx, "docname", "view1", options)
assert.NoError(t, err, "View call failed")
assert.Equal(t, 1, result.TotalRows)
assert.Equal(t, &sgbucket.ViewRow{ID: "doc2", Key: "k2", Value: "v2",
Doc: &expectedDoc}, result.Rows[0])
// Delete the design doc:
assert.NoError(t, coll.DeleteDDoc("docname"), "DeleteDDoc")
_, getErr := coll.GetDDoc("docname")
assert.True(t, errors.Is(getErr, sgbucket.MissingError{Key: "docname"}))
}