1
|
|
|
package test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation" |
7
|
|
|
"github.com/muonsoft/validation/code" |
8
|
|
|
"github.com/muonsoft/validation/it" |
9
|
|
|
"github.com/muonsoft/validation/validationtest" |
10
|
|
|
"github.com/muonsoft/validation/validator" |
11
|
|
|
"github.com/stretchr/testify/assert" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
func TestValidate_WhenArgumentForGivenType_ExpectValidationExecuted(t *testing.T) { |
15
|
|
|
tests := []struct { |
16
|
|
|
name string |
17
|
|
|
argument validation.Argument |
18
|
|
|
}{ |
19
|
|
|
{"Value", validation.Value(stringValue(""), it.IsNotBlank())}, |
20
|
|
|
{"Bool", validation.Bool(boolValue(false), it.IsNotBlank())}, |
21
|
|
|
{"Number", validation.Number(0, it.IsNotBlank())}, |
22
|
|
|
{"String", validation.String(stringValue(""), it.IsNotBlank())}, |
23
|
|
|
{"Iterable", validation.Iterable([]string{}, it.IsNotBlank())}, |
24
|
|
|
{"Countable", validation.Countable(0, it.IsNotBlank())}, |
25
|
|
|
{"Time", validation.Time(nilTime, it.IsNotBlank())}, |
26
|
|
|
{"Each", validation.Each([]string{""}, it.IsNotBlank())}, |
27
|
|
|
{"EachString", validation.EachString([]string{""}, it.IsNotBlank())}, |
28
|
|
|
{"Valid", validation.Valid(mockValidatableString{""}, it.IsNotBlank())}, |
29
|
|
|
} |
30
|
|
|
for _, test := range tests { |
31
|
|
|
t.Run(test.name, func(t *testing.T) { |
32
|
|
|
err := validator.Validate(test.argument) |
33
|
|
|
|
34
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
35
|
|
|
t.Helper() |
36
|
|
|
return assert.Len(t, violations, 1) && assert.Equal(t, code.NotBlank, violations[0].Code()) |
37
|
|
|
}) |
38
|
|
|
}) |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
func TestValidate_WhenPropertyArgument_ExpectValidPathInViolation(t *testing.T) { |
43
|
|
|
opts := []validation.Option{validation.PropertyName("internal"), it.IsNotBlank()} |
44
|
|
|
|
45
|
|
|
tests := []struct { |
46
|
|
|
name string |
47
|
|
|
argument validation.Argument |
48
|
|
|
expectedPath string |
49
|
|
|
}{ |
50
|
|
|
{"PropertyValue", validation.PropertyValue("property", stringValue(""), opts...), "property.internal"}, |
51
|
|
|
{"BoolProperty", validation.BoolProperty("property", boolValue(false), opts...), "property.internal"}, |
52
|
|
|
{"NumberProperty", validation.NumberProperty("property", 0, opts...), "property.internal"}, |
53
|
|
|
{"StringProperty", validation.StringProperty("property", stringValue(""), opts...), "property.internal"}, |
54
|
|
|
{"IterableProperty", validation.IterableProperty("property", []string{}, opts...), "property.internal"}, |
55
|
|
|
{"CountableProperty", validation.CountableProperty("property", 0, opts...), "property.internal"}, |
56
|
|
|
{"TimeProperty", validation.TimeProperty("property", nilTime, opts...), "property.internal"}, |
57
|
|
|
{"EachProperty", validation.EachProperty("property", []string{""}, opts...), "property.internal[0]"}, |
58
|
|
|
{"EachStringProperty", validation.EachStringProperty("property", []string{""}, opts...), "property.internal[0]"}, |
59
|
|
|
{"ValidProperty", validation.ValidProperty("property", mockValidatableString{""}, opts...), "property.internal.value"}, |
60
|
|
|
} |
61
|
|
|
for _, test := range tests { |
62
|
|
|
t.Run(test.name, func(t *testing.T) { |
63
|
|
|
err := validator.Validate(test.argument) |
64
|
|
|
|
65
|
|
|
validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool { |
66
|
|
|
t.Helper() |
67
|
|
|
return assert.Len(t, violations, 1) && |
68
|
|
|
assert.Equal(t, test.expectedPath, violations[0].PropertyPath().String()) |
69
|
|
|
}) |
70
|
|
|
}) |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|