From 2c8335ff3e357a72dfe9362a8848362818efc76e Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Fri, 6 Dec 2024 18:00:46 +0800 Subject: [PATCH 1/4] fill gaps in test conditions --- validator_test.go | 75 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/validator_test.go b/validator_test.go index 5eadb2502..a8f255832 100644 --- a/validator_test.go +++ b/validator_test.go @@ -5522,12 +5522,11 @@ func TestIsEqValidation(t *testing.T) { var errs error validate := New() - var j uint64 - var k float64 s := "abcd" i := 1 - j = 1 - k = 1.543 + j := uint64(1) + k := float64(1.543) + k32 := float32(1.543) arr := []string{"test"} now := time.Now().UTC() @@ -5543,6 +5542,9 @@ func TestIsEqValidation(t *testing.T) { errs = validate.Var(k, "eq=1.543") Equal(t, errs, nil) + errs = validate.Var(k32, "eq=1.543") + Equal(t, errs, nil) + errs = validate.Var(arr, "eq=1") Equal(t, errs, nil) @@ -5946,6 +5948,7 @@ func TestImageValidation(t *testing.T) { paths := map[string]string{ "empty": "", "directory": "testdata", + "noperm": filepath.Join("testdata", "noperm.png"), "missing": filepath.Join("testdata", "none.png"), "png": filepath.Join("testdata", "image.png"), "jpeg": filepath.Join("testdata", "image.jpg"), @@ -5979,6 +5982,17 @@ func TestImageValidation(t *testing.T) { func() {}, func() {}, }, + { + "no permission", + paths["noperm"], + false, + func() { + os.OpenFile(paths["noperm"], os.O_CREATE, 0o000) + }, + func() { + os.Remove(paths["noperm"]) + }, + }, { "valid png", paths["png"], @@ -6053,7 +6067,8 @@ func TestFilePathValidation(t *testing.T) { {"empty filepath", "", false}, {"valid filepath", filepath.Join("testdata", "a.go"), true}, {"invalid filepath", filepath.Join("testdata", "no\000.go"), false}, - {"directory, not a filepath", "testdata" + string(os.PathSeparator), false}, + {"existing directory, not a filepath", "testdata" + string(os.PathSeparator), false}, + {"non-existing directory, not a filepath", "missing" + string(os.PathSeparator), false}, {"directory", "testdata", false}, } @@ -11001,14 +11016,14 @@ func TestDirPathValidation(t *testing.T) { }, "Bad field type int") } -func TestStartsWithValidation(t *testing.T) { +func TestStartsNotWithValidation(t *testing.T) { tests := []struct { - Value string `validate:"startswith=(/^ヮ^)/*:・゚✧"` + Value string `validate:"startsnotwith=(/^ヮ^)/*:・゚✧"` Tag string ExpectedNil bool }{ - {Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "startswith=(/^ヮ^)/*:・゚✧", ExpectedNil: true}, - {Value: "abcd", Tag: "startswith=(/^ヮ^)/*:・゚✧", ExpectedNil: false}, + {Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "startsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: false}, + {Value: "abcd", Tag: "startsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: true}, } validate := New() @@ -11028,14 +11043,14 @@ func TestStartsWithValidation(t *testing.T) { } } -func TestEndsWithValidation(t *testing.T) { +func TestEndsNotWithValidation(t *testing.T) { tests := []struct { - Value string `validate:"endswith=(/^ヮ^)/*:・゚✧"` + Value string `validate:"endsnotwith=(/^ヮ^)/*:・゚✧"` Tag string ExpectedNil bool }{ - {Value: "glitter (/^ヮ^)/*:・゚✧", Tag: "endswith=(/^ヮ^)/*:・゚✧", ExpectedNil: true}, - {Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "endswith=(/^ヮ^)/*:・゚✧", ExpectedNil: false}, + {Value: "glitter (/^ヮ^)/*:・゚✧", Tag: "endsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: false}, + {Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "endsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: true}, } validate := New() @@ -12080,7 +12095,7 @@ func TestExcludedIf(t *testing.T) { test11 := struct { Field1 bool - Field2 *string `validate:"excluded_if=Field1 false"` + Field2 *string `validate:"excluded_if=Field1 false"` }{ Field1: false, Field2: nil, @@ -12754,8 +12769,10 @@ func TestIsIso3166AlphaNumericValidation(t *testing.T) { expected bool }{ {248, true}, + {uint(248), true}, {"248", true}, {0, false}, + {uint(0), false}, {1, false}, {"1", false}, {"invalid_int", false}, @@ -12788,9 +12805,11 @@ func TestIsIso3166AlphaNumericEUValidation(t *testing.T) { value interface{} expected bool }{ - {752, true}, //Sweden + {752, true}, // Sweden + {uint(752), true}, // Sweden {"752", true}, - {826, false}, // UK + {826, false}, // UK + {uint(826), false}, // UK {"826", false}, } @@ -12847,6 +12866,10 @@ func TestCountryCodeValidation(t *testing.T) { } } } + + PanicMatches(t, func() { + _ = validate.Var(1.1, "country_code") + }, "Bad field type float64") } func TestEUCountryCodeValidation(t *testing.T) { @@ -12880,6 +12903,10 @@ func TestEUCountryCodeValidation(t *testing.T) { } } } + + PanicMatches(t, func() { + _ = validate.Var(1.1, "eu_country_code") + }, "Bad field type float64") } func TestIsIso4217Validation(t *testing.T) { @@ -12912,12 +12939,14 @@ func TestIsIso4217Validation(t *testing.T) { func TestIsIso4217NumericValidation(t *testing.T) { tests := []struct { - value int `validate:"iso4217_numeric"` + value any `validate:"iso4217_numeric"` expected bool }{ {8, true}, {12, true}, + {uint(12), true}, {13, false}, + {uint(13), false}, } validate := New() @@ -12936,6 +12965,10 @@ func TestIsIso4217NumericValidation(t *testing.T) { } } } + + PanicMatches(t, func() { + _ = validate.Var("12", "iso4217_numeric") + }, "Bad field type string") } func TestTimeZoneValidation(t *testing.T) { @@ -13632,6 +13665,10 @@ func TestSpiceDBValueFormatValidation(t *testing.T) { } } } + + PanicMatches(t, func() { + _ = validate.Var(`a_B/a_b`, "spicedb=invalid") + }, "Unrecognized parameter: invalid") } func TestCreditCardFormatValidation(t *testing.T) { @@ -13717,6 +13754,10 @@ func TestLuhnChecksumValidation(t *testing.T) { } } } + + PanicMatches(t, func() { + _ = validate.Var(1.1, "luhn_checksum") + }, "Bad field type: float64") } func TestMultiOrOperatorGroup(t *testing.T) { From 87fc83d7c11a42eb4e7ab637018f865f671c6fd2 Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:21:18 +0800 Subject: [PATCH 2/4] fix issues found by golangci-lint, ignore error --- validator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator_test.go b/validator_test.go index a8f255832..77864c3ff 100644 --- a/validator_test.go +++ b/validator_test.go @@ -5987,7 +5987,7 @@ func TestImageValidation(t *testing.T) { paths["noperm"], false, func() { - os.OpenFile(paths["noperm"], os.O_CREATE, 0o000) + _, _ = os.OpenFile(paths["noperm"], os.O_CREATE, 0o000) }, func() { os.Remove(paths["noperm"]) From 2f914b0e8bf37e5a432f863068f55c1c985085ee Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:21:35 +0800 Subject: [PATCH 3/4] can't use any in 1.17 --- validator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator_test.go b/validator_test.go index 77864c3ff..d7ba0ca85 100644 --- a/validator_test.go +++ b/validator_test.go @@ -12939,7 +12939,7 @@ func TestIsIso4217Validation(t *testing.T) { func TestIsIso4217NumericValidation(t *testing.T) { tests := []struct { - value any `validate:"iso4217_numeric"` + value interface{} `validate:"iso4217_numeric"` expected bool }{ {8, true}, From 828515fc5165bbc31b128260aec0cf3225e3e2ae Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:28:34 +0800 Subject: [PATCH 4/4] fix test --- validator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator_test.go b/validator_test.go index d7ba0ca85..8ea0709dc 100644 --- a/validator_test.go +++ b/validator_test.go @@ -13757,7 +13757,7 @@ func TestLuhnChecksumValidation(t *testing.T) { PanicMatches(t, func() { _ = validate.Var(1.1, "luhn_checksum") - }, "Bad field type: float64") + }, "Bad field type float64") } func TestMultiOrOperatorGroup(t *testing.T) {