Debian Codegraph @

helpers/path_finder_test.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // https://pb33f.io
3
4 package helpers
5
6 import (
7 "testing"
8
9 "github.com/santhosh-tekuri/jsonschema/v6"
10 "github.com/stretchr/testify/assert"
11 )
12
13 func TestDiveIntoValidationError(t *testing.T) {
14 tests := []struct {
15 name string
16 error *jsonschema.ValidationError
17 expected string
18 }{
19 {
20 name: "empty instance location",
21 error: &jsonschema.ValidationError{
22 InstanceLocation: []string{},
23 },
24 expected: "",
25 },
26 {
27 name: "numeric path segments",
28 error: &jsonschema.ValidationError{
29 InstanceLocation: []string{"root", "array", "0", "1"},
30 },
31 expected: "$.root.array[0][1]",
32 },
33 {
34 name: "simple identifier path segments",
35 error: &jsonschema.ValidationError{
36 InstanceLocation: []string{"user", "name", "first"},
37 },
38 expected: "$.user.name.first",
39 },
40 {
41 name: "complex path segments requiring escaping",
42 error: &jsonschema.ValidationError{
43 InstanceLocation: []string{"user", "name-with-dash", "special'quote", "back\\slash"},
44 },
45 expected: "$.user['name-with-dash']['special\\'quote']['back\\\\slash']",
46 },
47 {
48 name: "mixed path segments",
49 error: &jsonschema.ValidationError{
50 InstanceLocation: []string{"users", "0", "address", "street-name", "123"},
51 },
52 expected: "$.users[0].address['street-name'][123]",
53 },
54 {
55 name: "with nested causes",
56 error: &jsonschema.ValidationError{
57 InstanceLocation: []string{"root"},
58 Causes: []*jsonschema.ValidationError{
59 {
60 InstanceLocation: []string{"nested", "error"},
61 },
62 },
63 },
64 expected: "$.root",
65 },
66 }
67
68 for _, tt := range tests {
69 t.Run(tt.name, func(t *testing.T) {
70 result := ExtractJSONPathFromValidationError(tt.error)
71 assert.Equal(t, tt.expected, result)
72 })
73 }
74 }
75
76 func TestIsNumeric(t *testing.T) {
77 tests := []struct {
78 input string
79 expected bool
80 }{
81 {"123", true},
82 {"0", true},
83 {"01", true},
84 {"", false},
85 {"abc", false},
86 {"123abc", false},
87 {"12.3", false},
88 {"-123", false},
89 }
90
91 for _, tt := range tests {
92 t.Run(tt.input, func(t *testing.T) {
93 result := isNumeric(tt.input)
94 assert.Equal(t, tt.expected, result)
95 })
96 }
97 }
98
99 func TestIsSimpleIdentifier(t *testing.T) {
100 tests := []struct {
101 input string
102 expected bool
103 }{
104 {"abc", true},
105 {"a123", true},
106 {"_abc", true},
107 {"_123", true},
108 {"abc_123", true},
109 {"", false},
110 {"123abc", false},
111 {"abc-def", false},
112 {"abc.def", false},
113 {"abc def", false},
114 }
115
116 for _, tt := range tests {
117 t.Run(tt.input, func(t *testing.T) {
118 result := isSimpleIdentifier(tt.input)
119 assert.Equal(t, tt.expected, result)
120 })
121 }
122 }
123
124 func TestEscapeBracketString(t *testing.T) {
125 tests := []struct {
126 input string
127 expected string
128 }{
129 {"normal", "normal"},
130 {"with'quote", "with\\'quote"},
131 {"with\\backslash", "with\\\\backslash"},
132 {"with'quote\\and\\backslash", "with\\'quote\\\\and\\\\backslash"},
133 {"", ""},
134 }
135
136 for _, tt := range tests {
137 t.Run(tt.input, func(t *testing.T) {
138 result := escapeBracketString(tt.input)
139 assert.Equal(t, tt.expected, result)
140 })
141 }
142 }
143
144 // TestDiveIntoValidationErrorRecursion tests that the function properly handles
145 // recursive traversal through nested validation errors.
146 func TestDiveIntoValidationErrorRecursion(t *testing.T) {
147 childError1 := &jsonschema.ValidationError{
148 InstanceLocation: []string{"child1", "prop"},
149 }
150
151 childError2 := &jsonschema.ValidationError{
152 InstanceLocation: []string{"child2", "0", "name"},
153 }
154
155 parentError := &jsonschema.ValidationError{
156 InstanceLocation: []string{"parent"},
157 Causes: []*jsonschema.ValidationError{childError1, childError2},
158 }
159
160 // The parent error should return its own path
161 result := ExtractJSONPathFromValidationError(parentError)
162 assert.Equal(t, "$.parent", result)
163
164 // Verify the child errors return their paths correctly when called directly
165 assert.Equal(t, "$.child1.prop", ExtractJSONPathFromValidationError(childError1))
166 assert.Equal(t, "$.child2[0].name", ExtractJSONPathFromValidationError(childError2))
167 }
168
169 // TestDiveIntoValidationErrorEdgeCases tests edge cases including empty strings and unusual characters
170 func TestDiveIntoValidationErrorEdgeCases(t *testing.T) {
171 tests := []struct {
172 name string
173 error *jsonschema.ValidationError
174 expected string
175 }{
176 {
177 name: "empty strings as elements",
178 error: &jsonschema.ValidationError{
179 InstanceLocation: []string{"", "property"},
180 },
181 expected: "$[''].property",
182 },
183 {
184 name: "Unicode characters",
185 error: &jsonschema.ValidationError{
186 InstanceLocation: []string{"🙂", "unicode_property"},
187 },
188 expected: "$['🙂'].unicode_property",
189 },
190 {
191 name: "null causes",
192 error: &jsonschema.ValidationError{
193 InstanceLocation: []string{"root"},
194 Causes: nil,
195 },
196 expected: "$.root",
197 },
198 }
199
200 for _, tt := range tests {
201 t.Run(tt.name, func(t *testing.T) {
202 result := ExtractJSONPathFromValidationError(tt.error)
203 assert.Equal(t, tt.expected, result)
204 })
205 }
206 }
207
208 // TestExtractJSONPathsFromValidationErrors tests the ExtractJSONPathsFromValidationErrors function
209 func TestExtractJSONPathsFromValidationErrors(t *testing.T) {
210 tests := []struct {
211 name string
212 errors []*jsonschema.ValidationError
213 expected []string
214 }{
215 {
216 name: "nil errors",
217 errors: nil,
218 expected: nil,
219 },
220 {
221 name: "empty errors",
222 errors: []*jsonschema.ValidationError{},
223 expected: nil,
224 },
225 {
226 name: "single error with empty path",
227 errors: []*jsonschema.ValidationError{
228 {
229 InstanceLocation: []string{},
230 },
231 },
232 expected: nil,
233 },
234 {
235 name: "single error with path",
236 errors: []*jsonschema.ValidationError{
237 {
238 InstanceLocation: []string{"root", "property"},
239 },
240 },
241 expected: []string{"$.root.property"},
242 },
243 {
244 name: "multiple errors with paths",
245 errors: []*jsonschema.ValidationError{
246 {
247 InstanceLocation: []string{"users", "0", "name"},
248 },
249 {
250 InstanceLocation: []string{"users", "1", "address", "street"},
251 },
252 },
253 expected: []string{"$.users[0].name", "$.users[1].address.street"},
254 },
255 {
256 name: "mixed errors - some with empty paths",
257 errors: []*jsonschema.ValidationError{
258 {
259 InstanceLocation: []string{},
260 },
261 {
262 InstanceLocation: []string{"users", "0", "name"},
263 },
264 {
265 InstanceLocation: []string{},
266 },
267 },
268 expected: []string{"$.users[0].name"},
269 },
270 {
271 name: "complex paths with special characters",
272 errors: []*jsonschema.ValidationError{
273 {
274 InstanceLocation: []string{"data", "special-field", "nested"},
275 },
276 {
277 InstanceLocation: []string{"data", "array", "0", "item's", "property"},
278 },
279 },
280 expected: []string{"$.data['special-field'].nested", "$.data.array[0]['item\\'s'].property"},
281 },
282 {
283 name: "with nested causes",
284 errors: []*jsonschema.ValidationError{
285 {
286 InstanceLocation: []string{"parent"},
287 Causes: []*jsonschema.ValidationError{
288 {
289 InstanceLocation: []string{"child", "property"},
290 },
291 },
292 },
293 },
294 expected: []string{"$.parent"},
295 },
296 }
297
298 for _, tt := range tests {
299 t.Run(tt.name, func(t *testing.T) {
300 result := ExtractJSONPathsFromValidationErrors(tt.errors)
301 assert.Equal(t, tt.expected, result)
302 })
303 }
304 }
305
306 func TestExtractFieldNameFromInstanceLocation(t *testing.T) {
307 testCases := []struct {
308 name string
309 instancePath []string
310 expected string
311 }{
312 {
313 name: "Empty path",
314 instancePath: []string{},
315 expected: "",
316 },
317 {
318 name: "Single field",
319 instancePath: []string{"name"},
320 expected: "name",
321 },
322 {
323 name: "Nested field",
324 instancePath: []string{"user", "profile", "email"},
325 expected: "email",
326 },
327 {
328 name: "Array index",
329 instancePath: []string{"users", "0", "name"},
330 expected: "name",
331 },
332 {
333 name: "Complex path",
334 instancePath: []string{"root", "nested", "array", "1", "field"},
335 expected: "field",
336 },
337 {
338 name: "Field with special characters",
339 instancePath: []string{"user", "email-address", "value"},
340 expected: "value",
341 },
342 {
343 name: "Numeric field name",
344 instancePath: []string{"data", "123"},
345 expected: "123",
346 },
347 }
348
349 for _, tc := range testCases {
350 t.Run(tc.name, func(t *testing.T) {
351 result := ExtractFieldNameFromInstanceLocation(tc.instancePath)
352 assert.Equal(t, tc.expected, result)
353 })
354 }
355 }
356
357 func TestExtractJSONPathFromInstanceLocation(t *testing.T) {
358 testCases := []struct {
359 name string
360 instancePath []string
361 expected string
362 }{
363 {
364 name: "Empty path",
365 instancePath: []string{},
366 expected: "",
367 },
368 {
369 name: "Simple field",
370 instancePath: []string{"name"},
371 expected: "$.name",
372 },
373 {
374 name: "Nested object fields",
375 instancePath: []string{"user", "profile", "email"},
376 expected: "$.user.profile.email",
377 },
378 {
379 name: "Array access",
380 instancePath: []string{"users", "0", "name"},
381 expected: "$.users[0].name",
382 },
383 {
384 name: "Mixed array and object",
385 instancePath: []string{"data", "items", "1", "properties", "value"},
386 expected: "$.data.items[1].properties.value",
387 },
388 {
389 name: "Field with dashes",
390 instancePath: []string{"user", "email-address"},
391 expected: "$.user['email-address']",
392 },
393 {
394 name: "Field with spaces",
395 instancePath: []string{"user", "full name"},
396 expected: "$.user['full name']",
397 },
398 {
399 name: "Field with special characters",
400 instancePath: []string{"data", "field'with'quotes"},
401 expected: "$.data['field\\'with\\'quotes']",
402 },
403 {
404 name: "Field with backslash",
405 instancePath: []string{"data", "field\\with\\backslash"},
406 expected: "$.data['field\\\\with\\\\backslash']",
407 },
408 {
409 name: "Unicode field name",
410 instancePath: []string{"🙂", "unicode_field"},
411 expected: "$['🙂'].unicode_field",
412 },
413 {
414 name: "Numeric array indices",
415 instancePath: []string{"matrix", "0", "1", "value"},
416 expected: "$.matrix[0][1].value",
417 },
418 }
419
420 for _, tc := range testCases {
421 t.Run(tc.name, func(t *testing.T) {
422 result := ExtractJSONPathFromInstanceLocation(tc.instancePath)
423 assert.Equal(t, tc.expected, result)
424 })
425 }
426 }
427
428 func TestExtractFieldNameFromStringLocation(t *testing.T) {
429 testCases := []struct {
430 name string
431 instancePath string
432 expected string
433 }{
434 {
435 name: "Empty path",
436 instancePath: "",
437 expected: "",
438 },
439 {
440 name: "Single field",
441 instancePath: "/name",
442 expected: "name",
443 },
444 {
445 name: "Nested field",
446 instancePath: "/user/profile/email",
447 expected: "email",
448 },
449 {
450 name: "Array index",
451 instancePath: "/users/0/name",
452 expected: "name",
453 },
454 {
455 name: "Complex path",
456 instancePath: "/root/nested/array/1/field",
457 expected: "field",
458 },
459 {
460 name: "Field with special characters",
461 instancePath: "/user/email-address/value",
462 expected: "value",
463 },
464 {
465 name: "Root path only",
466 instancePath: "/",
467 expected: "",
468 },
469 }
470
471 for _, tc := range testCases {
472 t.Run(tc.name, func(t *testing.T) {
473 result := ExtractFieldNameFromStringLocation(tc.instancePath)
474 assert.Equal(t, tc.expected, result)
475 })
476 }
477 }
478
479 func TestExtractJSONPathFromStringLocation(t *testing.T) {
480 testCases := []struct {
481 name string
482 instancePath string
483 expected string
484 }{
485 {
486 name: "Empty path",
487 instancePath: "",
488 expected: "",
489 },
490 {
491 name: "Simple field",
492 instancePath: "/name",
493 expected: "$.name",
494 },
495 {
496 name: "Nested object fields",
497 instancePath: "/user/profile/email",
498 expected: "$.user.profile.email",
499 },
500 {
501 name: "Array access",
502 instancePath: "/users/0/name",
503 expected: "$.users[0].name",
504 },
505 {
506 name: "Mixed array and object",
507 instancePath: "/data/items/1/properties/value",
508 expected: "$.data.items[1].properties.value",
509 },
510 {
511 name: "Root path only",
512 instancePath: "/",
513 expected: "",
514 },
515 {
516 name: "Complex nested path",
517 instancePath: "/matrix/0/1/value",
518 expected: "$.matrix[0][1].value",
519 },
520 }
521
522 for _, tc := range testCases {
523 t.Run(tc.name, func(t *testing.T) {
524 result := ExtractJSONPathFromStringLocation(tc.instancePath)
525 assert.Equal(t, tc.expected, result)
526 })
527 }
528 }
529
530 func TestConvertStringLocationToPathSegments(t *testing.T) {
531 testCases := []struct {
532 name string
533 instancePath string
534 expected []string
535 }{
536 {
537 name: "Empty string",
538 instancePath: "",
539 expected: []string{},
540 },
541 {
542 name: "Root path only",
543 instancePath: "/",
544 expected: []string{},
545 },
546 {
547 name: "Single field",
548 instancePath: "/name",
549 expected: []string{"name"},
550 },
551 {
552 name: "Multiple fields",
553 instancePath: "/user/profile/email",
554 expected: []string{"user", "profile", "email"},
555 },
556 {
557 name: "Array index",
558 instancePath: "/users/0/name",
559 expected: []string{"users", "0", "name"},
560 },
561 {
562 name: "Multiple array indices",
563 instancePath: "/matrix/0/1/value",
564 expected: []string{"matrix", "0", "1", "value"},
565 },
566 {
567 name: "Field with special characters",
568 instancePath: "/user/email-address",
569 expected: []string{"user", "email-address"},
570 },
571 {
572 name: "Complex nested path",
573 instancePath: "/data/items/1/properties/field-name",
574 expected: []string{"data", "items", "1", "properties", "field-name"},
575 },
576 {
577 name: "Leading and trailing slashes",
578 instancePath: "///user/name///",
579 expected: []string{"user", "name"},
580 },
581 }
582
583 for _, tc := range testCases {
584 t.Run(tc.name, func(t *testing.T) {
585 result := ConvertStringLocationToPathSegments(tc.instancePath)
586 assert.Equal(t, tc.expected, result)
587 })
588 }
589 }