Skip to content

Commit 7ed97ec

Browse files
test(jsonfilter): add unit tests for BuildEntSelectorFromJSONFilter
Signed-off-by: Matías Insaurralde <matias@chainloop.dev>
1 parent 84e5e54 commit 7ed97ec

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

pkg/jsonfilter/jsonfilter_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// Copyright 2026 The Chainloop Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package jsonfilter
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestBuildEntSelectorFromJSONFilter(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
filter *JSONFilter
29+
wantErr string
30+
}{
31+
{
32+
name: "missing column",
33+
filter: &JSONFilter{Operator: OpEQ, Value: "foo"},
34+
wantErr: "invalid filter: column and operator are required",
35+
},
36+
{
37+
name: "missing operator",
38+
filter: &JSONFilter{Column: "metadata", Value: "foo"},
39+
wantErr: "invalid filter: column and operator are required",
40+
},
41+
{
42+
name: "unsupported operator",
43+
filter: &JSONFilter{Column: "metadata", Operator: "gt", Value: "foo"},
44+
wantErr: "unsupported operator: gt",
45+
},
46+
{
47+
name: "eq operator with string value",
48+
filter: &JSONFilter{Column: "metadata", FieldPath: "name", Operator: OpEQ, Value: "foo"},
49+
},
50+
{
51+
name: "eq operator with nested field path",
52+
filter: &JSONFilter{Column: "metadata", FieldPath: "labels.env", Operator: OpEQ, Value: "prod"},
53+
},
54+
{
55+
name: "eq operator with empty field path",
56+
filter: &JSONFilter{Column: "metadata", FieldPath: "", Operator: OpEQ, Value: "foo"},
57+
},
58+
{
59+
name: "neq operator with string value",
60+
filter: &JSONFilter{Column: "metadata", FieldPath: "name", Operator: OpNEQ, Value: "bar"},
61+
},
62+
{
63+
name: "in operator with single value",
64+
filter: &JSONFilter{Column: "metadata", FieldPath: "env", Operator: OpIN, Value: "prod"},
65+
},
66+
{
67+
name: "in operator with comma-separated values",
68+
filter: &JSONFilter{Column: "metadata", FieldPath: "env", Operator: OpIN, Value: "prod,staging,dev"},
69+
},
70+
{
71+
name: "in operator trims spaces around values",
72+
filter: &JSONFilter{Column: "metadata", FieldPath: "env", Operator: OpIN, Value: "prod, staging , dev"},
73+
},
74+
{
75+
name: "in operator with non-string value",
76+
filter: &JSONFilter{Column: "metadata", FieldPath: "env", Operator: OpIN, Value: 42},
77+
wantErr: "invalid value for 'in' operator: must be a slice of strings",
78+
},
79+
{
80+
name: "in operator with slice value instead of string",
81+
filter: &JSONFilter{Column: "metadata", FieldPath: "env", Operator: OpIN, Value: []string{"prod", "dev"}},
82+
wantErr: "invalid value for 'in' operator: must be a slice of strings",
83+
},
84+
}
85+
86+
for _, tc := range tests {
87+
t.Run(tc.name, func(t *testing.T) {
88+
pred, err := BuildEntSelectorFromJSONFilter(tc.filter)
89+
if tc.wantErr != "" {
90+
require.EqualError(t, err, tc.wantErr)
91+
assert.Nil(t, pred)
92+
} else {
93+
require.NoError(t, err)
94+
assert.NotNil(t, pred)
95+
}
96+
})
97+
}
98+
}

0 commit comments

Comments
 (0)