Skip to content

Commit 507e3e4

Browse files
committed
fix: builder_test now use a consistent assertion style
1 parent c240e0f commit 507e3e4

File tree

1 file changed

+15
-36
lines changed

1 file changed

+15
-36
lines changed

listener/apischema/builder_test.go

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/openmfp/kubernetes-graphql-gateway/common"
9+
"github.com/stretchr/testify/assert"
910
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1011
"k8s.io/apimachinery/pkg/api/meta"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -47,9 +48,7 @@ func TestGetOpenAPISchemaKey(t *testing.T) {
4748

4849
for _, tc := range tests {
4950
got := getOpenAPISchemaKey(tc.gvk)
50-
if got != tc.want {
51-
t.Errorf("getOpenAPISchemaKey(%+v) = %q; want %q", tc.gvk, got, tc.want)
52-
}
51+
assert.Equal(t, tc.want, got, "getOpenAPISchemaKey(%+v) result mismatch", tc.gvk)
5352
}
5453
}
5554

@@ -90,18 +89,11 @@ func TestGetCRDGroupVersionKind(t *testing.T) {
9089
for _, tc := range tests {
9190
t.Run(tc.name, func(t *testing.T) {
9291
got, err := getCRDGroupVersionKind(tc.spec)
92+
assert.Equal(t, tc.wantErr, err, "error value mismatch")
9393
if tc.wantErr != nil {
94-
if err != tc.wantErr {
95-
t.Fatalf("expected error %v, got %v", tc.wantErr, err)
96-
}
9794
return
9895
}
99-
if err != nil {
100-
t.Fatalf("unexpected error: %v", err)
101-
}
102-
if got == nil || *got != *tc.want {
103-
t.Errorf("got %+v; want %+v", got, tc.want)
104-
}
96+
assert.Equal(t, tc.want, got, "result value mismatch")
10597
})
10698
}
10799
}
@@ -137,21 +129,14 @@ func TestNewSchemaBuilder(t *testing.T) {
137129
t.Run(tc.name, func(t *testing.T) {
138130
b := NewSchemaBuilder(tc.client, []string{"X/v1"})
139131
if tc.wantError {
140-
if b.err == nil {
141-
t.Error("expected error, got nil")
142-
}
143-
if len(b.schemas) != 0 {
144-
t.Errorf("expected 0 schemas on error, got %d", len(b.schemas))
145-
}
132+
assert.NotNil(t, b.err, "expected error, got nil")
133+
assert.Equal(t, 0, len(b.schemas), "expected 0 schemas on error")
146134
return
147135
}
148-
if len(b.schemas) != tc.wantLen {
149-
t.Fatalf("expected %d schema entry, got %d", tc.wantLen, len(b.schemas))
150-
}
136+
assert.Equal(t, tc.wantLen, len(b.schemas), "schema count mismatch")
151137
if tc.wantKey != "" {
152-
if _, ok := b.schemas[tc.wantKey]; !ok {
153-
t.Errorf("schema key %s not found in builder.schemas", tc.wantKey)
154-
}
138+
_, ok := b.schemas[tc.wantKey]
139+
assert.True(t, ok, "schema key %s not found in builder.schemas", tc.wantKey)
155140
}
156141
})
157142
}
@@ -213,9 +198,8 @@ func TestWithCRDCategories(t *testing.T) {
213198
t.Fatal("expected CategoriesExtensionKey to be set")
214199
}
215200
cats, ok := ext.([]string)
216-
if !ok || len(cats) != len(tc.wantCats) || cats[0] != tc.wantCats[0] {
217-
t.Errorf("unexpected categories: %#v", ext)
218-
}
201+
assert.True(t, ok, "categories should be []string")
202+
assert.Equal(t, tc.wantCats, cats, "categories mismatch")
219203
})
220204
}
221205
}
@@ -265,9 +249,8 @@ func TestWithApiResourceCategories(t *testing.T) {
265249
t.Fatal("expected CategoriesExtensionKey to be set by WithApiResourceCategories")
266250
}
267251
cats, ok := ext.([]string)
268-
if !ok || len(cats) != len(tc.wantCats) || cats[len(tc.wantCats)-1] != tc.wantCats[len(tc.wantCats)-1] {
269-
t.Errorf("unexpected categories: %#v", ext)
270-
}
252+
assert.True(t, ok, "categories should be []string")
253+
assert.Equal(t, tc.wantCats, cats, "categories mismatch")
271254
})
272255
}
273256
}
@@ -301,10 +284,6 @@ func TestWithScope(t *testing.T) {
301284

302285
// Validate
303286
scope, ok := b.schemas["g.v1.K"].VendorExtensible.Extensions[common.ScopeExtensionKey]
304-
if !ok {
305-
t.Fatal("expected ScopeExtensionKey to be set")
306-
}
307-
if scope != apiextensionsv1.NamespaceScoped {
308-
t.Errorf("expected NamespaceScoped, got %v", scope)
309-
}
287+
assert.True(t, ok, "expected ScopeExtensionKey to be set")
288+
assert.Equal(t, apiextensionsv1.NamespaceScoped, scope, "scope value mismatch")
310289
}

0 commit comments

Comments
 (0)