From 7062384492e34846315330fc61bdcca0e351b0ba Mon Sep 17 00:00:00 2001 From: Yann Hamon Date: Thu, 9 May 2024 16:37:20 +0200 Subject: [PATCH] Slightly improve the coverage of the validator test (#271) --- pkg/validator/validator_test.go | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/pkg/validator/validator_test.go b/pkg/validator/validator_test.go index 902621a..aa86951 100644 --- a/pkg/validator/validator_test.go +++ b/pkg/validator/validator_test.go @@ -32,7 +32,8 @@ func TestValidate(t *testing.T) { schemaRegistry2 []byte ignoreMissingSchema bool strict bool - expect Status + expectStatus Status + expectErrors []ValidationError }{ { "valid resource", @@ -67,6 +68,7 @@ lastName: bar false, false, Valid, + []ValidationError{}, }, { "invalid resource", @@ -101,6 +103,12 @@ lastName: bar false, false, Invalid, + []ValidationError{ + { + Path: "/firstName", + Msg: "expected number, but got string", + }, + }, }, { "missing required field", @@ -134,6 +142,12 @@ firstName: foo false, false, Invalid, + []ValidationError{ + { + Path: "", + Msg: "missing properties: 'lastName'", + }, + }, }, { "key \"firstName\" already set in map", @@ -160,6 +174,7 @@ firstName: bar false, true, Error, + []ValidationError{}, }, { "key firstname already set in map in non-strict mode", @@ -186,6 +201,7 @@ firstName: bar false, false, Valid, + []ValidationError{}, }, { "resource has invalid yaml", @@ -223,6 +239,7 @@ lastName: bar false, false, Error, + []ValidationError{}, }, { "missing schema in 1st registry", @@ -260,6 +277,7 @@ lastName: bar false, false, Valid, + []ValidationError{}, }, { "non-json response in 1st registry", @@ -297,6 +315,7 @@ lastName: bar false, false, Valid, + []ValidationError{}, }, { "missing schema in both registries, ignore missing", @@ -311,6 +330,7 @@ lastName: bar true, false, Skipped, + []ValidationError{}, }, { "missing schema in both registries, do not ignore missing", @@ -325,6 +345,7 @@ lastName: bar false, false, Error, + []ValidationError{}, }, { "non-json response in both registries, ignore missing", @@ -339,6 +360,7 @@ lastName: bar true, false, Skipped, + []ValidationError{}, }, { "non-json response in both registries, do not ignore missing", @@ -353,6 +375,7 @@ lastName: bar false, false, Error, + []ValidationError{}, }, } { val := v{ @@ -373,11 +396,21 @@ lastName: bar }), }, } - if got := val.ValidateResource(resource.Resource{Bytes: testCase.rawResource}); got.Status != testCase.expect { + got := val.ValidateResource(resource.Resource{Bytes: testCase.rawResource}) + if got.Status != testCase.expectStatus { if got.Err != nil { - t.Errorf("Test '%s' - expected %d, got %d: %s", testCase.name, testCase.expect, got.Status, got.Err.Error()) + t.Errorf("Test '%s' - expected %d, got %d: %s", testCase.name, testCase.expectStatus, got.Status, got.Err.Error()) } else { - t.Errorf("%d - expected %d, got %d", i, testCase.expect, got.Status) + t.Errorf("Test '%s'- %d - expected %d, got %d", testCase.name, i, testCase.expectStatus, got.Status) + } + } + + if len(got.ValidationErrors) != len(testCase.expectErrors) { + t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors) + } + for i, _ := range testCase.expectErrors { + if testCase.expectErrors[i] != got.ValidationErrors[i] { + t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors) } } }