-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjs_map_fn_test.go
121 lines (106 loc) · 4.53 KB
/
js_map_fn_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
// Copyright 2013-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 sgbucket
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func testCtx(t *testing.T) context.Context {
return context.Background() // return background for tests, to match sync_gateway interfaces
}
// Just verify that the calls to the emit() fn show up in the output.
func TestEmitFunction(t *testing.T) {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc) {emit("key", "value"); emit("k2","v2")}`, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{}`, "doc1", 0, 0, nil})
assertNoError(t, err, "CallFunction failed")
assert.Equal(t, 2, len(rows))
assert.Equal(t, &ViewRow{ID: "doc1", Key: "key", Value: "value"}, rows[0])
assert.Equal(t, &ViewRow{ID: "doc1", Key: "k2", Value: "v2"}, rows[1])
}
func TestTimeout(t *testing.T) {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc) {while(true) {}}`, 1)
_, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{}`, "doc1", 0, 0, nil})
assert.ErrorIs(t, err, ErrJSTimeout)
}
func testMap(t *testing.T, mapFn string, doc string) []*ViewRow {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, mapFn, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{doc, "doc1", 0, 0, nil})
assertNoError(t, err, fmt.Sprintf("CallFunction failed on %s", doc))
return rows
}
// Now just make sure the input comes through intact
func TestInputParse(t *testing.T) {
rows := testMap(t, `function(doc) {emit(doc.key, doc.value);}`,
`{"key": "k", "value": "v"}`)
assert.Equal(t, 1, len(rows))
assert.Equal(t, &ViewRow{ID: "doc1", Key: "k", Value: "v"}, rows[0])
}
// Test different types of keys/values:
func TestKeyTypes(t *testing.T) {
rows := testMap(t, `function(doc) {emit(doc.key, doc.value);}`,
`{"ID": "doc1", "key": true, "value": false}`)
assert.Equal(t, &ViewRow{ID: "doc1", Key: true, Value: false}, rows[0])
rows = testMap(t, `function(doc) {emit(doc.key, doc.value);}`,
`{"ID": "doc1", "key": null, "value": 0}`)
assert.Equal(t, &ViewRow{ID: "doc1", Key: nil, Value: float64(0)}, rows[0])
rows = testMap(t, `function(doc) {emit(doc.key, doc.value);}`,
`{"ID": "doc1", "key": ["foo", 23, []], "value": [null]}`)
assert.Equal(t, &ViewRow{
ID: "doc1",
Key: []interface{}{"foo", 23.0, []interface{}{}},
Value: []interface{}{nil},
}, rows[0])
}
// Empty/no-op map fn
func TestEmptyJSMapFunction(t *testing.T) {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc) {}`, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{"key": "k", "value": "v"}`, "doc1", 0, 0, nil})
assertNoError(t, err, "CallFunction failed")
assert.Equal(t, 0, len(rows))
}
// Test meta object
func TestMeta(t *testing.T) {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc,meta) {if (meta.id!="doc1") throw("bad ID");}`, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{"key": "k", "value": "v"}`, "doc1", 0, 0, nil})
assertNoError(t, err, "CallFunction failed")
assert.Equal(t, 0, len(rows))
}
func TestXattrs(t *testing.T) {
xattrs := map[string][]byte{
"_sync": []byte(`{"hey":"hey"}`),
"user": []byte(`{"a":1}`),
}
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc,meta) {if (meta.xattrs._sync.hey != "hey") throw("bad xattrs");}`, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{"key": "k", "value": "v"}`, "doc1", 0, 0, xattrs})
assertNoError(t, err, "CallFunction failed")
assert.Equal(t, 0, len(rows))
}
func TestNoXattrs(t *testing.T) {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc,meta) {if (meta.xattrs !== undefined) throw("unexpected xattrs");}`, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{"key": "k", "value": "v"}`, "doc1", 0, 0, nil})
assertNoError(t, err, "CallFunction failed")
assert.Equal(t, 0, len(rows))
}
// Test the public API
func TestPublicJSMapFunction(t *testing.T) {
ctx := testCtx(t)
mapper := NewJSMapFunction(ctx, `function(doc) {emit(doc.key, doc.value);}`, 0)
rows, err := mapper.CallFunction(ctx, &JSMapFunctionInput{`{"key": "k", "value": "v"}`, "doc1", 0, 0, nil})
assertNoError(t, err, "CallFunction failed")
assert.Equal(t, 1, len(rows))
assert.Equal(t, &ViewRow{ID: "doc1", Key: "k", Value: "v"}, rows[0])
}