107 lines
2.1 KiB
Go
107 lines
2.1 KiB
Go
|
package svc
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
|
||
|
vi "code.30cm.net/digimon/library-go/validator"
|
||
|
)
|
||
|
|
||
|
func TestWithDecimalGte(t *testing.T) {
|
||
|
validate := vi.MustValidator(WithDecimalGt(), WithDecimalGte())
|
||
|
|
||
|
type TestStruct struct {
|
||
|
Value string `validate:"decimalGte=10.50"`
|
||
|
}
|
||
|
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
input TestStruct
|
||
|
wantErr bool
|
||
|
}{
|
||
|
{
|
||
|
name: "valid - equal",
|
||
|
input: TestStruct{Value: "10.50"},
|
||
|
wantErr: false,
|
||
|
},
|
||
|
{
|
||
|
name: "valid - greater",
|
||
|
input: TestStruct{Value: "15.00"},
|
||
|
wantErr: false,
|
||
|
},
|
||
|
{
|
||
|
name: "invalid - less",
|
||
|
input: TestStruct{Value: "9.99"},
|
||
|
wantErr: true,
|
||
|
},
|
||
|
{
|
||
|
name: "invalid - not a decimal",
|
||
|
input: TestStruct{Value: "abc"},
|
||
|
wantErr: true,
|
||
|
},
|
||
|
{
|
||
|
name: "valid - empty string",
|
||
|
input: TestStruct{Value: ""},
|
||
|
wantErr: true, // Assuming empty string is valid
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
err := validate.ValidateAll(tt.input)
|
||
|
|
||
|
if (err != nil) != tt.wantErr {
|
||
|
t.Errorf("TestWithDecimalGte() %s = error %v, wantErr %v", tt.name, err, tt.wantErr)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestWithDecimalGt(t *testing.T) {
|
||
|
validate := vi.MustValidator(WithDecimalGt(), WithDecimalGt())
|
||
|
|
||
|
type TestStruct struct {
|
||
|
Value string `validate:"decimalGt=10.50"`
|
||
|
}
|
||
|
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
input TestStruct
|
||
|
wantErr bool
|
||
|
}{
|
||
|
{
|
||
|
name: "valid - greater",
|
||
|
input: TestStruct{Value: "10.51"},
|
||
|
wantErr: false,
|
||
|
},
|
||
|
{
|
||
|
name: "invalid - equal",
|
||
|
input: TestStruct{Value: "10.50"},
|
||
|
wantErr: true, // should fail because the value is equal to the condition
|
||
|
},
|
||
|
{
|
||
|
name: "invalid - less",
|
||
|
input: TestStruct{Value: "9.99"},
|
||
|
wantErr: true,
|
||
|
},
|
||
|
{
|
||
|
name: "invalid - not a decimal",
|
||
|
input: TestStruct{Value: "abc"},
|
||
|
wantErr: true,
|
||
|
},
|
||
|
{
|
||
|
name: "valid - empty string",
|
||
|
input: TestStruct{Value: ""},
|
||
|
wantErr: true,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
err := validate.ValidateAll(tt.input)
|
||
|
if (err != nil) != tt.wantErr {
|
||
|
t.Errorf("TestWithDecimalGt() %s = error %v, wantErr %v", tt.name, err, tt.wantErr)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|