-
Notifications
You must be signed in to change notification settings - Fork 7
/
validator_test.go
96 lines (74 loc) · 1.97 KB
/
validator_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
93
94
95
96
package gpc
import (
"sync"
"testing"
)
type Login struct {
Email string `validate:"required,email"`
Password string `validate:"required,gt=7"`
}
type LoginSingleGpc struct {
Email string `validate:"required,email" gpc:"required=email tidak boleh kosong"`
Password string `validate:"required,gt=7" gpc:"required=password tidak boleh kosong"`
}
type LoginMultiGpc struct {
Email string `validate:"required,email" gpc:"required=email tidak boleh kosong,email=email format tidak valid"`
Password string `validate:"required,gt=7" gpc:"required=password tidak boleh kosong,gt=password harus lebih besar dari 7"`
}
func TestValidator(action *testing.T) {
action.Run("Should be TestValidator - with error", func(t *testing.T) {
var (
payload Login = Login{Email: "johndoe@#gmail.com", Password: "qwerty12"}
res, err = Validator(payload)
)
if err != nil {
t.FailNow()
}
if res == nil {
t.FailNow()
}
})
action.Run("Should be TestValidator - without error", func(t *testing.T) {
var (
payload Login = Login{Email: "[email protected]", Password: "qwerty12"}
res, err = Validator(payload)
)
if err != nil {
t.FailNow()
}
if res != nil {
t.FailNow()
}
})
action.Run("Should be TestValidator - with error use gorutine", func(t *testing.T) {
var (
wg *sync.WaitGroup = new(sync.WaitGroup)
errorsChan chan *FormatError = make(chan *FormatError)
)
wg.Add(1)
go func() {
wg.Done()
res, err := Validator(Login{Email: "johndoe@#gmail.com", Password: "qwerty12"})
if err != nil {
t.FailNow()
}
errorsChan <- res
}()
wg.Wait()
errors := <-errorsChan
if len(errors.Errors) < 1 {
t.FailNow()
}
})
action.Run("Should be TestValidator - large validation", func(t *testing.T) {
for i := 0; i < 100000; i++ {
res, err := Validator(Login{Email: "johndoe@#gmail.com", Password: "qwerty12"})
if err != nil {
t.FailNow()
}
if res == nil {
t.FailNow()
}
}
})
}