-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_test.go
More file actions
75 lines (61 loc) · 1.87 KB
/
validate_test.go
File metadata and controls
75 lines (61 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gencheck
import (
"fmt"
"testing"
"github.com/stretchr/testify/suite"
)
type ValidateTestSuite struct {
suite.Suite
}
func TestValidateTestSuite(t *testing.T) {
suite.Run(t, new(ValidateTestSuite))
}
type CanValidate struct {
Valid bool
}
func (c CanValidate) Validate() error {
if c.Valid {
return nil
}
return ValidationErrors{
NewFieldError("CanValidate", "Valid", "", fmt.Errorf("Valid when true")),
NewFieldError("CanValidate", "Valid", "valid", nil),
}
}
type NotValidateable struct {
Whatever string
}
// TestCanValidate_Happy
func (s *ValidateTestSuite) TestCanValidate_Happy() {
uut := &CanValidate{Valid: true}
s.Require().Nil(Validate(uut))
}
// TestCanValidate_ValidationError
func (s *ValidateTestSuite) TestCanValidate_ValidationError() {
uut := CanValidate{Valid: false}
err := Validate(uut)
s.Require().NotNil(err)
if ve, ok := err.(ValidationErrors); ok {
s.Equal("validation: field validation failed for 'CanValidate.Valid': Valid when true\nvalidation: field validation failed for 'CanValidate.Valid': tag='valid'", ve.Error())
s.Require().Len(ve, 2, "Should have 2 errors in the CanValidate Error")
fe := ve[0]
s.Equal("CanValidate", fe.Struct())
s.Equal("Valid", fe.Field())
s.Equal("", fe.Tag())
s.Equal("Valid when true", fe.Message())
s.Equal("validation: field validation failed for 'CanValidate.Valid': Valid when true", fe.Error())
fe = ve[1]
s.Equal("CanValidate", fe.Struct())
s.Equal("Valid", fe.Field())
s.Equal("valid", fe.Tag())
s.Equal("", fe.Message())
s.Equal("validation: field validation failed for 'CanValidate.Valid': tag='valid'", fe.Error())
} else {
s.FailNow("Error returned should have been validation errors")
}
}
// TestNotValidateable tests to see if the string passed in is nil
func (s *ValidateTestSuite) TestNotValidateable() {
uut := &NotValidateable{}
s.Require().Nil(Validate(uut))
}