Skip to content

Commit 8c20a4b

Browse files
committed
test: validation
1 parent adf634a commit 8c20a4b

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

validation/validation_test.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package validation_test
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
8+
"github.com/glocurrency/commons/validation"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
type mockFieldLevel struct {
13+
val string
14+
}
15+
16+
func (m mockFieldLevel) Field() reflect.Value {
17+
return reflect.ValueOf(m.val)
18+
}
19+
20+
// The other methods are unused by your validator, so can return zero values
21+
func (m mockFieldLevel) Top() reflect.Value { return reflect.Value{} }
22+
func (m mockFieldLevel) Parent() reflect.Value { return reflect.Value{} }
23+
func (m mockFieldLevel) FieldName() string { return "" }
24+
func (m mockFieldLevel) StructFieldName() string { return "" }
25+
func (m mockFieldLevel) Param() string { return "" }
26+
func (m mockFieldLevel) GetTag() string { return "" }
27+
func (m mockFieldLevel) ExtractType(field reflect.Value) (reflect.Value, reflect.Kind, bool) {
28+
return field, field.Kind(), false
29+
}
30+
func (m mockFieldLevel) GetStructFieldOK() (reflect.Value, reflect.Kind, bool) {
31+
return reflect.Value{}, reflect.Invalid, false
32+
}
33+
func (m mockFieldLevel) GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool) {
34+
return reflect.Value{}, reflect.Invalid, false
35+
}
36+
func (m mockFieldLevel) GetStructFieldOK2() (reflect.Value, reflect.Kind, bool, bool) {
37+
return reflect.Value{}, reflect.Invalid, false, false
38+
}
39+
func (m mockFieldLevel) GetStructFieldOKAdvanced2(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool, bool) {
40+
return reflect.Value{}, reflect.Invalid, false, false
41+
}
42+
43+
func TestValidate18YearsOld(t *testing.T) {
44+
now := time.Now()
45+
eighteenYearsAgo := now.AddDate(-18, 0, 0)
46+
seventeenYearsAgo := now.AddDate(-17, 0, 0)
47+
twentyYearsAgo := now.AddDate(-20, 0, 0)
48+
49+
tests := []struct {
50+
name string
51+
dob string
52+
valid bool
53+
}{
54+
{"Exactly 18", eighteenYearsAgo.Format("2006-01-02"), true},
55+
{"Under 18", seventeenYearsAgo.Format("2006-01-02"), false},
56+
{"Over 18", twentyYearsAgo.Format("2006-01-02"), true},
57+
{"Way Over 18", "123456-01-01", false},
58+
{"Invalid date", "invalid-date", false},
59+
}
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
field := mockFieldLevel{val: tt.dob}
64+
assert.Equal(t, tt.valid, validation.Validate18YearsOld(field))
65+
})
66+
}
67+
}
68+
69+
func TestValidate100YearsOld(t *testing.T) {
70+
now := time.Now()
71+
hundredYearsAgo := now.AddDate(-100, 0, 0)
72+
overHundred := now.AddDate(-101, 0, 0)
73+
underHundred := now.AddDate(-50, 0, 0)
74+
75+
tests := []struct {
76+
name string
77+
dob string
78+
valid bool
79+
}{
80+
{"Exactly 100", hundredYearsAgo.Format("2006-01-02"), true},
81+
{"Over 100", overHundred.Format("2006-01-02"), false},
82+
{"Under 100", underHundred.Format("2006-01-02"), true},
83+
{"Invalid date", "invalid", false},
84+
}
85+
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
field := mockFieldLevel{val: tt.dob}
89+
assert.Equal(t, tt.valid, validation.Validate100YearsOld(field))
90+
})
91+
}
92+
}
93+
94+
func TestValidateAlphaNumSpace(t *testing.T) {
95+
tests := []struct {
96+
name string
97+
input string
98+
valid bool
99+
}{
100+
{"Only letters", "HelloWorld", true},
101+
{"Letters and numbers", "Hello123", true},
102+
{"Letters, numbers, spaces", "Hello 123 World", true},
103+
{"Empty string", "", false},
104+
{"Symbols", "Hello@123", false},
105+
{"Dash not allowed", "Hello-123", false},
106+
}
107+
108+
for _, tt := range tests {
109+
t.Run(tt.name, func(t *testing.T) {
110+
fl := mockFieldLevel{val: tt.input}
111+
assert.Equal(t, tt.valid, validation.ValidateAlphaNumSpace(fl))
112+
})
113+
}
114+
}
115+
116+
func TestValidateAlphaNumSpaceDash(t *testing.T) {
117+
tests := []struct {
118+
name string
119+
input string
120+
valid bool
121+
}{
122+
{"Letters, numbers, spaces", "Hello 123", true},
123+
{"Includes dash", "Hello-123", true},
124+
{"Only dash", "-", true},
125+
{"Symbols not allowed", "Hello@123", false},
126+
{"Empty string", "", false},
127+
}
128+
129+
for _, tt := range tests {
130+
t.Run(tt.name, func(t *testing.T) {
131+
fl := mockFieldLevel{val: tt.input}
132+
assert.Equal(t, tt.valid, validation.ValidateAlphaNumSpaceDash(fl))
133+
})
134+
}
135+
}
136+
137+
func TestValidateBankSupported(t *testing.T) {
138+
tests := []struct {
139+
name string
140+
input string
141+
valid bool
142+
}{
143+
{"Letters and numbers", "Bank123", true},
144+
{"With spaces", "Bank 123", true},
145+
{"With symbols", `Bank/123-?:().,&"+`, true},
146+
{"Invalid symbol", "Bank@123", false},
147+
{"Empty string", "", false},
148+
}
149+
150+
for _, tt := range tests {
151+
t.Run(tt.name, func(t *testing.T) {
152+
fl := mockFieldLevel{val: tt.input}
153+
assert.Equal(t, tt.valid, validation.ValidateBankSupported(fl))
154+
})
155+
}
156+
}

0 commit comments

Comments
 (0)