-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_slice_test.go
162 lines (133 loc) · 4.59 KB
/
for_slice_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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_ForSlice(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
var (
ctx = context.Background()
data = []int{
fakerInstance.IntBetween(-100, -1),
fakerInstance.IntBetween(0, 100),
fakerInstance.IntBetween(1000, 9999),
}
valueRuleMocks, sliceElementsValidationErrorsBag = setupSliceElementsMocks(ctx, ctrl, "_", data, data)
)
// when
validationErrors, err := ForSlice(data, valueRuleMocks["_.*"])
// then
require.NoError(t, err)
require.Len(t, validationErrors, 3)
require.True(t, assertErrorsBagContainsErrorsForField(t, sliceElementsValidationErrorsBag, validationErrors["0"], "_.0"))
require.True(t, assertErrorsBagContainsErrorsForField(t, sliceElementsValidationErrorsBag, validationErrors["1"], "_.1"))
require.True(t, assertErrorsBagContainsErrorsForField(t, sliceElementsValidationErrorsBag, validationErrors["2"], "_.2"))
}
func Test_ForSlice_FailsWhenInvalidDataProvided(t *testing.T) {
// when
_, err := ForSlice(1, nil)
// then
require.ErrorIs(t, err, ve.NotListTypeError{})
}
func Test_ForSliceWithContext(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// given
var (
ctx = context.TODO()
data = []int{
fakerInstance.IntBetween(-100, -1),
fakerInstance.IntBetween(0, 100),
fakerInstance.IntBetween(1000, 9999),
}
valueRuleMocks, sliceElementsValidationErrorsBag = setupSliceElementsMocks(ctx, ctrl, "_", data, data)
)
// when
validationErrors, err := ForSliceWithContext(ctx, data, valueRuleMocks["_.*"])
// then
require.NoError(t, err)
require.Len(t, validationErrors, 3)
require.True(t, assertErrorsBagContainsErrorsForField(t, sliceElementsValidationErrorsBag, validationErrors["0"], "_.0"))
require.True(t, assertErrorsBagContainsErrorsForField(t, sliceElementsValidationErrorsBag, validationErrors["1"], "_.1"))
require.True(t, assertErrorsBagContainsErrorsForField(t, sliceElementsValidationErrorsBag, validationErrors["2"], "_.2"))
}
func Test_ForSliceWithContext_FailsWhenInvalidDataProvided(t *testing.T) {
// when
_, err := ForSliceWithContext(context.TODO(), 1, nil)
// then
require.ErrorIs(t, err, ve.NotListTypeError{})
}
func Test_ForSliceWithContext_ReturnsErrorFromOption(t *testing.T) {
// given
var (
ctx = context.TODO()
data = []int{
fakerInstance.IntBetween(-100, -1),
fakerInstance.IntBetween(0, 100),
fakerInstance.IntBetween(1000, 9999),
}
errFromOption = errors.New(fakerInstance.Lorem().Sentence(6))
)
// when
validationErrors, err := ForSliceWithContext(ctx, data, nil, func(_ *validatorOptions) error {
return errFromOption
})
// then
require.ErrorIs(t, err, errFromOption)
require.Nil(t, validationErrors)
}
func Test_ForSliceWithContext_WithDataCollector(t *testing.T) {
// given
var (
ctx = context.TODO()
data = []int{
fakerInstance.IntBetween(-100, -1),
fakerInstance.IntBetween(0, 100),
fakerInstance.IntBetween(1000, 9999),
}
)
t.Run("value is not assigned when validation error occurs", func(t *testing.T) {
collector := NewMapDataCollector()
// when
errorsBag, err := ForSliceWithContext(ctx, data, []vr.Rule{
vr.Required(),
vr.Custom(func(_ context.Context, _ any, _ any) (any, error) {
return nil, errors.New("validation failed")
}),
}, ForSliceWithDataCollector(collector))
// then
require.NoError(t, err)
require.True(t, errorsBag.Has("0"))
require.True(t, errorsBag.Has("1"))
require.True(t, errorsBag.Has("2"))
require.True(t, assertCollectorDoesNotHaveKey(t, collector, "0"))
require.True(t, assertCollectorDoesNotHaveKey(t, collector, "1"))
require.True(t, assertCollectorDoesNotHaveKey(t, collector, "2"))
})
t.Run("changed value is assigned after successful validation", func(t *testing.T) {
var newValue = fakerInstance.Int()
collector := NewMapDataCollector()
// when
errorsBag, err := ForSliceWithContext(ctx, data, []vr.Rule{
vr.Required(),
vr.Custom(func(_ context.Context, _ int, _ any) (int, error) {
return newValue, nil
}),
}, ForSliceWithDataCollector(collector))
// then
require.NoError(t, err)
require.False(t, errorsBag.Has("0"))
require.False(t, errorsBag.Has("1"))
require.False(t, errorsBag.Has("2"))
require.True(t, assertCollectorHasValue(t, collector, "0", newValue))
require.True(t, assertCollectorHasValue(t, collector, "1", newValue))
require.True(t, assertCollectorHasValue(t, collector, "2", newValue))
})
}