-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_value_test.go
148 lines (122 loc) · 3.18 KB
/
for_value_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package validator
import (
"context"
"errors"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
ve "github.com/donatorsky/go-validator/error"
vr "github.com/donatorsky/go-validator/rule"
)
func Test_ForValue(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
var (
ctx = context.Background()
data = fakerInstance.Int()
valueRuleMocks, valueValidationErrorsBag = setupValueMocks(ctx, ctrl, "*", data, data)
)
// when
validationErrors, err := ForValue(data, valueRuleMocks)
// then
require.NoError(t, err)
require.Len(t, validationErrors, 2)
require.Equal(t, valueValidationErrorsBag.Get("*"), validationErrors)
}
func Test_ForValueWithContext(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
var (
ctx = context.TODO()
data = fakerInstance.Int()
valueRuleMocks, valueValidationErrorsBag = setupValueMocks(ctx, ctrl, "*", data, data)
)
// when
validationErrors, err := ForValueWithContext(ctx, data, valueRuleMocks)
// then
require.NoError(t, err)
require.Len(t, validationErrors, 2)
require.Equal(t, valueValidationErrorsBag.Get("*"), validationErrors)
}
func Test_ForValueWithContext_ReturnsErrorFromOption(t *testing.T) {
// given
var errFromOption = errors.New(fakerInstance.Lorem().Sentence(6))
// when
validationErrors, err := ForValueWithContext[any](context.TODO(), nil, nil, func(_ *validatorOptions) error {
return errFromOption
})
// then
require.ErrorIs(t, err, errFromOption)
require.Nil(t, validationErrors)
}
func Test_ForValueWithContext_WithValueExporter(t *testing.T) {
// given
var (
ctx = context.TODO()
data = fakerInstance.IntBetween(-9999, 9999)
)
t.Run("value is not assigned when validation error occurs", func(t *testing.T) {
var out = 10_000
// when
validationErrors, err := ForValueWithContext(
ctx,
data,
[]vr.Rule{
vr.Required(),
vr.Custom(func(_ context.Context, _ any, _ any) (any, error) {
return nil, errors.New("validation failed")
}),
},
ForValueWithValueExporter(&out),
)
// then
require.NoError(t, err)
require.NotEmpty(t, validationErrors)
require.Equal(t, 10_000, out)
})
t.Run("value is not assigned when output value type differs", func(t *testing.T) {
var out int
// when
validationErrors, err := ForValueWithContext(
ctx,
data,
[]vr.Rule{
vr.Required(),
vr.Custom(func(_ context.Context, _ int, _ any) (string, error) {
return "", nil
}),
},
ForValueWithValueExporter(&out),
)
// then
require.ErrorIs(t, err, ve.ValueExporterTypeMismatchError{
ValueType: "string",
TargetType: "int",
})
require.Empty(t, validationErrors)
})
t.Run("changed value is assigned after successful validation", func(t *testing.T) {
var (
newValue = fakerInstance.Int()
out int
)
// when
validationErrors, err := ForValueWithContext(
ctx,
data,
[]vr.Rule{
vr.Required(),
vr.Custom(func(_ context.Context, _ int, _ any) (int, error) {
return newValue, nil
}),
},
ForValueWithValueExporter(&out),
)
// then
require.NoError(t, err)
require.Empty(t, validationErrors)
require.Equal(t, newValue, out)
})
}