8000 chore: add tests for repo by shivamsouravjha · Pull Request #21 · guiferpa/gody · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: add tests for repo #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/custom_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"testing"
"log"
)


// Test generated using Keploy
func TestErrInvalidPalindromeError(t *testing.T) {
err := &ErrInvalidPalindrome{Value: "not-a-palindrome"}
expected := "invalid palindrome: not-a-palindrome"
if err.Error() != expected {
t.Errorf("Expected %v, got %v", expected, err.Error())
}
}

// Test generated using Keploy
func TestPalindromeRuleName(t *testing.T) {
rule := &PalindromeRule{}
expected := "palindrome"
if rule.Name() != expected {
t.Errorf("Expected %v, got %v", expected, rule.Name())
}
}


// Test generated using Keploy
func TestCustomRuleValidation(t *testing.T) {
// Mocking log output
var logOutput string
log.SetOutput(&mockWriter{func(p []byte) (n int, err error) {
logOutput += string(p)
return len(p), nil
}})

CustomRuleValidation()

if logOutput == "" {
t.Error("Expected log output, got none")
}
}

type mockWriter struct {
writeFunc func(p []byte) (n int, err error)
}

func (m *mockWriter) Write(p []byte) (n int, err error) {
return m.writeFunc(p)
}

65 changes: 65 additions & 0 deletions examples/http_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"testing"
)


// Test generated using Keploy
func TestErrIsAdultErrorMessage(t *testing.T) {
err := &ErrIsAdult{}
expected := "The client isn't a adult then it isn't allowed buy"
if err.Error() != expected {
t.Errorf("Expected error message '%s', got '%s'", expected, err.Error())
}
}

// Test generated using Keploy
func TestIsAdultRuleName(t *testing.T) {
rule := &IsAdultRule{}
expected := "is_adult"
if rule.Name() != expected {
t.Errorf("Expected rule name '%s', got '%s'", expected, rule.Name())
}
}


// Test generated using Keploy
func TestIsAdultRuleValidateEmptyValue(t *testing.T) {
rule := &IsAdultRule{adultAge: 21}
valid, err := rule.Validate("", "", "")
if !valid || err == nil {
t.Errorf("Expected validation to fail with ErrIsAdult, got valid=%v, err=%v", valid, err)
}
}


// Test generated using Keploy
func TestIsAdultRuleValidateNonIntegerValue(t *testing.T) {
rule := &IsAdultRule{adultAge: 21}
valid, err := rule.Validate("", "notanumber", "")
if valid || err == nil {
t.Errorf("Expected validation to fail with an error, got valid=%v, err=%v", valid, err)
}
}


// Test generated using Keploy
func TestIsAdultRuleValidateUnderage(t *testing.T) {
rule := &IsAdultRule{adultAge: 21}
valid, err := rule.Validate("", "18", "")
if !valid || err == nil {
t.Errorf("Expected validation to fail with ErrIsAdult, got valid=%v, err=%v", valid, err)
}
}


// Test generated using Keploy
func TestIsAdultRuleValidateAdult(t *testing.T) {
rule := &IsAdultRule{adultAge: 21}
valid, err := rule.Validate("", "21", "")
if !valid || err != nil {
t.Errorf("Expected validation to pass, got valid=%v, err=%v", valid, err)
}
}

134 changes: 70 additions & 64 deletions examples/simple.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,81 @@
package main

import (
"log"
"log"

gody "github.com/guiferpa/gody/v2"
"github.com/guiferpa/gody/v2/rule"
gody "github.com/guiferpa/gody/v2"
"github.com/guiferpa/gody/v2/rule"
)

func SimpleDefaultValidation() {
b := struct {
Text string `json:"text" validate:"not_empty"`
}{}

valid, err := gody.DefaultValidate(b, nil)
if err != nil {
if !valid {
log.Println("body do not validated:", err)
}

switch err.(type) {
case *rule.ErrNotEmpty:
log.Println("not empty error:", err)

}
}
func SimpleDefaultValidation(text string) error {
b := struct {
Text string `json:"text" validate:"not_empty"`
}{
Text: text,
}

valid, err := gody.DefaultValidate(b, nil)
if err != nil {
if !valid {
log.Println("body do not validated:", err)
}

switch err.(type) {
8000 case *rule.ErrNotEmpty:
log.Println("not empty error:", err)
}
}
return err
}

func SimplePureValidation() {
b := struct {
Text string `json:"text" validate:"not_empty"`
}{}

rules := []gody.Rule{
rule.NotEmpty,
}
valid, err := gody.Validate(b, rules)
if err != nil {
if !valid {
log.Println("body do not validated:", err)
}

switch err.(type) {
case *rule.ErrNotEmpty:
log.Println("not empty error:", err)

}
}
func SimplePureValidation(text string) error {
b := struct {
Text string `json:"text" validate:"not_empty"`
}{
Text: text,
}

rules := []gody.Rule{
rule.NotEmpty,
}
valid, err := gody.Validate(b, rules)
if err != nil {
if !valid {
log.Println("body do not validated:", err)
}

switch err.(type) {
case *rule.ErrNotEmpty:
log.Println("not empty error:", err)
}
}
return err
}

func SimpleValidationFromValidator() {
b := struct {
Text string `json:"text" validate:"not_empty"`
}{}

validator := gody.NewValidator()

if err := validator.AddRules(rule.NotEmpty); err != nil {
log.Println(err)
return
}

valid, err := validator.Validate(b)
if err != nil {
if !valid {
log.Println("body do not validated:", err)
}

switch err.(type) {
case *rule.ErrNotEmpty:
log.Println("not empty error:", err)

}
}
func SimpleValidationFromValidator(text string) error {
b := struct {
Text string `json:"text" validate:"not_empty"`
}{
Text: text,
}

validator := gody.NewValidator()

if err := validator.AddRules(rule.NotEmpty); err != nil {
log.Println(err)
return err
}

valid, err := validator.Validate(b)
if err != nil {
if !valid {
log.Println("body do not validated:", err)
}

switch err.(type) {
case *rule.ErrNotEmpty:
log.Println("not empty error:", err)
}
}
return err
}
42 changes: 42 additions & 0 deletions examples/simple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"testing"
"github.com/guiferpa/gody/v2/rule"
)


// Test generated using Keploy
func TestSimpleDefaultValidation_EmptyText(t *testing.T) {
err := SimpleDefaultValidation("")
if err == nil {
t.Errorf("Expected error for empty Text, but got nil")
}
if _, ok := err.(*rule.ErrNotEmpty); !ok {
t.Errorf("Expected ErrNotEmpty, but got %v", err)
}
}

// Test generated using Keploy
func TestSimplePureValidation_EmptyText(t *testing.T) {
err := SimplePureValidation("")
if err == nil {
t.Errorf("Expected error for empty Text, but got nil")
}
if _, ok := err.(*rule.ErrNotEmpty); !ok {
t.Errorf("Expected ErrNotEmpty, but got %v", err)
}
}


// Test generated using Keploy
func TestSimpleValidationFromValidator_EmptyText(t *testing.T) {
err := SimpleValidationFromValidator("")
if err == nil {
t.Errorf("Expected error for empty Text, but got nil")
}
if _, ok := err.(*rule.ErrNotEmpty); !ok {
t.Errorf("Expected ErrNotEmpty, but got %v", err)
}
}

36 changes: 33 additions & 3 deletions validate_test.go
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messages of assert conditions are too much generics. It's not valuable when we're developing and need to run the tests.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gody

import (
"reflect"
"testing"
"reflect"
"testing"

"github.com/guiferpa/gody/v2/rule/ruletest"
"github.com/guiferpa/gody/v2/rule/ruletest"
)

type StructAForTest struct {
Expand Down Expand Up @@ -108,3 +108,33 @@ func TestSetTagName(t *testing.T) {
return
}
}

// Test generated using Keploy
func TestDefaultValidate_CallsRawDefaultValidate(t *testing.T) {
payload := StructAForTest{A: "test-a", B: "test-b"}
customRules := []Rule{ruletest.NewRule("fake", true, nil)}

validated, err := DefaultValidate(payload, customRules)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !validated {
t.Error("Expected validation to pass, but it failed")
}
}


// Test generated using Keploy
func TestRawValidate_RawSerializeError(t *testing.T) {
payload := make(chan int) // Invalid type to trigger RawSerialize error
rules := []Rule{ruletest.NewRule("fake", true, nil)}

validated, err := RawValidate(payload, "validate", rules)
if validated {
t.Error("Expected validation to fail, but it passed")
}
if err == nil {
t.Error("Expected an error, but got nil")
}
}

0