-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation_test.go
92 lines (86 loc) · 1.47 KB
/
validation_test.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package utilities
import "testing"
func TestValidateRequiredFields(t *testing.T) {
type tp struct {
Name string `validate:"nonzero"`
Age int `validate:"nonzero"`
}
tc := []struct {
name string
input tp
expectErr bool
}{
{
name: "valid",
input: tp{
Name: "test",
Age: 10,
},
expectErr: false,
},
{
name: "invalid",
input: tp{
Age: 10,
},
expectErr: true,
},
{
name: "invalid",
input: tp{
Name: "test",
},
expectErr: true,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
err := ValidateRequiredFields(c.input)
if err != nil && !c.expectErr {
t.Errorf("unexpected error: %v", err)
}
if err == nil && c.expectErr {
t.Errorf("expected error but got nil")
}
})
}
}
func TestValidateEmail(t *testing.T) {
tc := []struct {
name string
input string
expectErr bool
}{
{
name: "valid",
input: "ab@mail.com",
expectErr: false,
},
{
name: "invalid",
input: "abmail.com",
expectErr: true,
},
{
name: "invalid",
input: "ab@mail",
expectErr: true,
},
{
name: "invalid",
input: "abmail",
expectErr: true,
},
}
for _, c := range tc {
t.Run(c.name, func(t *testing.T) {
err := ValidateEmail(c.input)
if err != nil && !c.expectErr {
t.Errorf("unexpected error: %v", err)
}
if err == nil && c.expectErr {
t.Errorf("expected error but got nil")
}
})
}
}