Skip to content

Commit 025e48e

Browse files
committed
fix: increased coverage for builder
1 parent 8700ba5 commit 025e48e

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

listener/apischema/builder_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77

88
"github.com/openmfp/kubernetes-graphql-gateway/common"
99
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
10+
"k8s.io/apimachinery/pkg/api/meta"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/runtime/schema"
1113
"k8s.io/client-go/openapi"
1214
"k8s.io/kube-openapi/pkg/validation/spec"
1315
)
@@ -16,16 +18,32 @@ type fakeClient struct {
1618
paths map[string]openapi.GroupVersion
1719
}
1820

21+
type fakeErrClient struct{}
22+
23+
type fakeRESTMapper struct {
24+
isNamespaced bool
25+
err error
26+
}
27+
1928
func (f *fakeClient) Paths() (map[string]openapi.GroupVersion, error) {
2029
return f.paths, nil
2130
}
2231

23-
type fakeErrClient struct{}
24-
2532
func (f *fakeErrClient) Paths() (map[string]openapi.GroupVersion, error) {
2633
return nil, errors.New("fail Paths")
2734
}
2835

36+
func (f *fakeRESTMapper) RESTMapping(gk metav1.GroupKind, versions ...string) (*meta.RESTMapping, error) {
37+
if f.err != nil {
38+
return nil, f.err
39+
}
40+
scope := meta.RESTScopeRoot
41+
if f.isNamespaced {
42+
scope = meta.RESTScopeNamespace
43+
}
44+
return &meta.RESTMapping{Scope: scope}, nil
45+
}
46+
2947
// TestGetOpenAPISchemaKey tests the getOpenAPISchemaKey function. It checks if the
3048
// function correctly formats the GroupVersionKind into the expected schema key format.
3149
func TestGetOpenAPISchemaKey(t *testing.T) {
@@ -269,3 +287,40 @@ func TestWithApiResourceCategories(t *testing.T) {
269287
})
270288
}
271289
}
290+
291+
// TestWithScope tests the WithScope method for the SchemaBuilder struct.
292+
func TestWithScope(t *testing.T) {
293+
gvk := schema.GroupVersionKind{Group: "g", Version: "v1", Kind: "K"}
294+
295+
// Create schema with GVK extension
296+
s := &spec.Schema{
297+
VendorExtensible: spec.VendorExtensible{
298+
Extensions: map[string]interface{}{
299+
common.GVKExtensionKey: []map[string]string{
300+
{"group": gvk.Group, "version": gvk.Version, "kind": gvk.Kind},
301+
},
302+
},
303+
},
304+
}
305+
306+
b := &SchemaBuilder{
307+
schemas: map[string]*spec.Schema{
308+
"g.v1.K": s,
309+
},
310+
}
311+
312+
// Create RESTMapper and mark GVK as namespaced
313+
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{gvk.GroupVersion()})
314+
mapper.Add(gvk, meta.RESTScopeNamespace)
315+
316+
b.WithScope(mapper)
317+
318+
// Validate
319+
scope, ok := b.schemas["g.v1.K"].VendorExtensible.Extensions[common.ScopeExtensionKey]
320+
if !ok {
321+
t.Fatal("expected ScopeExtensionKey to be set")
322+
}
323+
if scope != apiextensionsv1.NamespaceScoped {
324+
t.Errorf("expected NamespaceScoped, got %v", scope)
325+
}
326+
}

0 commit comments

Comments
 (0)