MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestCallValidateFunction

Function TestCallValidateFunction

db/event_handler_test.go:48–101  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

46}
47
48func TestCallValidateFunction(t *testing.T) {
49 // Boolean return type handling of CallValidateFunction; Mock up a document change event and
50 // filter function which returns a bool value while calling CallValidateFunction.
51 channels := base.SetFromArray([]string{"Netflix"})
52 docId, body, oldBodyJSON := "doc1", Body{BodyId: "doc1", "key1": "value1"}, ""
53 bodyBytes := base.MustJSONMarshal(t, body)
54 event := &DocumentChangeEvent{DocID: docId, DocBytes: bodyBytes, OldDoc: oldBodyJSON, Channels: channels}
55
56 ctx := base.TestCtx(t)
57 // Boolean return type handling of CallValidateFunction; bool true value.
58 source := `function(doc) { if (doc.key1 == "value1") { return true; } else { return false; } }`
59 filterFunc := NewJSEventFunction(ctx, source)
60 result, err := filterFunc.CallValidateFunction(ctx, event)
61 assert.True(t, result, "It should return true since doc.key1 is value1")
62 assert.NoError(t, err, "It should return boolean result")
63
64 // Boolean return type handling of CallValidateFunction; bool false value.
65 source = `function(doc) { if (doc.key1 == "value2") { return true; } else { return false; } }`
66 filterFunc = NewJSEventFunction(ctx, source)
67 result, err = filterFunc.CallValidateFunction(ctx, event)
68 assert.False(t, result, "It should return false since doc.key1 is not value2")
69 assert.NoError(t, err, "It should return boolean result")
70
71 // Parsable boolean string return type handling of CallValidateFunction.
72 source = `function(doc) { if (doc.key1 == "value1") { return "true"; } else { return "false"; } }`
73 filterFunc = NewJSEventFunction(ctx, source)
74 result, err = filterFunc.CallValidateFunction(ctx, event)
75 assert.True(t, result, "It should return true since doc.key1 is value1")
76 assert.NoError(t, err, "It should return parsable boolean result")
77
78 // Non parsable boolean string return type handling of CallValidateFunction.
79 source = `function(doc) { if (doc.key1 == "value1") { return "TrUe"; } else { return "false"; } }`
80 filterFunc = NewJSEventFunction(ctx, source)
81 result, err = filterFunc.CallValidateFunction(ctx, event)
82 assert.False(t, result, "It should return false since 'TrUe' is non parsable boolean string")
83 assert.Error(t, err, "It should return parsable throw ParseBool error")
84 assert.Contains(t, err.Error(), `invalid syntax`)
85
86 // Not boolean and not parsable boolean string return type handling of CallValidateFunction.
87 source = `function(doc) { if (doc.key1 == "Pi") { return 3.14; } else { return 0.0; } }`
88 filterFunc = NewJSEventFunction(ctx, source)
89 result, err = filterFunc.CallValidateFunction(ctx, event)
90 assert.False(t, result, "It should return not boolean and not parsable boolean string value")
91 assert.Error(t, err, "It should throw Validate function returned non-boolean value error")
92 assert.Contains(t, err.Error(), "Validate function returned non-boolean value.")
93
94 // Simulate CallFunction failure by making syntax error in filter function.
95 source = `function(doc) { invalidKeyword if (doc.key1 == "value1") { return true; } else { return false; } }`
96 filterFunc = NewJSEventFunction(ctx, source)
97 result, err = filterFunc.CallValidateFunction(ctx, event)
98 assert.False(t, result, "It should return false due to the syntax error in filter function")
99 assert.Error(t, err, "It should throw an error due to syntax error")
100 assert.Contains(t, err.Error(), "Unexpected token")
101}

Callers

nothing calls this directly

Calls 7

CallValidateFunctionMethod · 0.95
SetFromArrayFunction · 0.92
MustJSONMarshalFunction · 0.92
TestCtxFunction · 0.92
NewJSEventFunctionFunction · 0.85
ContainsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected