Skip to content

Commit 1c6bc74

Browse files
authored
Merge pull request #1718 from allencloud/add-ut
test: add unit test for filter validation
2 parents e97af77 + d5566fb commit 1c6bc74

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pkg/utils/filters/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func FromURLParam(param string) (map[string][]string, error) {
9696
func Validate(filter map[string][]string) error {
9797
for name := range filter {
9898
if _, exist := acceptedFilters[name]; !exist {
99-
return fmt.Errorf("Invalid filter %s", name)
99+
return fmt.Errorf("invalid filter %s", name)
100100
}
101101
}
102102

pkg/utils/filters/filter_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,51 @@ func TestParseFilter(t *testing.T) {
5050
}
5151
}
5252
}
53+
54+
func TestValidate(t *testing.T) {
55+
type args struct {
56+
filter map[string][]string
57+
}
58+
tests := []struct {
59+
name string
60+
args args
61+
wantErr bool
62+
}{
63+
{
64+
name: "normal successful case",
65+
args: args{
66+
filter: map[string][]string{
67+
"id": {"a"},
68+
},
69+
},
70+
wantErr: false,
71+
},
72+
{
73+
name: "failure case with nonexistence",
74+
args: args{
75+
filter: map[string][]string{
76+
"id": {"a"},
77+
"nonexistence": {"b"},
78+
},
79+
},
80+
wantErr: true,
81+
},
82+
{
83+
name: "failure case with future filter name",
84+
args: args{
85+
filter: map[string][]string{
86+
"id": {"a"},
87+
"before": {"b"}, // this will be implemented in the future.
88+
},
89+
},
90+
wantErr: true,
91+
},
92+
}
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
if err := Validate(tt.args.filter); (err != nil) != tt.wantErr {
96+
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
97+
}
98+
})
99+
}
100+
}

0 commit comments

Comments
 (0)