Skip to content

Commit

Permalink
Adding tests for cel expressions demonstrating use of has
Browse files Browse the repository at this point in the history
I was reading through this test to get a handle for the type of expressions I could
use in overlays and filter expressions and noticed that there weren't any tests
demonstrating the capabilities discussed in #890.

This change adds two tests within the existing structure of the cel_test to help
this file be used as a place to go for example expressions that should work or,
alternative, fail.
  • Loading branch information
jmcshane committed Feb 6, 2021
1 parent 6654c3d commit e8d3dac
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/interceptors/cel/cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ func TestInterceptor_Process(t *testing.T) {
"other": "thing",
},
},
}, {
name: "demonstrate defaulting logic within cel interceptor",
CEL: &triggersv1.CELInterceptor{
Overlays: []triggersv1.CELOverlay{
{Key: "one", Expression: "has(body.value) ? body.value : 'default'"},
{Key: "two", Expression: "has(body.test.first) ? body.test.first : 'default'"},
},
},
body: json.RawMessage(`{"value":"test","test":{"other":"thing"}}`),
wantExtensions: map[string]interface{}{
"one": "test",
"two": "default",
},
}, {
name: "filters and overlays can access passed in extensions",
CEL: &triggersv1.CELInterceptor{
Expand Down Expand Up @@ -696,6 +709,11 @@ func TestExpressionEvaluation_Error(t *testing.T) {
expr: "body.value.marshalJSON()",
want: "unexpected type 'string' passed to marshalJSON",
},
{
name: "has function missing nested key",
expr: "has(body.pull_request.repository.owner) ? body.pull_request.repository.owner : 'me'",
want: `failed to evaluate: no such key: repository`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(rt *testing.T) {
Expand Down

0 comments on commit e8d3dac

Please sign in to comment.