|
1
|
|
|
package validation |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"testing" |
|
8
|
|
|
|
|
9
|
|
|
"github.com/stretchr/testify/assert" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
func TestViolation_Error_MessageOnly_ErrorWithMessage(t *testing.T) { |
|
13
|
|
|
violation := internalViolation{message: "message"} |
|
14
|
|
|
|
|
15
|
|
|
err := violation.Error() |
|
16
|
|
|
|
|
17
|
|
|
assert.Equal(t, "violation: message", err) |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
func TestInternalViolation_Is(t *testing.T) { |
|
21
|
|
|
tests := []struct { |
|
22
|
|
|
name string |
|
23
|
|
|
codes []string |
|
24
|
|
|
expectedIs bool |
|
25
|
|
|
}{ |
|
26
|
|
|
{ |
|
27
|
|
|
name: "empty list", |
|
28
|
|
|
expectedIs: false, |
|
29
|
|
|
}, |
|
30
|
|
|
{ |
|
31
|
|
|
name: "no matches", |
|
32
|
|
|
codes: []string{"alpha", "beta"}, |
|
33
|
|
|
expectedIs: false, |
|
34
|
|
|
}, |
|
35
|
|
|
{ |
|
36
|
|
|
name: "one of the codes is matching", |
|
37
|
|
|
codes: []string{"alpha", "beta", "code"}, |
|
38
|
|
|
expectedIs: true, |
|
39
|
|
|
}, |
|
40
|
|
|
} |
|
41
|
|
|
for _, test := range tests { |
|
42
|
|
|
t.Run(test.name, func(t *testing.T) { |
|
43
|
|
|
violation := internalViolation{code: "code"} |
|
44
|
|
|
|
|
45
|
|
|
is := violation.Is(test.codes...) |
|
46
|
|
|
|
|
47
|
|
|
assert.Equal(t, test.expectedIs, is) |
|
48
|
|
|
}) |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
func TestViolationList_Has(t *testing.T) { |
|
53
|
|
|
tests := []struct { |
|
54
|
|
|
name string |
|
55
|
|
|
codes []string |
|
56
|
|
|
expectedIs bool |
|
57
|
|
|
}{ |
|
58
|
|
|
{ |
|
59
|
|
|
name: "empty list", |
|
60
|
|
|
expectedIs: false, |
|
61
|
|
|
}, |
|
62
|
|
|
{ |
|
63
|
|
|
name: "no matches", |
|
64
|
|
|
codes: []string{"alpha", "beta"}, |
|
65
|
|
|
expectedIs: false, |
|
66
|
|
|
}, |
|
67
|
|
|
{ |
|
68
|
|
|
name: "one of the codes is matching", |
|
69
|
|
|
codes: []string{"alpha", "beta", "code"}, |
|
70
|
|
|
expectedIs: true, |
|
71
|
|
|
}, |
|
72
|
|
|
} |
|
73
|
|
|
for _, test := range tests { |
|
74
|
|
|
t.Run(test.name, func(t *testing.T) { |
|
75
|
|
|
violations := ViolationList{internalViolation{code: "code"}} |
|
76
|
|
|
|
|
77
|
|
|
has := violations.Has(test.codes...) |
|
78
|
|
|
|
|
79
|
|
|
assert.Equal(t, test.expectedIs, has) |
|
80
|
|
|
}) |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
func TestViolationList_Filter_ViolationsWithCodes_FilteredList(t *testing.T) { |
|
85
|
|
|
violations := ViolationList{ |
|
86
|
|
|
internalViolation{code: "alpha"}, |
|
87
|
|
|
internalViolation{code: "beta"}, |
|
88
|
|
|
internalViolation{code: "gamma"}, |
|
89
|
|
|
internalViolation{code: "delta"}, |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
filtered := violations.Filter("delta", "beta") |
|
93
|
|
|
|
|
94
|
|
|
if assert.Len(t, filtered, 2) { |
|
95
|
|
|
assert.Equal(t, "beta", filtered[0].Code()) |
|
96
|
|
|
assert.Equal(t, "delta", filtered[1].Code()) |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
func TestViolation_Error_MessageAndPropertyPath_ErrorWithPropertyPathAndMessage(t *testing.T) { |
|
101
|
|
|
violation := internalViolation{ |
|
102
|
|
|
message: "message", |
|
103
|
|
|
propertyPath: PropertyPath{PropertyNameElement("propertyPath")}, |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
err := violation.Error() |
|
107
|
|
|
|
|
108
|
|
|
assert.Equal(t, "violation at 'propertyPath': message", err) |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
func TestViolationList_Error_CoupleOfViolations_JoinedMessage(t *testing.T) { |
|
112
|
|
|
violations := ViolationList{ |
|
113
|
|
|
internalViolation{ |
|
114
|
|
|
message: "first message", |
|
115
|
|
|
propertyPath: PropertyPath{PropertyNameElement("path"), ArrayIndexElement(0)}, |
|
116
|
|
|
}, |
|
117
|
|
|
internalViolation{ |
|
118
|
|
|
message: "second message", |
|
119
|
|
|
propertyPath: PropertyPath{PropertyNameElement("path"), ArrayIndexElement(1)}, |
|
120
|
|
|
}, |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
err := violations.Error() |
|
124
|
|
|
|
|
125
|
|
|
assert.Equal(t, "violation at 'path[0]': first message; violation at 'path[1]': second message", err) |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
func TestViolationList_Error_EmptyList_ErrorWithHelpMessage(t *testing.T) { |
|
129
|
|
|
violations := ViolationList{} |
|
130
|
|
|
|
|
131
|
|
|
err := violations.Error() |
|
132
|
|
|
|
|
133
|
|
|
assert.Equal(t, "the list of violations is empty, it looks like you forgot to use the AsError method somewhere", err) |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
func TestIsViolation_CustomError_False(t *testing.T) { |
|
137
|
|
|
err := errors.New("error") |
|
138
|
|
|
|
|
139
|
|
|
is := IsViolation(err) |
|
140
|
|
|
|
|
141
|
|
|
assert.False(t, is) |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
func TestIsViolation_Violation_True(t *testing.T) { |
|
145
|
|
|
err := &internalViolation{message: "message"} |
|
146
|
|
|
|
|
147
|
|
|
is := IsViolation(err) |
|
148
|
|
|
|
|
149
|
|
|
assert.True(t, is) |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
func TestIsViolationList_CustomError_False(t *testing.T) { |
|
153
|
|
|
err := errors.New("error") |
|
154
|
|
|
|
|
155
|
|
|
is := IsViolationList(err) |
|
156
|
|
|
|
|
157
|
|
|
assert.False(t, is) |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
func TestIsViolationList_Violation_True(t *testing.T) { |
|
161
|
|
|
err := ViolationList{internalViolation{message: "message"}} |
|
162
|
|
|
|
|
163
|
|
|
is := IsViolationList(err) |
|
164
|
|
|
|
|
165
|
|
|
assert.True(t, is) |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
func TestUnwrapViolation_WrappedViolation_UnwrappedViolation(t *testing.T) { |
|
169
|
|
|
wrapped := &internalViolation{message: "message"} |
|
170
|
|
|
err := fmt.Errorf("error: %w", wrapped) |
|
171
|
|
|
|
|
172
|
|
|
unwrapped, ok := UnwrapViolation(err) |
|
173
|
|
|
|
|
174
|
|
|
assert.True(t, ok) |
|
175
|
|
|
assert.Equal(t, wrapped, unwrapped) |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
func TestUnwrapViolationList_WrappedViolationList_UnwrappedViolationList(t *testing.T) { |
|
179
|
|
|
wrapped := ViolationList{internalViolation{message: "message"}} |
|
180
|
|
|
err := fmt.Errorf("error: %w", wrapped) |
|
181
|
|
|
|
|
182
|
|
|
unwrapped, ok := UnwrapViolationList(err) |
|
183
|
|
|
|
|
184
|
|
|
assert.True(t, ok) |
|
185
|
|
|
assert.Equal(t, wrapped, unwrapped) |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
func TestMarshalInternalViolationToJSON(t *testing.T) { |
|
189
|
|
|
tests := []struct { |
|
190
|
|
|
name string |
|
191
|
|
|
violation internalViolation |
|
192
|
|
|
expectedJSON string |
|
193
|
|
|
}{ |
|
194
|
|
|
{ |
|
195
|
|
|
name: "full data", |
|
196
|
|
|
violation: internalViolation{ |
|
197
|
|
|
code: "code", |
|
198
|
|
|
message: "message", |
|
199
|
|
|
messageTemplate: "messageTemplate", |
|
200
|
|
|
parameters: map[string]string{"name": "value"}, |
|
201
|
|
|
propertyPath: PropertyPath{PropertyNameElement("properties"), ArrayIndexElement(1), PropertyNameElement("name")}, |
|
202
|
|
|
}, |
|
203
|
|
|
expectedJSON: `{ |
|
204
|
|
|
"code": "code", |
|
205
|
|
|
"message": "message", |
|
206
|
|
|
"propertyPath": "properties[1].name" |
|
207
|
|
|
}`, |
|
208
|
|
|
}, |
|
209
|
|
|
{ |
|
210
|
|
|
name: "empty data", |
|
211
|
|
|
violation: internalViolation{}, |
|
212
|
|
|
expectedJSON: `{"code": "", "message": ""}`, |
|
213
|
|
|
}, |
|
214
|
|
|
} |
|
215
|
|
|
for _, test := range tests { |
|
216
|
|
|
t.Run(test.name, func(t *testing.T) { |
|
217
|
|
|
data, err := json.Marshal(test.violation) |
|
218
|
|
|
|
|
219
|
|
|
if assert.NoError(t, err) { |
|
220
|
|
|
assert.JSONEq(t, test.expectedJSON, string(data)) |
|
221
|
|
|
} |
|
222
|
|
|
}) |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|