|
1 | 1 | package model |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "testing" |
6 | 5 |
|
7 | | - "github.com/siherrmann/validator/helper" |
8 | 6 | "github.com/stretchr/testify/assert" |
9 | 7 | ) |
10 | 8 |
|
@@ -180,132 +178,3 @@ func TestGetValidationsFromStruct(t *testing.T) { |
180 | 178 | }) |
181 | 179 | } |
182 | 180 | } |
183 | | - |
184 | | -func TestValidationJSONMarshaling(t *testing.T) { |
185 | | - t.Run("Marshal and unmarshal Validation with nil Groups", func(t *testing.T) { |
186 | | - v := Validation{ |
187 | | - Key: "TestField", |
188 | | - Type: String, |
189 | | - Requirement: "min5", |
190 | | - Groups: nil, |
191 | | - Default: "", |
192 | | - } |
193 | | - |
194 | | - // Marshal to JSON |
195 | | - jsonBytes, err := json.Marshal(v) |
196 | | - assert.NoError(t, err, "Expected no error marshaling") |
197 | | - |
198 | | - // Unmarshal back |
199 | | - var result Validation |
200 | | - err = json.Unmarshal(jsonBytes, &result) |
201 | | - assert.NoError(t, err, "Expected no error unmarshaling") |
202 | | - assert.Equal(t, v, result, "Expected validation to match after round-trip") |
203 | | - }) |
204 | | - |
205 | | - t.Run("Marshal and unmarshal Validation with Groups", func(t *testing.T) { |
206 | | - v := Validation{ |
207 | | - Key: "TestField", |
208 | | - Type: String, |
209 | | - Requirement: "min5", |
210 | | - Groups: []*Group{{Name: "gr1", ConditionType: "min", ConditionValue: "1"}}, |
211 | | - Default: "", |
212 | | - } |
213 | | - |
214 | | - // Marshal to JSON |
215 | | - jsonBytes, err := json.Marshal(v) |
216 | | - assert.NoError(t, err, "Expected no error marshaling") |
217 | | - |
218 | | - // Unmarshal back |
219 | | - var result Validation |
220 | | - err = json.Unmarshal(jsonBytes, &result) |
221 | | - assert.NoError(t, err, "Expected no error unmarshaling") |
222 | | - assert.Equal(t, v, result, "Expected validation to match after round-trip") |
223 | | - }) |
224 | | - |
225 | | - t.Run("Unmarshal Validation from map[string]any", func(t *testing.T) { |
226 | | - jsonMap := map[string]any{ |
227 | | - "Key": "TestField", |
228 | | - "Type": String, |
229 | | - "Requirement": "min5", |
230 | | - "Groups": nil, |
231 | | - "Default": "", |
232 | | - } |
233 | | - |
234 | | - var result Validation |
235 | | - err := helper.MapJsonMapToStruct(jsonMap, &result) |
236 | | - assert.NoError(t, err, "Expected no error mapping to struct") |
237 | | - assert.Equal(t, "TestField", result.Key) |
238 | | - assert.Equal(t, String, result.Type) |
239 | | - assert.Equal(t, "min5", result.Requirement) |
240 | | - assert.Nil(t, result.Groups) |
241 | | - }) |
242 | | - |
243 | | - t.Run("Unmarshal Validation with Groups from map[string]any", func(t *testing.T) { |
244 | | - jsonMap := map[string]any{ |
245 | | - "Key": "TestField", |
246 | | - "Type": String, |
247 | | - "Requirement": "min5", |
248 | | - "Groups": []any{ |
249 | | - map[string]any{"Name": "gr1", "ConditionType": "min", "ConditionValue": "1"}, |
250 | | - }, |
251 | | - "Default": "", |
252 | | - } |
253 | | - |
254 | | - var result Validation |
255 | | - err := helper.MapJsonMapToStruct(jsonMap, &result) |
256 | | - assert.NoError(t, err, "Expected no error mapping to struct") |
257 | | - assert.Equal(t, "TestField", result.Key) |
258 | | - assert.Equal(t, String, result.Type) |
259 | | - assert.Equal(t, "min5", result.Requirement) |
260 | | - assert.NotNil(t, result.Groups) |
261 | | - assert.Len(t, result.Groups, 1) |
262 | | - assert.Equal(t, "gr1", result.Groups[0].Name) |
263 | | - }) |
264 | | - |
265 | | - t.Run("Unmarshal Validation with empty Groups from map[string]any", func(t *testing.T) { |
266 | | - jsonMap := map[string]any{ |
267 | | - "Key": "TestField", |
268 | | - "Type": String, |
269 | | - "Requirement": "min5", |
270 | | - "Groups": []any{}, |
271 | | - "Default": "", |
272 | | - } |
273 | | - |
274 | | - var result Validation |
275 | | - err := helper.MapJsonMapToStruct(jsonMap, &result) |
276 | | - assert.NoError(t, err, "Expected no error mapping to struct with empty Groups") |
277 | | - assert.Equal(t, "TestField", result.Key) |
278 | | - assert.NotNil(t, result.Groups) |
279 | | - assert.Len(t, result.Groups, 0, "Expected Groups to be empty slice") |
280 | | - }) |
281 | | - |
282 | | - t.Run("Unmarshal Validation with InnerValidation", func(t *testing.T) { |
283 | | - jsonMap := map[string]any{ |
284 | | - "Key": "TestField", |
285 | | - "Type": Struct, |
286 | | - "Requirement": "-", |
287 | | - "Groups": nil, |
288 | | - "Default": "", |
289 | | - "InnerValidation": []any{ |
290 | | - map[string]any{ |
291 | | - "Key": "InnerField", |
292 | | - "Type": String, |
293 | | - "Requirement": "min3", |
294 | | - "Groups": nil, |
295 | | - "Default": "", |
296 | | - }, |
297 | | - }, |
298 | | - } |
299 | | - |
300 | | - var result Validation |
301 | | - err := helper.MapJsonMapToStruct(jsonMap, &result) |
302 | | - assert.NoError(t, err, "Expected no error mapping to struct with InnerValidation") |
303 | | - assert.Equal(t, "TestField", result.Key) |
304 | | - assert.Equal(t, Struct, result.Type) |
305 | | - assert.NotNil(t, result.InnerValidation) |
306 | | - assert.Len(t, result.InnerValidation, 1) |
307 | | - assert.Equal(t, "InnerField", result.InnerValidation[0].Key) |
308 | | - assert.Equal(t, String, result.InnerValidation[0].Type) |
309 | | - assert.Equal(t, "min3", result.InnerValidation[0].Requirement) |
310 | | - }) |
311 | | -} |
0 commit comments