-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator_test.go
79 lines (70 loc) · 1.77 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
package utils_test
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/dollarsignteam/go-utils"
)
type Data struct {
Balance string `validate:"required,number_string"`
}
func TestValidateNumberString(t *testing.T) {
tests := []struct {
Input string
Expected bool
}{
{Input: "123", Expected: true},
{Input: "-123", Expected: true},
{Input: "1.23", Expected: true},
{Input: "-1.23", Expected: true},
{Input: "1,000.00", Expected: true},
{Input: "-1,000.99", Expected: true},
{Input: ".123456789", Expected: true},
{Input: "-1,0,00.99", Expected: false},
{Input: "-10,00.99", Expected: false},
{Input: "1.x", Expected: false},
}
for _, test := range tests {
data := Data{Balance: test.Input}
err := utils.Validate.Struct(data)
assert.Equal(t, test.Expected, err == nil, test.Input)
}
}
func TestGetJSONTagName(t *testing.T) {
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"-"`
}
tests := []struct {
fieldName string
expected string
}{
{fieldName: "ID", expected: "id"},
{fieldName: "Name", expected: "name"},
{fieldName: "Email", expected: ""},
}
for _, test := range tests {
field, _ := reflect.TypeOf(User{}).FieldByName(test.fieldName)
result := utils.GetJSONTagName(field)
assert.Equal(t, test.expected, result)
}
}
func TestValidateStruct_Valid(t *testing.T) {
s := Data{
Balance: "123",
}
err := utils.ValidateStruct(s)
assert.NoError(t, err)
}
func TestValidateStruct_Invalid(t *testing.T) {
s := Data{}
err := utils.ValidateStruct(s)
assert.EqualError(t, err, "Validation failed for 'Balance'")
}
func BenchmarkValidateNumberString(b *testing.B) {
data := Data{Balance: "10,000.00"}
for n := 0; n < b.N; n++ {
utils.Validate.Struct(data)
}
}