-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
103 lines (94 loc) · 2.55 KB
/
utils_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
97
98
99
100
101
102
103
package gromer
import (
"regexp"
"testing"
"time"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
)
func init() {
pinCodeRegex := regexp.MustCompile("^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$")
RegisterValidation("pincode", "is not in valid format", func(fl validator.FieldLevel) bool {
return pinCodeRegex.MatchString(fl.Field().String())
})
}
func TestValidator(t *testing.T) {
assert.NoError(t, Validator.Var("560001", "pincode"))
assert.EqualError(t, Validator.Var("ABCD", "pincode"), "Key: '' Error:Field validation for '' failed on the 'pincode' tag")
}
type Todo struct {
ID string `json:"id" validate:"required"`
OrgID string `json:"orgId" validate:"required"`
Pincode string `json:"pincode" validate:"required,pincode"`
PAN string `json:"pan" validate:"required"`
CreatedAt time.Time `json:"createdAt"`
}
func TestValidate(t *testing.T) {
todo := &Todo{
ID: "123",
OrgID: "",
Pincode: "",
PAN: "",
}
err := Validate(todo)
validationErrors := err.(validator.ValidationErrors)
assert.Equal(t, GetValidationError(validationErrors), map[string]string{
"pincode": "is required",
"pan": "is required",
"orgId": "is required",
})
todo.Pincode = "AWS"
todo.OrgID = "123"
err = Validate(todo)
validationErrors = err.(validator.ValidationErrors)
assert.EqualValues(t, map[string]string{
"pincode": "is not in valid format",
"pan": "is required",
}, GetValidationError(validationErrors))
}
type Note struct {
ID string `json:"id"`
Text string `json:"text"`
CreatedAt time.Time `json:"createdAt"`
}
func TestMerge(t *testing.T) {
note := &Note{
ID: "",
Text: "",
CreatedAt: time.Time{},
}
err := Merge(note, &Note{
ID: "1",
Text: "1",
CreatedAt: time.Date(2020, 10, 10, 0, 0, 0, 0, time.UTC),
})
assert.NoError(t, err)
assert.EqualValues(t, &Note{
ID: "1",
Text: "1",
CreatedAt: time.Date(2020, 10, 10, 0, 0, 0, 0, time.UTC),
}, note)
err = Merge(note, &Note{
ID: "2",
Text: "2",
CreatedAt: time.Date(2020, 11, 11, 0, 0, 0, 0, time.UTC),
})
assert.NoError(t, err)
assert.EqualValues(t, &Note{
ID: "2",
Text: "2",
CreatedAt: time.Date(2020, 11, 11, 0, 0, 0, 0, time.UTC),
}, note)
// TODO: look at this merge
err = Merge(note, &Note{
ID: "2",
Text: "",
CreatedAt: time.Time{},
})
assert.NoError(t, err)
assert.EqualValues(t, &Note{
ID: "2",
Text: "2",
CreatedAt: time.Date(2020, 11, 11, 0, 0, 0, 0, time.UTC),
}, note)
}