From 16b9a1c957c351347e4a27ef9a5376cb0af3fa6f Mon Sep 17 00:00:00 2001 From: aacebo Date: Mon, 7 Oct 2024 14:27:57 -0400 Subject: [PATCH] add missing test cov --- float_test.go | 50 ++++++++++++++++++++++++++++++++--- int_test.go | 24 ++++++++++++++--- string_test.go | 72 +++++++++++++++++++++++++++++++++++++++++--------- time_test.go | 58 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 180 insertions(+), 24 deletions(-) diff --git a/float_test.go b/float_test.go index 73f3619..6369c4d 100644 --- a/float_test.go +++ b/float_test.go @@ -44,8 +44,42 @@ func Test_Float(t *testing.T) { }) }) + t.Run("type", func(t *testing.T) { + t.Run("should succeed when float", func(t *testing.T) { + err := owl.Float().Validate(1.5) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when int", func(t *testing.T) { + err := owl.Float().Validate(1) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should fail when not int/float", func(t *testing.T) { + err := owl.Float().Validate(true) + + if err == nil { + t.FailNow() + } + }) + }) + t.Run("min", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.Float().Min(5).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when gt min", func(t *testing.T) { err := owl.Float().Min(5).Validate(6) if err != nil { @@ -53,7 +87,7 @@ func Test_Float(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when lt min", func(t *testing.T) { err := owl.Float().Min(5).Validate(4) if err == nil { @@ -63,7 +97,15 @@ func Test_Float(t *testing.T) { }) t.Run("max", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.Float().Max(5).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when lt max", func(t *testing.T) { err := owl.Float().Max(5).Validate(4) if err != nil { @@ -71,7 +113,7 @@ func Test_Float(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when gt max", func(t *testing.T) { err := owl.Float().Max(5).Validate(6) if err == nil { diff --git a/int_test.go b/int_test.go index 808aa4d..8222abc 100644 --- a/int_test.go +++ b/int_test.go @@ -45,7 +45,15 @@ func Test_Int(t *testing.T) { }) t.Run("min", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.Int().Min(5).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when gt min", func(t *testing.T) { err := owl.Int().Min(5).Validate(6) if err != nil { @@ -53,7 +61,7 @@ func Test_Int(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when lt min", func(t *testing.T) { err := owl.Int().Min(5).Validate(4) if err == nil { @@ -63,7 +71,15 @@ func Test_Int(t *testing.T) { }) t.Run("max", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.Int().Max(5).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when lt max", func(t *testing.T) { err := owl.Int().Max(5).Validate(4) if err != nil { @@ -71,7 +87,7 @@ func Test_Int(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when gt max", func(t *testing.T) { err := owl.Int().Max(5).Validate(6) if err == nil { diff --git a/string_test.go b/string_test.go index 5053d58..b71a6b5 100644 --- a/string_test.go +++ b/string_test.go @@ -46,7 +46,15 @@ func Test_String(t *testing.T) { }) t.Run("min", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.String().Min(5).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when length gt min", func(t *testing.T) { err := owl.String().Min(5).Validate("tester") if err != nil { @@ -54,7 +62,7 @@ func Test_String(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when length lt min", func(t *testing.T) { err := owl.String().Min(5).Validate("test") if err == nil { @@ -64,7 +72,15 @@ func Test_String(t *testing.T) { }) t.Run("max", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.String().Max(5).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when length lt max", func(t *testing.T) { err := owl.String().Max(5).Validate("test") if err != nil { @@ -72,7 +88,7 @@ func Test_String(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when length gt max", func(t *testing.T) { err := owl.String().Max(5).Validate("tester") if err == nil { @@ -82,7 +98,15 @@ func Test_String(t *testing.T) { }) t.Run("regex", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.String().Regex(regexp.MustCompile("^[0-9a-zA-Z_-]+$")).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when matches", func(t *testing.T) { err := owl.String().Regex(regexp.MustCompile("^[0-9a-zA-Z_-]+$")).Validate("test") if err != nil { @@ -90,7 +114,7 @@ func Test_String(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when not matches", func(t *testing.T) { err := owl.String().Regex(regexp.MustCompile("^[0-9a-zA-Z_-]+$")).Validate("a test") if err == nil { @@ -100,7 +124,15 @@ func Test_String(t *testing.T) { }) t.Run("email", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.String().Email().Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when email", func(t *testing.T) { err := owl.String().Email().Validate("test@gmail.com") if err != nil { @@ -108,7 +140,7 @@ func Test_String(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when not email", func(t *testing.T) { err := owl.String().Email().Validate("test") if err == nil { @@ -118,7 +150,15 @@ func Test_String(t *testing.T) { }) t.Run("uuid", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.String().UUID().Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when uuid", func(t *testing.T) { err := owl.String().UUID().Validate("afefc1ab-b8f2-4803-8e9a-60515854141a") if err != nil { @@ -126,7 +166,7 @@ func Test_String(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when not uuid", func(t *testing.T) { err := owl.String().UUID().Validate("test") if err == nil { @@ -136,7 +176,15 @@ func Test_String(t *testing.T) { }) t.Run("url", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.String().URL().Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when url", func(t *testing.T) { err := owl.String().URL().Validate("https://www.google.com") if err != nil { @@ -144,7 +192,7 @@ func Test_String(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when not url", func(t *testing.T) { err := owl.String().URL().Validate("test") if err == nil { diff --git a/time_test.go b/time_test.go index 5ade9ae..2be48a5 100644 --- a/time_test.go +++ b/time_test.go @@ -27,8 +27,50 @@ func Test_Time(t *testing.T) { }) }) + t.Run("type", func(t *testing.T) { + t.Run("should succeed when `time.Time`", func(t *testing.T) { + err := owl.Time().Validate(time.Now()) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when time.Time string", func(t *testing.T) { + err := owl.Time().Validate(time.Now().Format(time.RFC3339)) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should fail when invalid string", func(t *testing.T) { + err := owl.Time().Validate(time.Now().String()) + + if err == nil { + t.FailNow() + } + }) + + t.Run("should fail when not string/time.Time", func(t *testing.T) { + err := owl.Time().Validate(true) + + if err == nil { + t.FailNow() + } + }) + }) + t.Run("min", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.Time().Min(time.Now().AddDate(-1, 0, 0)).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when gt min", func(t *testing.T) { err := owl.Time().Min(time.Now().AddDate(-1, 0, 0)).Validate(time.Now().Format(time.RFC3339)) if err != nil { @@ -36,7 +78,7 @@ func Test_Time(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when lt min", func(t *testing.T) { err := owl.Time().Min(time.Now()).Validate(time.Now().AddDate(-1, 0, 0).Format(time.RFC3339)) if err == nil { @@ -46,7 +88,15 @@ func Test_Time(t *testing.T) { }) t.Run("max", func(t *testing.T) { - t.Run("should succeed", func(t *testing.T) { + t.Run("should succeed when nil", func(t *testing.T) { + err := owl.Time().Max(time.Now().AddDate(1, 0, 0)).Validate(nil) + + if err != nil { + t.Fatal(err.Error()) + } + }) + + t.Run("should succeed when lt max", func(t *testing.T) { err := owl.Time().Max(time.Now().AddDate(1, 0, 0)).Validate(time.Now()) if err != nil { @@ -54,7 +104,7 @@ func Test_Time(t *testing.T) { } }) - t.Run("should fail", func(t *testing.T) { + t.Run("should fail when gt max", func(t *testing.T) { err := owl.Time().Max(time.Now()).Validate(time.Now().AddDate(1, 0, 0)) if err == nil {