Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 1 | package mapper |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 5 | "testing" |
| 6 | |
Akron | fa55bb2 | 2025-05-26 15:10:42 +0200 | [diff] [blame] | 7 | "github.com/KorAP/KoralPipe-TermMapper/ast" |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 8 | "github.com/KorAP/KoralPipe-TermMapper/config" |
Akron | fa55bb2 | 2025-05-26 15:10:42 +0200 | [diff] [blame] | 9 | "github.com/KorAP/KoralPipe-TermMapper/matcher" |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 10 | "github.com/stretchr/testify/assert" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | ) |
| 13 | |
| 14 | func TestMapper(t *testing.T) { |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 15 | // 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 | } |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 27 | |
| 28 | // Create a new mapper |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 29 | m, err := NewMapper([]config.MappingList{mappingList}) |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 30 | 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 | { |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 82 | name: "Simple A to B mapping with rewrites", |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 83 | mappingID: "test-mapper", |
| 84 | opts: MappingOptions{ |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 85 | Direction: AtoB, |
| 86 | AddRewrites: true, |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 87 | }, |
| 88 | input: `{ |
| 89 | "@type": "koral:token", |
| 90 | "wrap": { |
Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 91 | "@type": "koral:term", |
| 92 | "foundry": "opennlp", |
| 93 | "key": "PIDAT", |
| 94 | "layer": "p", |
| 95 | "match": "match:eq" |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 96 | } |
| 97 | }`, |
| 98 | expected: `{ |
| 99 | "@type": "koral:token", |
| 100 | "wrap": { |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 101 | "@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", |
| 123 | "editor": "termMapper", |
Akron | 8a87d9a | 2025-05-27 15:30:48 +0200 | [diff] [blame] | 124 | "original": { |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 125 | "@type": "koral:term", |
| 126 | "foundry": "opennlp", |
| 127 | "key": "PIDAT", |
| 128 | "layer": "p", |
| 129 | "match": "match:eq" |
| 130 | } |
| 131 | } |
| 132 | ] |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 133 | } |
| 134 | }`, |
| 135 | }, |
| 136 | { |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 137 | name: "Mapping with foundry override and rewrites", |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 138 | mappingID: "test-mapper", |
| 139 | opts: MappingOptions{ |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 140 | Direction: AtoB, |
| 141 | FoundryB: "custom", |
| 142 | AddRewrites: true, |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 143 | }, |
| 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 | ], |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 175 | "relation": "relation:and", |
| 176 | "rewrites": [ |
| 177 | { |
| 178 | "@type": "koral:rewrite", |
| 179 | "editor": "termMapper", |
Akron | 8a87d9a | 2025-05-27 15:30:48 +0200 | [diff] [blame] | 180 | "original": { |
| 181 | "@type": "koral:term", |
| 182 | "foundry": "opennlp", |
| 183 | "key": "PIDAT", |
| 184 | "layer": "p", |
| 185 | "match": "match:eq" |
| 186 | } |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 187 | } |
| 188 | ] |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 189 | } |
| 190 | }`, |
| 191 | }, |
| 192 | { |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 193 | 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 | { |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 221 | 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{ |
Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 242 | Direction: Direction(false), |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 243 | }, |
| 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 | }`, |
Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 254 | 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, |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 265 | }, |
Akron | 8f1970f | 2025-05-30 12:52:03 +0200 | [diff] [blame^] | 266 | { |
| 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 | }, |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | for _, tt := range tests { |
| 412 | t.Run(tt.name, func(t *testing.T) { |
| 413 | // Parse input JSON |
| 414 | var inputData interface{} |
| 415 | err := json.Unmarshal([]byte(tt.input), &inputData) |
| 416 | require.NoError(t, err) |
| 417 | |
| 418 | // Apply mappings |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 419 | result, err := m.ApplyQueryMappings(tt.mappingID, tt.opts, inputData) |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 420 | if tt.expectError { |
| 421 | assert.Error(t, err) |
| 422 | return |
| 423 | } |
| 424 | require.NoError(t, err) |
| 425 | |
| 426 | // Parse expected JSON |
| 427 | var expectedData interface{} |
| 428 | 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 | } |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 436 | |
Akron | cc83eb5 | 2025-05-27 14:39:12 +0200 | [diff] [blame] | 437 | func 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 |
| 509 | var inputData interface{} |
| 510 | 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 |
| 518 | var expectedData interface{} |
| 519 | err = json.Unmarshal([]byte(expected), &expectedData) |
| 520 | require.NoError(t, err) |
| 521 | |
| 522 | // Compare results |
| 523 | assert.Equal(t, expectedData, result) |
| 524 | } |
| 525 | |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 526 | func 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 | |
| 614 | func TestInvalidPatternReplacement(t *testing.T) { |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 615 | // 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 | } |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 626 | |
| 627 | // Create a new mapper |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 628 | m, err := NewMapper([]config.MappingList{mappingList}) |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 629 | 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 | |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 658 | result, err := m.ApplyQueryMappings("test-mapper", MappingOptions{Direction: AtoB}, inputData) |
Akron | d5850f8 | 2025-05-23 16:44:44 +0200 | [diff] [blame] | 659 | 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 | } |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 670 | |
| 671 | func TestQueryWrapperMappings(t *testing.T) { |
| 672 | |
| 673 | mappingList := config.MappingList{ |
| 674 | ID: "test-wrapper", |
| 675 | FoundryA: "opennlp", |
| 676 | LayerA: "orth", |
| 677 | FoundryB: "upos", |
| 678 | LayerB: "orth", |
| 679 | Mappings: []config.MappingRule{ |
| 680 | "[opennlp/orth=Baum] <> [opennlp/orth=X]", |
| 681 | }, |
| 682 | } |
| 683 | |
| 684 | // Create a new mapper |
| 685 | m, err := NewMapper([]config.MappingList{mappingList}) |
| 686 | require.NoError(t, err) |
| 687 | |
| 688 | tests := []struct { |
| 689 | name string |
| 690 | mappingID string |
| 691 | opts MappingOptions |
| 692 | input string |
| 693 | expected string |
| 694 | expectError bool |
| 695 | }{ |
| 696 | { |
Akron | cc83eb5 | 2025-05-27 14:39:12 +0200 | [diff] [blame] | 697 | name: "Query wrapper case with rewrites preservation", |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 698 | mappingID: "test-wrapper", |
| 699 | opts: MappingOptions{ |
| 700 | Direction: AtoB, |
| 701 | }, |
| 702 | input: `{ |
| 703 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 704 | "collection": { |
| 705 | "@type": "koral:doc", |
| 706 | "key": "availability", |
| 707 | "match": "match:eq", |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 708 | "type": "type:regex", |
| 709 | "value": "CC.*" |
| 710 | }, |
| 711 | "query": { |
| 712 | "@type": "koral:token", |
Akron | cc83eb5 | 2025-05-27 14:39:12 +0200 | [diff] [blame] | 713 | "rewrites": [ |
| 714 | { |
| 715 | "@type": "koral:rewrite", |
| 716 | "_comment": "Original rewrite that should be preserved", |
| 717 | "editor": "Original", |
| 718 | "operation": "operation:original", |
| 719 | "src": "Original" |
| 720 | } |
| 721 | ], |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 722 | "wrap": { |
| 723 | "@type": "koral:term", |
| 724 | "foundry": "opennlp", |
| 725 | "key": "Baum", |
| 726 | "layer": "orth", |
| 727 | "match": "match:eq" |
| 728 | } |
| 729 | } |
| 730 | }`, |
| 731 | expected: `{ |
| 732 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 733 | "collection": { |
| 734 | "@type": "koral:doc", |
| 735 | "key": "availability", |
| 736 | "match": "match:eq", |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 737 | "type": "type:regex", |
| 738 | "value": "CC.*" |
| 739 | }, |
| 740 | "query": { |
| 741 | "@type": "koral:token", |
Akron | cc83eb5 | 2025-05-27 14:39:12 +0200 | [diff] [blame] | 742 | "rewrites": [ |
| 743 | { |
| 744 | "@type": "koral:rewrite", |
| 745 | "_comment": "Original rewrite that should be preserved", |
| 746 | "editor": "Original", |
| 747 | "operation": "operation:original", |
| 748 | "src": "Original" |
| 749 | } |
| 750 | ], |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 751 | "wrap": { |
| 752 | "@type": "koral:term", |
| 753 | "foundry": "opennlp", |
| 754 | "key": "X", |
| 755 | "layer": "orth", |
| 756 | "match": "match:eq" |
| 757 | } |
| 758 | } |
| 759 | }`, |
| 760 | }, |
| 761 | { |
| 762 | name: "Empty query field", |
| 763 | mappingID: "test-wrapper", |
| 764 | opts: MappingOptions{ |
| 765 | Direction: AtoB, |
| 766 | }, |
| 767 | input: `{ |
| 768 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 769 | "query": null |
| 770 | }`, |
| 771 | expected: `{ |
| 772 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 773 | "query": null |
| 774 | }`, |
| 775 | }, |
| 776 | { |
| 777 | name: "Missing query field", |
| 778 | mappingID: "test-wrapper", |
| 779 | opts: MappingOptions{ |
| 780 | Direction: AtoB, |
| 781 | }, |
| 782 | input: `{ |
| 783 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 784 | "collection": { |
| 785 | "@type": "koral:doc" |
| 786 | } |
| 787 | }`, |
| 788 | expected: `{ |
| 789 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 790 | "collection": { |
| 791 | "@type": "koral:doc" |
| 792 | } |
| 793 | }`, |
| 794 | }, |
| 795 | { |
| 796 | name: "Query field with non-object value", |
| 797 | mappingID: "test-wrapper", |
| 798 | opts: MappingOptions{ |
| 799 | Direction: AtoB, |
| 800 | }, |
| 801 | input: `{ |
| 802 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 803 | "query": "invalid" |
| 804 | }`, |
| 805 | expected: `{ |
| 806 | "@context": "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld", |
| 807 | "query": "invalid" |
| 808 | }`, |
| 809 | }, |
Akron | cc83eb5 | 2025-05-27 14:39:12 +0200 | [diff] [blame] | 810 | { |
| 811 | name: "Query with rewrites in nested token", |
| 812 | mappingID: "test-wrapper", |
| 813 | opts: MappingOptions{ |
| 814 | Direction: AtoB, |
| 815 | }, |
| 816 | input: `{ |
| 817 | "@type": "koral:token", |
| 818 | "rewrites": [ |
| 819 | { |
| 820 | "@type": "koral:rewrite", |
| 821 | "_comment": "Nested rewrite that should be preserved", |
| 822 | "editor": "Nested", |
| 823 | "operation": "operation:nested", |
| 824 | "src": "Nested" |
| 825 | } |
| 826 | ], |
| 827 | "wrap": { |
| 828 | "@type": "koral:term", |
| 829 | "foundry": "opennlp", |
| 830 | "key": "Baum", |
| 831 | "layer": "orth", |
| 832 | "match": "match:eq" |
| 833 | } |
| 834 | }`, |
| 835 | expected: `{ |
| 836 | "@type": "koral:token", |
| 837 | "rewrites": [ |
| 838 | { |
| 839 | "@type": "koral:rewrite", |
| 840 | "_comment": "Nested rewrite that should be preserved", |
| 841 | "editor": "Nested", |
| 842 | "operation": "operation:nested", |
| 843 | "src": "Nested" |
| 844 | } |
| 845 | ], |
| 846 | "wrap": { |
| 847 | "@type": "koral:term", |
| 848 | "foundry": "opennlp", |
| 849 | "key": "X", |
| 850 | "layer": "orth", |
| 851 | "match": "match:eq" |
| 852 | } |
| 853 | }`, |
| 854 | }, |
Akron | 7b4984e | 2025-05-26 19:12:20 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | for _, tt := range tests { |
| 858 | t.Run(tt.name, func(t *testing.T) { |
| 859 | // Parse input JSON |
| 860 | var inputData interface{} |
| 861 | err := json.Unmarshal([]byte(tt.input), &inputData) |
| 862 | require.NoError(t, err) |
| 863 | |
| 864 | // Apply mappings |
| 865 | result, err := m.ApplyQueryMappings(tt.mappingID, tt.opts, inputData) |
| 866 | if tt.expectError { |
| 867 | assert.Error(t, err) |
| 868 | return |
| 869 | } |
| 870 | require.NoError(t, err) |
| 871 | |
| 872 | // Parse expected JSON |
| 873 | var expectedData interface{} |
| 874 | err = json.Unmarshal([]byte(tt.expected), &expectedData) |
| 875 | require.NoError(t, err) |
| 876 | |
| 877 | // Compare results |
| 878 | assert.Equal(t, expectedData, result) |
| 879 | }) |
| 880 | } |
| 881 | } |