blob: 4d3a4115cf3a8064e354c52049be72f6f9ca4c87 [file] [log] [blame]
Akron32d53de2025-05-22 13:45:32 +02001package mapper
2
3import (
4 "encoding/json"
Akron32d53de2025-05-22 13:45:32 +02005 "testing"
6
Akron2ef703c2025-07-03 15:57:42 +02007 "github.com/KorAP/Koral-Mapper/ast"
8 "github.com/KorAP/Koral-Mapper/config"
9 "github.com/KorAP/Koral-Mapper/matcher"
Akron32d53de2025-05-22 13:45:32 +020010 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12)
13
14func TestMapper(t *testing.T) {
Akrona00d4752025-05-26 17:34:36 +020015 // Create test mapping list
16 mappingList := config.MappingList{
17 ID: "test-mapper",
18 FoundryA: "opennlp",
19 LayerA: "p",
20 FoundryB: "upos",
21 LayerB: "p",
22 Mappings: []config.MappingRule{
23 "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]",
24 "[DET] <> [opennlp/p=DET]",
25 },
26 }
Akron32d53de2025-05-22 13:45:32 +020027
28 // Create a new mapper
Akrona00d4752025-05-26 17:34:36 +020029 m, err := NewMapper([]config.MappingList{mappingList})
Akron32d53de2025-05-22 13:45:32 +020030 require.NoError(t, err)
31
32 tests := []struct {
33 name string
34 mappingID string
35 opts MappingOptions
36 input string
37 expected string
38 expectError bool
39 }{
40 {
41 name: "Simple A to B mapping",
42 mappingID: "test-mapper",
43 opts: MappingOptions{
44 Direction: AtoB,
45 },
46 input: `{
47 "@type": "koral:token",
48 "wrap": {
49 "@type": "koral:term",
50 "foundry": "opennlp",
51 "key": "PIDAT",
52 "layer": "p",
53 "match": "match:eq"
54 }
55 }`,
56 expected: `{
57 "@type": "koral:token",
58 "wrap": {
59 "@type": "koral:termGroup",
60 "operands": [
61 {
62 "@type": "koral:term",
63 "foundry": "opennlp",
64 "key": "PIDAT",
65 "layer": "p",
66 "match": "match:eq"
67 },
68 {
69 "@type": "koral:term",
70 "foundry": "opennlp",
71 "key": "AdjType",
72 "layer": "p",
73 "match": "match:eq",
74 "value": "Pdt"
75 }
76 ],
77 "relation": "relation:and"
78 }
79 }`,
80 },
81 {
Akron0d9117c2025-05-27 15:20:21 +020082 name: "Simple A to B mapping with rewrites",
Akron32d53de2025-05-22 13:45:32 +020083 mappingID: "test-mapper",
84 opts: MappingOptions{
Akron0d9117c2025-05-27 15:20:21 +020085 Direction: AtoB,
86 AddRewrites: true,
Akron32d53de2025-05-22 13:45:32 +020087 },
88 input: `{
89 "@type": "koral:token",
90 "wrap": {
Akrona1a183f2025-05-26 17:47:33 +020091 "@type": "koral:term",
92 "foundry": "opennlp",
93 "key": "PIDAT",
94 "layer": "p",
95 "match": "match:eq"
Akron32d53de2025-05-22 13:45:32 +020096 }
97 }`,
98 expected: `{
99 "@type": "koral:token",
100 "wrap": {
Akron0d9117c2025-05-27 15:20:21 +0200101 "@type": "koral:termGroup",
102 "operands": [
103 {
104 "@type": "koral:term",
105 "foundry": "opennlp",
106 "key": "PIDAT",
107 "layer": "p",
108 "match": "match:eq"
109 },
110 {
111 "@type": "koral:term",
112 "foundry": "opennlp",
113 "key": "AdjType",
114 "layer": "p",
115 "match": "match:eq",
116 "value": "Pdt"
117 }
118 ],
119 "relation": "relation:and",
120 "rewrites": [
121 {
122 "@type": "koral:rewrite",
Akron2f93c582026-02-19 16:49:13 +0100123 "editor": "Koral-Mapper",
Akron8a87d9a2025-05-27 15:30:48 +0200124 "original": {
Akron0d9117c2025-05-27 15:20:21 +0200125 "@type": "koral:term",
126 "foundry": "opennlp",
127 "key": "PIDAT",
128 "layer": "p",
129 "match": "match:eq"
130 }
131 }
132 ]
Akron32d53de2025-05-22 13:45:32 +0200133 }
134 }`,
135 },
136 {
Akron0d9117c2025-05-27 15:20:21 +0200137 name: "Mapping with foundry override and rewrites",
Akron32d53de2025-05-22 13:45:32 +0200138 mappingID: "test-mapper",
139 opts: MappingOptions{
Akron0d9117c2025-05-27 15:20:21 +0200140 Direction: AtoB,
141 FoundryB: "custom",
142 AddRewrites: true,
Akron32d53de2025-05-22 13:45:32 +0200143 },
144 input: `{
145 "@type": "koral:token",
146 "wrap": {
147 "@type": "koral:term",
148 "foundry": "opennlp",
149 "key": "PIDAT",
150 "layer": "p",
151 "match": "match:eq"
152 }
153 }`,
154 expected: `{
155 "@type": "koral:token",
156 "wrap": {
157 "@type": "koral:termGroup",
158 "operands": [
159 {
160 "@type": "koral:term",
161 "foundry": "custom",
162 "key": "PIDAT",
163 "layer": "p",
164 "match": "match:eq"
165 },
166 {
167 "@type": "koral:term",
168 "foundry": "custom",
169 "key": "AdjType",
170 "layer": "p",
171 "match": "match:eq",
172 "value": "Pdt"
173 }
174 ],
Akron0d9117c2025-05-27 15:20:21 +0200175 "relation": "relation:and",
176 "rewrites": [
177 {
178 "@type": "koral:rewrite",
Akron2f93c582026-02-19 16:49:13 +0100179 "editor": "Koral-Mapper",
Akron8a87d9a2025-05-27 15:30:48 +0200180 "original": {
181 "@type": "koral:term",
182 "foundry": "opennlp",
183 "key": "PIDAT",
184 "layer": "p",
185 "match": "match:eq"
186 }
Akron0d9117c2025-05-27 15:20:21 +0200187 }
188 ]
Akron32d53de2025-05-22 13:45:32 +0200189 }
190 }`,
191 },
192 {
Akron0d9117c2025-05-27 15:20:21 +0200193 name: "B to A direction",
194 mappingID: "test-mapper",
195 opts: MappingOptions{
196 Direction: BtoA,
197 },
198 input: `{
199 "@type": "koral:token",
200 "wrap": {
201 "@type": "koral:term",
202 "foundry": "opennlp",
203 "key": "PIDAT",
204 "layer": "p",
205 "match": "match:eq"
206 }
207 }`,
208 expected: `{
209 "@type": "koral:token",
210 "wrap": {
211 "@type": "koral:term",
212 "foundry": "opennlp",
213 "key": "PIDAT",
214 "layer": "p",
215 "match": "match:eq"
216 }
217 }`,
218 expectError: false,
219 },
220 {
Akron32d53de2025-05-22 13:45:32 +0200221 name: "Invalid mapping ID",
222 mappingID: "nonexistent",
223 opts: MappingOptions{
224 Direction: AtoB,
225 },
226 input: `{
227 "@type": "koral:token",
228 "wrap": {
229 "@type": "koral:term",
230 "foundry": "opennlp",
231 "key": "PIDAT",
232 "layer": "p",
233 "match": "match:eq"
234 }
235 }`,
236 expectError: true,
237 },
238 {
239 name: "Invalid direction",
240 mappingID: "test-mapper",
241 opts: MappingOptions{
Akrona1a183f2025-05-26 17:47:33 +0200242 Direction: Direction(false),
Akron32d53de2025-05-22 13:45:32 +0200243 },
244 input: `{
245 "@type": "koral:token",
246 "wrap": {
247 "@type": "koral:term",
248 "foundry": "opennlp",
249 "key": "PIDAT",
250 "layer": "p",
251 "match": "match:eq"
252 }
253 }`,
Akrona1a183f2025-05-26 17:47:33 +0200254 expected: `{
255 "@type": "koral:token",
256 "wrap": {
257 "@type": "koral:term",
258 "foundry": "opennlp",
259 "key": "PIDAT",
260 "layer": "p",
261 "match": "match:eq"
262 }
263 }`,
264 expectError: false,
Akron32d53de2025-05-22 13:45:32 +0200265 },
Akron8f1970f2025-05-30 12:52:03 +0200266 {
267 name: "Query with legacy rewrite field names",
268 mappingID: "test-mapper",
269 opts: MappingOptions{
270 Direction: AtoB,
271 },
272 input: `{
273 "@type": "koral:token",
274 "rewrites": [
275 {
276 "@type": "koral:rewrite",
277 "_comment": "Legacy rewrite with source instead of editor",
278 "source": "LegacyEditor",
279 "operation": "operation:legacy",
280 "origin": "LegacySource"
281 }
282 ],
283 "wrap": {
284 "@type": "koral:term",
285 "foundry": "opennlp",
286 "key": "PIDAT",
287 "layer": "p",
288 "match": "match:eq"
289 }
290 }`,
291 expected: `{
292 "@type": "koral:token",
293 "rewrites": [
294 {
295 "@type": "koral:rewrite",
296 "_comment": "Legacy rewrite with source instead of editor",
297 "editor": "LegacyEditor",
298 "operation": "operation:legacy",
299 "src": "LegacySource"
300 }
301 ],
302 "wrap": {
303 "@type": "koral:termGroup",
304 "operands": [
305 {
306 "@type": "koral:term",
307 "foundry": "opennlp",
308 "key": "PIDAT",
309 "layer": "p",
310 "match": "match:eq"
311 },
312 {
313 "@type": "koral:term",
314 "foundry": "opennlp",
315 "key": "AdjType",
316 "layer": "p",
317 "match": "match:eq",
318 "value": "Pdt"
319 }
320 ],
321 "relation": "relation:and"
322 }
323 }`,
324 },
325 {
326 name: "Query with mixed legacy and modern rewrite fields",
327 mappingID: "test-mapper",
328 opts: MappingOptions{
329 Direction: AtoB,
330 },
331 input: `{
332 "@type": "koral:token",
333 "rewrites": [
334 {
335 "@type": "koral:rewrite",
336 "_comment": "Modern rewrite",
337 "editor": "ModernEditor",
338 "operation": "operation:modern",
339 "original": {
340 "@type": "koral:term",
341 "foundry": "original",
342 "key": "original-key"
343 }
344 },
345 {
346 "@type": "koral:rewrite",
347 "_comment": "Legacy rewrite with precedence test",
348 "editor": "PreferredEditor",
349 "source": "IgnoredSource",
350 "operation": "operation:precedence",
351 "original": "PreferredOriginal",
352 "src": "IgnoredSrc",
353 "origin": "IgnoredOrigin"
354 }
355 ],
356 "wrap": {
357 "@type": "koral:term",
358 "foundry": "opennlp",
359 "key": "PIDAT",
360 "layer": "p",
361 "match": "match:eq"
362 }
363 }`,
364 expected: `{
365 "@type": "koral:token",
366 "rewrites": [
367 {
368 "@type": "koral:rewrite",
369 "_comment": "Modern rewrite",
370 "editor": "ModernEditor",
371 "operation": "operation:modern",
372 "original": {
373 "@type": "koral:term",
374 "foundry": "original",
375 "key": "original-key"
376 }
377 },
378 {
379 "@type": "koral:rewrite",
380 "_comment": "Legacy rewrite with precedence test",
381 "editor": "PreferredEditor",
382 "operation": "operation:precedence",
383 "original": "PreferredOriginal"
384 }
385 ],
386 "wrap": {
387 "@type": "koral:termGroup",
388 "operands": [
389 {
390 "@type": "koral:term",
391 "foundry": "opennlp",
392 "key": "PIDAT",
393 "layer": "p",
394 "match": "match:eq"
395 },
396 {
397 "@type": "koral:term",
398 "foundry": "opennlp",
399 "key": "AdjType",
400 "layer": "p",
401 "match": "match:eq",
402 "value": "Pdt"
403 }
404 ],
405 "relation": "relation:and"
406 }
407 }`,
408 },
Akron32d53de2025-05-22 13:45:32 +0200409 }
410
411 for _, tt := range tests {
412 t.Run(tt.name, func(t *testing.T) {
413 // Parse input JSON
Akron121c66e2025-06-02 16:34:05 +0200414 var inputData any
Akron32d53de2025-05-22 13:45:32 +0200415 err := json.Unmarshal([]byte(tt.input), &inputData)
416 require.NoError(t, err)
417
418 // Apply mappings
Akron7b4984e2025-05-26 19:12:20 +0200419 result, err := m.ApplyQueryMappings(tt.mappingID, tt.opts, inputData)
Akron32d53de2025-05-22 13:45:32 +0200420 if tt.expectError {
421 assert.Error(t, err)
422 return
423 }
424 require.NoError(t, err)
425
426 // Parse expected JSON
Akron121c66e2025-06-02 16:34:05 +0200427 var expectedData any
Akron32d53de2025-05-22 13:45:32 +0200428 err = json.Unmarshal([]byte(tt.expected), &expectedData)
429 require.NoError(t, err)
430
431 // Compare results
432 assert.Equal(t, expectedData, result)
433 })
434 }
435}
Akrond5850f82025-05-23 16:44:44 +0200436
Akroncc83eb52025-05-27 14:39:12 +0200437func TestTokenToTermGroupWithRewrites(t *testing.T) {
438 // Create test mapping list specifically for token to termGroup test
439 mappingList := config.MappingList{
440 ID: "test-token-to-termgroup",
441 FoundryA: "opennlp",
442 LayerA: "p",
443 FoundryB: "opennlp", // Keep the same foundry for both sides
444 LayerB: "p",
445 Mappings: []config.MappingRule{
446 "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]",
447 },
448 }
449
450 // Create a new mapper
451 m, err := NewMapper([]config.MappingList{mappingList})
452 require.NoError(t, err)
453
454 input := `{
455 "@type": "koral:token",
456 "rewrites": [
457 {
458 "@type": "koral:rewrite",
459 "_comment": "This rewrite should be preserved",
460 "editor": "TestEditor",
461 "operation": "operation:test",
462 "src": "TestSource"
463 }
464 ],
465 "wrap": {
466 "@type": "koral:term",
467 "foundry": "opennlp",
468 "key": "PIDAT",
469 "layer": "p",
470 "match": "match:eq"
471 }
472 }`
473
474 expected := `{
475 "@type": "koral:token",
476 "rewrites": [
477 {
478 "@type": "koral:rewrite",
479 "_comment": "This rewrite should be preserved",
480 "editor": "TestEditor",
481 "operation": "operation:test",
482 "src": "TestSource"
483 }
484 ],
485 "wrap": {
486 "@type": "koral:termGroup",
487 "operands": [
488 {
489 "@type": "koral:term",
490 "foundry": "opennlp",
491 "key": "PIDAT",
492 "layer": "p",
493 "match": "match:eq"
494 },
495 {
496 "@type": "koral:term",
497 "foundry": "opennlp",
498 "key": "AdjType",
499 "layer": "p",
500 "match": "match:eq",
501 "value": "Pdt"
502 }
503 ],
504 "relation": "relation:and"
505 }
506 }`
507
508 // Parse input JSON
Akron121c66e2025-06-02 16:34:05 +0200509 var inputData any
Akroncc83eb52025-05-27 14:39:12 +0200510 err = json.Unmarshal([]byte(input), &inputData)
511 require.NoError(t, err)
512
513 // Apply mappings
514 result, err := m.ApplyQueryMappings("test-token-to-termgroup", MappingOptions{Direction: AtoB}, inputData)
515 require.NoError(t, err)
516
517 // Parse expected JSON
Akron121c66e2025-06-02 16:34:05 +0200518 var expectedData any
Akroncc83eb52025-05-27 14:39:12 +0200519 err = json.Unmarshal([]byte(expected), &expectedData)
520 require.NoError(t, err)
521
522 // Compare results
523 assert.Equal(t, expectedData, result)
524}
525
Akrond5850f82025-05-23 16:44:44 +0200526func TestMatchComplexPatterns(t *testing.T) {
527 tests := []struct {
528 name string
529 pattern ast.Pattern
530 replacement ast.Replacement
531 input ast.Node
532 expected ast.Node
533 }{
534 {
535 name: "Deep nested pattern with mixed operators",
536 pattern: ast.Pattern{
537 Root: &ast.TermGroup{
538 Operands: []ast.Node{
539 &ast.Term{
540 Key: "A",
541 Match: ast.MatchEqual,
542 },
543 &ast.TermGroup{
544 Operands: []ast.Node{
545 &ast.Term{
546 Key: "B",
547 Match: ast.MatchEqual,
548 },
549 &ast.TermGroup{
550 Operands: []ast.Node{
551 &ast.Term{
552 Key: "C",
553 Match: ast.MatchEqual,
554 },
555 &ast.Term{
556 Key: "D",
557 Match: ast.MatchEqual,
558 },
559 },
560 Relation: ast.AndRelation,
561 },
562 },
563 Relation: ast.OrRelation,
564 },
565 },
566 Relation: ast.AndRelation,
567 },
568 },
569 replacement: ast.Replacement{
570 Root: &ast.Term{
571 Key: "RESULT",
572 Match: ast.MatchEqual,
573 },
574 },
575 input: &ast.TermGroup{
576 Operands: []ast.Node{
577 &ast.Term{
578 Key: "A",
579 Match: ast.MatchEqual,
580 },
581 &ast.TermGroup{
582 Operands: []ast.Node{
583 &ast.Term{
584 Key: "C",
585 Match: ast.MatchEqual,
586 },
587 &ast.Term{
588 Key: "D",
589 Match: ast.MatchEqual,
590 },
591 },
592 Relation: ast.AndRelation,
593 },
594 },
595 Relation: ast.AndRelation,
596 },
597 expected: &ast.Term{
598 Key: "RESULT",
599 Match: ast.MatchEqual,
600 },
601 },
602 }
603
604 for _, tt := range tests {
605 t.Run(tt.name, func(t *testing.T) {
606 m, err := matcher.NewMatcher(tt.pattern, tt.replacement)
607 require.NoError(t, err)
608 result := m.Replace(tt.input)
609 assert.Equal(t, tt.expected, result)
610 })
611 }
612}
613
614func TestInvalidPatternReplacement(t *testing.T) {
Akrona00d4752025-05-26 17:34:36 +0200615 // Create test mapping list
616 mappingList := config.MappingList{
617 ID: "test-mapper",
618 FoundryA: "opennlp",
619 LayerA: "p",
620 FoundryB: "upos",
621 LayerB: "p",
622 Mappings: []config.MappingRule{
623 "[PIDAT] <> [opennlp/p=PIDAT & opennlp/p=AdjType:Pdt]",
624 },
625 }
Akrond5850f82025-05-23 16:44:44 +0200626
627 // Create a new mapper
Akrona00d4752025-05-26 17:34:36 +0200628 m, err := NewMapper([]config.MappingList{mappingList})
Akrond5850f82025-05-23 16:44:44 +0200629 require.NoError(t, err)
630
631 tests := []struct {
632 name string
633 input string
634 expectError bool
635 errorMsg string
636 }{
637 {
638 name: "Invalid input - empty term group",
639 input: `{
640 "@type": "koral:token",
641 "wrap": {
642 "@type": "koral:termGroup",
643 "operands": [],
644 "relation": "relation:and"
645 }
646 }`,
647 expectError: true,
648 errorMsg: "failed to parse JSON into AST: error parsing wrapped node: term group must have at least one operand",
649 },
650 }
651
652 for _, tt := range tests {
653 t.Run(tt.name, func(t *testing.T) {
654 var inputData any
655 err := json.Unmarshal([]byte(tt.input), &inputData)
656 require.NoError(t, err)
657
Akron7b4984e2025-05-26 19:12:20 +0200658 result, err := m.ApplyQueryMappings("test-mapper", MappingOptions{Direction: AtoB}, inputData)
Akrond5850f82025-05-23 16:44:44 +0200659 if tt.expectError {
660 assert.Error(t, err)
661 assert.Equal(t, tt.errorMsg, err.Error())
662 assert.Nil(t, result)
663 } else {
664 assert.NoError(t, err)
665 assert.NotNil(t, result)
666 }
667 })
668 }
669}
Akron7b4984e2025-05-26 19:12:20 +0200670
Akron958fc472026-05-19 13:58:52 +0200671func TestMultiFieldRewritesAreReversible(t *testing.T) {
672 mappingList := config.MappingList{
673 ID: "multi-field",
674 FoundryA: "opennlp",
675 LayerA: "p",
676 FoundryB: "upos",
677 LayerB: "pos",
678 Mappings: []config.MappingRule{
679 "[DET] <> [PRON]",
680 },
681 }
682
683 m, err := NewMapper([]config.MappingList{mappingList})
684 require.NoError(t, err)
685
686 tests := []struct {
687 name string
688 opts MappingOptions
689 input string
690 expected string
691 }{
692 {
693 name: "Multi-field change: foundry + layer + key all change",
694 opts: MappingOptions{
695 Direction: AtoB,
696 AddRewrites: true,
697 },
698 input: `{
699 "@type": "koral:token",
700 "wrap": {
701 "@type": "koral:term",
702 "foundry": "opennlp",
703 "key": "DET",
704 "layer": "p",
705 "match": "match:eq"
706 }
707 }`,
708 expected: `{
709 "@type": "koral:token",
710 "wrap": {
711 "@type": "koral:term",
712 "foundry": "upos",
713 "key": "PRON",
714 "layer": "pos",
715 "match": "match:eq",
716 "rewrites": [
717 {
718 "@type": "koral:rewrite",
719 "editor": "Koral-Mapper",
720 "scope": "foundry",
721 "original": "opennlp"
722 },
723 {
724 "@type": "koral:rewrite",
725 "editor": "Koral-Mapper",
726 "scope": "layer",
727 "original": "p"
728 },
729 {
730 "@type": "koral:rewrite",
731 "editor": "Koral-Mapper",
732 "scope": "key",
733 "original": "DET"
734 }
735 ]
736 }
737 }`,
738 },
739 {
740 name: "Reverse direction: foundry + layer + key all change back",
741 opts: MappingOptions{
742 Direction: BtoA,
743 AddRewrites: true,
744 },
745 input: `{
746 "@type": "koral:token",
747 "wrap": {
748 "@type": "koral:term",
749 "foundry": "upos",
750 "key": "PRON",
751 "layer": "pos",
752 "match": "match:eq"
753 }
754 }`,
755 expected: `{
756 "@type": "koral:token",
757 "wrap": {
758 "@type": "koral:term",
759 "foundry": "opennlp",
760 "key": "DET",
761 "layer": "p",
762 "match": "match:eq",
763 "rewrites": [
764 {
765 "@type": "koral:rewrite",
766 "editor": "Koral-Mapper",
767 "scope": "foundry",
768 "original": "upos"
769 },
770 {
771 "@type": "koral:rewrite",
772 "editor": "Koral-Mapper",
773 "scope": "layer",
774 "original": "pos"
775 },
776 {
777 "@type": "koral:rewrite",
778 "editor": "Koral-Mapper",
779 "scope": "key",
780 "original": "PRON"
781 }
782 ]
783 }
784 }`,
785 },
786 }
787
788 for _, tt := range tests {
789 t.Run(tt.name, func(t *testing.T) {
790 var inputData any
791 err := json.Unmarshal([]byte(tt.input), &inputData)
792 require.NoError(t, err)
793
794 result, err := m.ApplyQueryMappings("multi-field", tt.opts, inputData)
795 require.NoError(t, err)
796
797 var expectedData any
798 err = json.Unmarshal([]byte(tt.expected), &expectedData)
799 require.NoError(t, err)
800
801 assert.Equal(t, expectedData, result)
802 })
803 }
804}
805
806func TestSingleFieldRewrite(t *testing.T) {
807 mappingList := config.MappingList{
808 ID: "same-fl",
809 FoundryA: "opennlp",
810 LayerA: "p",
811 FoundryB: "opennlp",
812 LayerB: "p",
813 Mappings: []config.MappingRule{
814 "[DET] <> [PRON]",
815 },
816 }
817
818 m, err := NewMapper([]config.MappingList{mappingList})
819 require.NoError(t, err)
820
821 var inputData any
822 err = json.Unmarshal([]byte(`{
823 "@type": "koral:token",
824 "wrap": {
825 "@type": "koral:term",
826 "foundry": "opennlp",
827 "key": "DET",
828 "layer": "p",
829 "match": "match:eq"
830 }
831 }`), &inputData)
832 require.NoError(t, err)
833
834 result, err := m.ApplyQueryMappings("same-fl", MappingOptions{
835 Direction: AtoB,
836 AddRewrites: true,
837 }, inputData)
838 require.NoError(t, err)
839
840 var expectedData any
841 err = json.Unmarshal([]byte(`{
842 "@type": "koral:token",
843 "wrap": {
844 "@type": "koral:term",
845 "foundry": "opennlp",
846 "key": "PRON",
847 "layer": "p",
848 "match": "match:eq",
849 "rewrites": [
850 {
851 "@type": "koral:rewrite",
852 "editor": "Koral-Mapper",
853 "scope": "key",
854 "original": "DET"
855 }
856 ]
857 }
858 }`), &expectedData)
859 require.NoError(t, err)
860
861 assert.Equal(t, expectedData, result)
862}
863
864func TestBuildRewritesFieldInjection(t *testing.T) {
865 tests := []struct {
866 name string
867 original *ast.Term
868 new_ *ast.Term
869 expectedScopes []string
870 hasOriginals []bool
871 }{
872 {
873 name: "All fields change with originals",
874 original: &ast.Term{Foundry: "a", Layer: "l1", Key: "k1", Value: "v1", Match: ast.MatchEqual},
875 new_: &ast.Term{Foundry: "b", Layer: "l2", Key: "k2", Value: "v2", Match: ast.MatchEqual},
876 expectedScopes: []string{"foundry", "layer", "key", "value"},
877 hasOriginals: []bool{true, true, true, true},
878 },
879 {
880 name: "Injection: empty value becomes non-empty",
881 original: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Match: ast.MatchEqual},
882 new_: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Value: "v", Match: ast.MatchEqual},
883 expectedScopes: []string{"value"},
884 hasOriginals: []bool{false},
885 },
886 {
887 name: "Deletion: non-empty value becomes empty",
888 original: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Value: "v", Match: ast.MatchEqual},
889 new_: &ast.Term{Foundry: "a", Layer: "l", Key: "k", Match: ast.MatchEqual},
890 expectedScopes: []string{"value"},
891 hasOriginals: []bool{true},
892 },
893 }
894
895 for _, tt := range tests {
896 t.Run(tt.name, func(t *testing.T) {
897 rewrites := buildRewrites(tt.original, tt.new_)
898 require.Len(t, rewrites, len(tt.expectedScopes))
899 for i, rw := range rewrites {
900 assert.Equal(t, RewriteEditor, rw.Editor)
901 assert.Equal(t, tt.expectedScopes[i], rw.Scope)
902 if tt.hasOriginals[i] {
903 assert.NotNil(t, rw.Original, "expected original for scope %s", tt.expectedScopes[i])
904 } else {
905 assert.Nil(t, rw.Original, "expected no original for scope %s (injection)", tt.expectedScopes[i])
906 }
907 }
908 })
909 }
910}
911
Akron7b4984e2025-05-26 19:12:20 +0200912func TestQueryWrapperMappings(t *testing.T) {
913
914 mappingList := config.MappingList{
915 ID: "test-wrapper",
916 FoundryA: "opennlp",
917 LayerA: "orth",
918 FoundryB: "upos",
919 LayerB: "orth",
920 Mappings: []config.MappingRule{
921 "[opennlp/orth=Baum] <> [opennlp/orth=X]",
922 },
923 }
924
925 // Create a new mapper
926 m, err := NewMapper([]config.MappingList{mappingList})
927 require.NoError(t, err)
928
929 tests := []struct {
930 name string
931 mappingID string
932 opts MappingOptions
933 input string
934 expected string
935 expectError bool
936 }{
937 {
Akroncc83eb52025-05-27 14:39:12 +0200938 name: "Query wrapper case with rewrites preservation",
Akron7b4984e2025-05-26 19:12:20 +0200939 mappingID: "test-wrapper",
940 opts: MappingOptions{
941 Direction: AtoB,
942 },
943 input: `{
944 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
945 "collection": {
946 "@type": "koral:doc",
947 "key": "availability",
948 "match": "match:eq",
Akron7b4984e2025-05-26 19:12:20 +0200949 "type": "type:regex",
950 "value": "CC.*"
951 },
952 "query": {
953 "@type": "koral:token",
Akroncc83eb52025-05-27 14:39:12 +0200954 "rewrites": [
955 {
956 "@type": "koral:rewrite",
957 "_comment": "Original rewrite that should be preserved",
958 "editor": "Original",
959 "operation": "operation:original",
960 "src": "Original"
961 }
962 ],
Akron7b4984e2025-05-26 19:12:20 +0200963 "wrap": {
964 "@type": "koral:term",
965 "foundry": "opennlp",
966 "key": "Baum",
967 "layer": "orth",
968 "match": "match:eq"
969 }
970 }
971 }`,
972 expected: `{
973 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
974 "collection": {
975 "@type": "koral:doc",
976 "key": "availability",
977 "match": "match:eq",
Akron7b4984e2025-05-26 19:12:20 +0200978 "type": "type:regex",
979 "value": "CC.*"
980 },
981 "query": {
982 "@type": "koral:token",
Akroncc83eb52025-05-27 14:39:12 +0200983 "rewrites": [
984 {
985 "@type": "koral:rewrite",
986 "_comment": "Original rewrite that should be preserved",
987 "editor": "Original",
988 "operation": "operation:original",
989 "src": "Original"
990 }
991 ],
Akron7b4984e2025-05-26 19:12:20 +0200992 "wrap": {
993 "@type": "koral:term",
994 "foundry": "opennlp",
995 "key": "X",
996 "layer": "orth",
997 "match": "match:eq"
998 }
999 }
1000 }`,
1001 },
1002 {
1003 name: "Empty query field",
1004 mappingID: "test-wrapper",
1005 opts: MappingOptions{
1006 Direction: AtoB,
1007 },
1008 input: `{
1009 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1010 "query": null
1011 }`,
1012 expected: `{
1013 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1014 "query": null
1015 }`,
1016 },
1017 {
1018 name: "Missing query field",
1019 mappingID: "test-wrapper",
1020 opts: MappingOptions{
1021 Direction: AtoB,
1022 },
1023 input: `{
1024 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1025 "collection": {
1026 "@type": "koral:doc"
1027 }
1028 }`,
1029 expected: `{
1030 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1031 "collection": {
1032 "@type": "koral:doc"
1033 }
1034 }`,
1035 },
1036 {
1037 name: "Query field with non-object value",
1038 mappingID: "test-wrapper",
1039 opts: MappingOptions{
1040 Direction: AtoB,
1041 },
1042 input: `{
1043 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1044 "query": "invalid"
1045 }`,
1046 expected: `{
1047 "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
1048 "query": "invalid"
1049 }`,
1050 },
Akroncc83eb52025-05-27 14:39:12 +02001051 {
1052 name: "Query with rewrites in nested token",
1053 mappingID: "test-wrapper",
1054 opts: MappingOptions{
1055 Direction: AtoB,
1056 },
1057 input: `{
1058 "@type": "koral:token",
1059 "rewrites": [
1060 {
1061 "@type": "koral:rewrite",
1062 "_comment": "Nested rewrite that should be preserved",
1063 "editor": "Nested",
1064 "operation": "operation:nested",
1065 "src": "Nested"
1066 }
1067 ],
1068 "wrap": {
1069 "@type": "koral:term",
1070 "foundry": "opennlp",
1071 "key": "Baum",
1072 "layer": "orth",
1073 "match": "match:eq"
1074 }
1075 }`,
1076 expected: `{
1077 "@type": "koral:token",
1078 "rewrites": [
1079 {
1080 "@type": "koral:rewrite",
1081 "_comment": "Nested rewrite that should be preserved",
1082 "editor": "Nested",
1083 "operation": "operation:nested",
1084 "src": "Nested"
1085 }
1086 ],
1087 "wrap": {
1088 "@type": "koral:term",
1089 "foundry": "opennlp",
1090 "key": "X",
1091 "layer": "orth",
1092 "match": "match:eq"
1093 }
1094 }`,
1095 },
Akron7b4984e2025-05-26 19:12:20 +02001096 }
1097
1098 for _, tt := range tests {
1099 t.Run(tt.name, func(t *testing.T) {
1100 // Parse input JSON
Akron121c66e2025-06-02 16:34:05 +02001101 var inputData any
Akron7b4984e2025-05-26 19:12:20 +02001102 err := json.Unmarshal([]byte(tt.input), &inputData)
1103 require.NoError(t, err)
1104
1105 // Apply mappings
1106 result, err := m.ApplyQueryMappings(tt.mappingID, tt.opts, inputData)
1107 if tt.expectError {
1108 assert.Error(t, err)
1109 return
1110 }
1111 require.NoError(t, err)
1112
1113 // Parse expected JSON
Akron121c66e2025-06-02 16:34:05 +02001114 var expectedData any
Akron7b4984e2025-05-26 19:12:20 +02001115 err = json.Unmarshal([]byte(tt.expected), &expectedData)
1116 require.NoError(t, err)
1117
1118 // Compare results
1119 assert.Equal(t, expectedData, result)
1120 })
1121 }
1122}