Skip to content

Commit 2f9d907

Browse files
committed
Add unit test for rules.go
1 parent ef8b6eb commit 2f9d907

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

pkg/validation/rules_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package validation
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestValidateRequired(t *testing.T) {
8+
tests := []struct {
9+
value string
10+
errorMsg string
11+
wantErr bool
12+
}{
13+
{"", "This field is required", true}, // Should return error
14+
{"Hello", "This field is required", false}, // Should not return error
15+
}
16+
17+
for _, tt := range tests {
18+
t.Run(tt.value, func(t *testing.T) {
19+
err := validateRequired(tt.value, tt.errorMsg)
20+
if (err != nil) != tt.wantErr {
21+
t.Errorf("validateRequired() error = %v, wantErr %v", err, tt.wantErr)
22+
}
23+
})
24+
}
25+
}
26+
27+
func TestValidationIsUUID(t *testing.T) {
28+
tests := []struct {
29+
value string
30+
errorMsg string
31+
wantErr bool
32+
}{
33+
{"invalid-uuid", "Invalid UUID", true}, // Should return error
34+
{"f47ac10b-58cc-4372-a567-0e02b2c3d479", "Invalid UUID", false}, // Should not return error
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.value, func(t *testing.T) {
39+
err := validationIsUUID(tt.value, tt.errorMsg)
40+
if (err != nil) != tt.wantErr {
41+
t.Errorf("validationIsUUID() error = %v, wantErr %v", err, tt.wantErr)
42+
}
43+
})
44+
}
45+
}
46+
47+
func TestValidateMinLength(t *testing.T) {
48+
tests := []struct {
49+
value string
50+
min int
51+
errorMsg string
52+
wantErr bool
53+
}{
54+
{"short", 10, "Must be at least 10 characters", true}, // Should return error
55+
{"longenough", 5, "Must be at least 10 characters", false}, // Should not return error
56+
}
57+
58+
for _, tt := range tests {
59+
t.Run(tt.value, func(t *testing.T) {
60+
err := validateMinLength(tt.value, tt.min, tt.errorMsg)
61+
if (err != nil) != tt.wantErr {
62+
t.Errorf("validateMinLength() error = %v, wantErr %v", err, tt.wantErr)
63+
}
64+
})
65+
}
66+
}
67+
68+
func TestValidateIsEmail(t *testing.T) {
69+
tests := []struct {
70+
value string
71+
errorMsg string
72+
wantErr bool
73+
}{
74+
{"invalid-email", "Invalid email", true}, // Should return error
75+
{"[email protected]", "Invalid email", false}, // Should not return error
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.value, func(t *testing.T) {
80+
err := validateIsEmail(tt.value, tt.errorMsg)
81+
if (err != nil) != tt.wantErr {
82+
t.Errorf("validateIsEmail() error = %v, wantErr %v", err, tt.wantErr)
83+
}
84+
})
85+
}
86+
}
87+
88+
func TestValidateIsNumber(t *testing.T) {
89+
tests := []struct {
90+
value string
91+
errorMsg string
92+
wantErr bool
93+
}{
94+
{"not-a-number", "Must be a number", true}, // Should return error
95+
{"12345", "Must be a number", false}, // Should not return error
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.value, func(t *testing.T) {
100+
err := validateIsNumber(tt.value, tt.errorMsg)
101+
if (err != nil) != tt.wantErr {
102+
t.Errorf("validateIsNumber() error = %v, wantErr %v", err, tt.wantErr)
103+
}
104+
})
105+
}
106+
}
107+
108+
// func TestValidationBetween(t *testing.T) {
109+
// tests := []struct {
110+
// value string
111+
// min int
112+
// max int
113+
// errorMsg string
114+
// wantErr bool
115+
// }{
116+
// {"short", 5, 10, "Length must be between 5 and 10", true}, // Should return error
117+
// {"perfect", 5, 10, "Length must be between 5 and 10", false}, // Should not return error
118+
// }
119+
120+
// for _, tt := range tests {
121+
// t.Run(tt.value, func(t *testing.T) {
122+
// err := validationBetween(tt.value, tt.min, tt.max, tt.errorMsg)
123+
// if (err != nil) != tt.wantErr {
124+
// t.Errorf("validationBetween() error = %v, wantErr %v", err, tt.wantErr)
125+
// }
126+
// })
127+
// }
128+
// }
129+
130+
func TestValidationMinMaxNumber(t *testing.T) {
131+
tests := []struct {
132+
value string
133+
min int
134+
max int
135+
errorMsg string
136+
wantErr bool
137+
}{
138+
{"50", 10, 100, "Number must be between 10 and 100", false}, // Should not return error
139+
{"200", 10, 100, "Number must be between 10 and 100", true}, // Should return error
140+
}
141+
142+
for _, tt := range tests {
143+
t.Run(tt.value, func(t *testing.T) {
144+
err := validationMinMaxNumber(tt.value, tt.min, tt.max, tt.errorMsg)
145+
if (err != nil) != tt.wantErr {
146+
t.Errorf("validationMinMaxNumber() error = %v, wantErr %v", err, tt.wantErr)
147+
}
148+
})
149+
}
150+
}

0 commit comments

Comments
 (0)