|
6 | 6 | "testing"
|
7 | 7 |
|
8 | 8 | "github.com/openmfp/kubernetes-graphql-gateway/common"
|
| 9 | + "github.com/stretchr/testify/assert" |
9 | 10 | apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
10 | 11 | "k8s.io/apimachinery/pkg/api/meta"
|
11 | 12 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
@@ -47,9 +48,7 @@ func TestGetOpenAPISchemaKey(t *testing.T) {
|
47 | 48 |
|
48 | 49 | for _, tc := range tests {
|
49 | 50 | 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) |
53 | 52 | }
|
54 | 53 | }
|
55 | 54 |
|
@@ -90,18 +89,11 @@ func TestGetCRDGroupVersionKind(t *testing.T) {
|
90 | 89 | for _, tc := range tests {
|
91 | 90 | t.Run(tc.name, func(t *testing.T) {
|
92 | 91 | got, err := getCRDGroupVersionKind(tc.spec)
|
| 92 | + assert.Equal(t, tc.wantErr, err, "error value mismatch") |
93 | 93 | if tc.wantErr != nil {
|
94 |
| - if err != tc.wantErr { |
95 |
| - t.Fatalf("expected error %v, got %v", tc.wantErr, err) |
96 |
| - } |
97 | 94 | return
|
98 | 95 | }
|
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") |
105 | 97 | })
|
106 | 98 | }
|
107 | 99 | }
|
@@ -137,21 +129,14 @@ func TestNewSchemaBuilder(t *testing.T) {
|
137 | 129 | t.Run(tc.name, func(t *testing.T) {
|
138 | 130 | b := NewSchemaBuilder(tc.client, []string{"X/v1"})
|
139 | 131 | 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") |
146 | 134 | return
|
147 | 135 | }
|
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") |
151 | 137 | 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) |
155 | 140 | }
|
156 | 141 | })
|
157 | 142 | }
|
@@ -213,9 +198,8 @@ func TestWithCRDCategories(t *testing.T) {
|
213 | 198 | t.Fatal("expected CategoriesExtensionKey to be set")
|
214 | 199 | }
|
215 | 200 | 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") |
219 | 203 | })
|
220 | 204 | }
|
221 | 205 | }
|
@@ -265,9 +249,8 @@ func TestWithApiResourceCategories(t *testing.T) {
|
265 | 249 | t.Fatal("expected CategoriesExtensionKey to be set by WithApiResourceCategories")
|
266 | 250 | }
|
267 | 251 | 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") |
271 | 254 | })
|
272 | 255 | }
|
273 | 256 | }
|
@@ -301,10 +284,6 @@ func TestWithScope(t *testing.T) {
|
301 | 284 |
|
302 | 285 | // Validate
|
303 | 286 | 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") |
310 | 289 | }
|
0 commit comments