Skip to content

Commit 420ad10

Browse files
committed
fix: mockery and asserts changes
1 parent e066519 commit 420ad10

File tree

6 files changed

+140
-188
lines changed

6 files changed

+140
-188
lines changed

listener/apischema/builder_test.go

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package apischema
22

33
import (
4-
"encoding/json"
54
"errors"
65
"testing"
76

87
"github.com/openmfp/kubernetes-graphql-gateway/common"
8+
apischemaMocks "github.com/openmfp/kubernetes-graphql-gateway/listener/apischema/mocks"
99
"github.com/stretchr/testify/assert"
1010
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1111
"k8s.io/apimachinery/pkg/api/meta"
@@ -15,20 +15,6 @@ import (
1515
"k8s.io/kube-openapi/pkg/validation/spec"
1616
)
1717

18-
type fakeClient struct {
19-
paths map[string]openapi.GroupVersion
20-
}
21-
22-
type fakeErrClient struct{}
23-
24-
func (f *fakeClient) Paths() (map[string]openapi.GroupVersion, error) {
25-
return f.paths, nil
26-
}
27-
28-
func (f *fakeErrClient) Paths() (map[string]openapi.GroupVersion, error) {
29-
return nil, errors.New("fail Paths")
30-
}
31-
3218
// TestGetOpenAPISchemaKey tests the getOpenAPISchemaKey function. It checks if the
3319
// function correctly formats the GroupVersionKind into the expected schema key format.
3420
func TestGetOpenAPISchemaKey(t *testing.T) {
@@ -103,33 +89,52 @@ func TestGetCRDGroupVersionKind(t *testing.T) {
10389
// and the expected schema key.
10490
func TestNewSchemaBuilder(t *testing.T) {
10591
tests := []struct {
106-
name string
107-
client openapi.Client
108-
wantLen int
109-
wantKey string
110-
wantError bool
92+
name string
93+
client openapi.Client
94+
wantErr error
95+
wantLen int
96+
wantKey string
11197
}{
11298
{
11399
name: "populates_schemas",
114-
client: &fakeClient{paths: map[string]openapi.GroupVersion{"/X/v1": fakeGV{data: func() []byte {
115-
d, _ := json.Marshal(&schemaResponse{
116-
Components: schemasComponentsWrapper{Schemas: map[string]*spec.Schema{"X.v1.K": {}}}})
117-
return d
118-
}(), err: nil}}},
100+
client: func() openapi.Client {
101+
mock := apischemaMocks.NewMockClient(t)
102+
mockGV := apischemaMocks.NewMockGroupVersion(t)
103+
paths := map[string]openapi.GroupVersion{
104+
"/v1": mockGV,
105+
}
106+
mock.EXPECT().Paths().Return(paths, nil)
107+
mockGV.EXPECT().Schema("application/json").Return([]byte(`{
108+
"components": {
109+
"schemas": {
110+
"v1.Pod": {
111+
"type": "object",
112+
"x-kubernetes-group-version-kind": [{"group": "", "kind": "Pod", "version": "v1"}]
113+
}
114+
}
115+
}
116+
}`), nil)
117+
return mock
118+
}(),
119+
wantErr: nil,
119120
wantLen: 1,
120-
wantKey: "X.v1.K",
121+
wantKey: "v1.Pod",
121122
},
122123
{
123-
name: "error_on_Paths",
124-
client: &fakeErrClient{},
125-
wantLen: 0,
126-
wantError: true,
124+
name: "error_on_Paths",
125+
client: func() openapi.Client {
126+
mock := apischemaMocks.NewMockClient(t)
127+
mock.EXPECT().Paths().Return(nil, errors.New("paths error"))
128+
return mock
129+
}(),
130+
wantErr: ErrGetOpenAPIPaths,
127131
},
128132
}
133+
129134
for _, tc := range tests {
130135
t.Run(tc.name, func(t *testing.T) {
131-
b := NewSchemaBuilder(tc.client, []string{"X/v1"})
132-
if tc.wantError {
136+
b := NewSchemaBuilder(tc.client, []string{"v1"})
137+
if tc.wantErr != nil {
133138
assert.NotNil(t, b.err, "expected error, got nil")
134139
assert.Equal(t, 0, len(b.schemas), "expected 0 schemas on error")
135140
return
@@ -144,7 +149,8 @@ func TestNewSchemaBuilder(t *testing.T) {
144149
}
145150

146151
// TestWithCRDCategories tests the WithCRDCategories method
147-
// for the SchemaBuilder struct. It checks if the categories are correctly added to the schema's extensions.
152+
// for the SchemaBuilder struct. It checks if the categories are correctly added
153+
// to the schema's extensions.
148154
func TestWithCRDCategories(t *testing.T) {
149155
tests := []struct {
150156
name string
@@ -190,14 +196,10 @@ func TestWithCRDCategories(t *testing.T) {
190196
b.WithCRDCategories(tc.crd)
191197
ext, found := b.schemas[tc.key].VendorExtensible.Extensions[common.CategoriesExtensionKey]
192198
if tc.wantCats == nil {
193-
if found {
194-
t.Errorf("expected no categories, but found: %#v", ext)
195-
}
199+
assert.False(t, found, "expected no categories")
196200
return
197201
}
198-
if !found {
199-
t.Fatal("expected CategoriesExtensionKey to be set")
200-
}
202+
assert.True(t, found, "expected CategoriesExtensionKey to be set")
201203
cats, ok := ext.([]string)
202204
assert.True(t, ok, "categories should be []string")
203205
assert.Equal(t, tc.wantCats, cats, "categories mismatch")
@@ -206,7 +208,8 @@ func TestWithCRDCategories(t *testing.T) {
206208
}
207209

208210
// TestWithApiResourceCategories tests the WithApiResourceCategories method
209-
// for the SchemaBuilder struct. It checks if the categories are correctly added to the schema's extensions.
211+
// for the SchemaBuilder struct. It checks if the categories are correctly added
212+
// to the schema's extensions.
210213
func TestWithApiResourceCategories(t *testing.T) {
211214
tests := []struct {
212215
name string
@@ -241,14 +244,10 @@ func TestWithApiResourceCategories(t *testing.T) {
241244
b.WithApiResourceCategories(tc.list)
242245
ext, found := b.schemas[tc.key].VendorExtensible.Extensions[common.CategoriesExtensionKey]
243246
if tc.wantCats == nil {
244-
if found {
245-
t.Errorf("expected no categories, but found: %#v", ext)
246-
}
247+
assert.False(t, found, "expected no categories")
247248
return
248249
}
249-
if !found {
250-
t.Fatal("expected CategoriesExtensionKey to be set by WithApiResourceCategories")
251-
}
250+
assert.True(t, found, "expected CategoriesExtensionKey to be set")
252251
cats, ok := ext.([]string)
253252
assert.True(t, ok, "categories should be []string")
254253
assert.Equal(t, tc.wantCats, cats, "categories mismatch")

0 commit comments

Comments
 (0)