Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql: handle unknown property in MapFilter #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ License.
package graphql

import (
"errors"
"fmt"

"github.com/openshift-kni/oran-o2ims/internal/model"
Expand Down Expand Up @@ -99,6 +100,9 @@ func (t FilterTerm) MapFilter(mapPropertyFunc func(string) string) (searchFilter

// Convert to GraphQL property
searchProperty := mapPropertyFunc(t.Path[0])
if searchProperty == "" {
return nil, errors.New("unknown GraphQL property")
}

// Build search filter
searchFilter = &model.SearchFilter{
Expand Down
31 changes: 28 additions & 3 deletions internal/graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ License.
package graphql

import (
"errors"

"github.com/openshift-kni/oran-o2ims/internal/model"
"github.com/openshift-kni/oran-o2ims/internal/search"

Expand All @@ -27,10 +29,14 @@ import (
var _ = Describe("GraphQL filters", func() {
DescribeTable(
"Map a filter term to a SearchFilter",
func(term search.Term, expected *model.SearchFilter, mapPropertyFunc func(string) string) {
func(term search.Term, expectedFilter *model.SearchFilter, expectedErr error, mapPropertyFunc func(string) string) {
actual, err := FilterTerm(term).MapFilter(mapPropertyFunc)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(expected))
Expect(actual).To(Equal(expectedFilter))
if expectedErr != nil {
Expect(err).To(HaveOccurred())
} else {
Expect(err).ToNot(HaveOccurred())
}
},
Entry(
"Filter term for Cluster",
Expand All @@ -47,6 +53,7 @@ var _ = Describe("GraphQL filters", func() {
Property: "cluster",
Values: []*string{ptr.To("=spoke0")},
},
nil,
func(s string) string {
return PropertyCluster(s).MapProperty()
},
Expand All @@ -66,6 +73,24 @@ var _ = Describe("GraphQL filters", func() {
Property: "cluster",
Values: []*string{ptr.To("=spoke0")},
},
nil,
func(s string) string {
return PropertyNode(s).MapProperty()
},
),
Entry(
"Fail when an unknown property is specified",
search.Term{
Operator: search.Eq,
Path: []string{
"location",
},
Values: []any{
"EU",
},
},
nil,
errors.New("unknown GraphQL property"),
func(s string) string {
return PropertyNode(s).MapProperty()
},
Expand Down
Loading