github.com/weaviate/weaviate@v1.24.6/usecases/modulecomponents/arguments/nearText/graphql_extract_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package nearText 13 14 import ( 15 "reflect" 16 "testing" 17 ) 18 19 func Test_extractNearTextFn(t *testing.T) { 20 type args struct { 21 source map[string]interface{} 22 } 23 tests := []struct { 24 name string 25 args args 26 want *NearTextParams 27 }{ 28 { 29 "Extract with concepts", 30 args{ 31 source: map[string]interface{}{ 32 "concepts": []interface{}{"c1", "c2", "c3"}, 33 }, 34 }, 35 &NearTextParams{ 36 Values: []string{"c1", "c2", "c3"}, 37 }, 38 }, 39 { 40 "Extract with concepts, distance, limit and network", 41 args{ 42 source: map[string]interface{}{ 43 "concepts": []interface{}{"c1", "c2", "c3"}, 44 "distance": float64(0.4), 45 "limit": 100, 46 "network": true, 47 }, 48 }, 49 &NearTextParams{ 50 Values: []string{"c1", "c2", "c3"}, 51 Distance: 0.4, 52 WithDistance: true, 53 Limit: 100, 54 Network: true, 55 }, 56 }, 57 { 58 "Extract with concepts, certainty, limit and network", 59 args{ 60 source: map[string]interface{}{ 61 "concepts": []interface{}{"c1", "c2", "c3"}, 62 "certainty": float64(0.4), 63 "limit": 100, 64 "network": true, 65 }, 66 }, 67 &NearTextParams{ 68 Values: []string{"c1", "c2", "c3"}, 69 Certainty: 0.4, 70 Limit: 100, 71 Network: true, 72 }, 73 }, 74 { 75 "Extract with moveTo, moveAwayFrom, and distance", 76 args{ 77 source: map[string]interface{}{ 78 "concepts": []interface{}{"c1", "c2", "c3"}, 79 "distance": float64(0.89), 80 "limit": 500, 81 "network": false, 82 "moveTo": map[string]interface{}{ 83 "concepts": []interface{}{"positive"}, 84 "force": float64(0.5), 85 }, 86 "moveAwayFrom": map[string]interface{}{ 87 "concepts": []interface{}{"epic"}, 88 "force": float64(0.25), 89 }, 90 }, 91 }, 92 &NearTextParams{ 93 Values: []string{"c1", "c2", "c3"}, 94 Distance: 0.89, 95 WithDistance: true, 96 Limit: 500, 97 Network: false, 98 MoveTo: ExploreMove{ 99 Values: []string{"positive"}, 100 Force: 0.5, 101 }, 102 MoveAwayFrom: ExploreMove{ 103 Values: []string{"epic"}, 104 Force: 0.25, 105 }, 106 }, 107 }, 108 { 109 "Extract with moveTo, moveAwayFrom, and certainty", 110 args{ 111 source: map[string]interface{}{ 112 "concepts": []interface{}{"c1", "c2", "c3"}, 113 "certainty": float64(0.89), 114 "limit": 500, 115 "network": false, 116 "moveTo": map[string]interface{}{ 117 "concepts": []interface{}{"positive"}, 118 "force": float64(0.5), 119 }, 120 "moveAwayFrom": map[string]interface{}{ 121 "concepts": []interface{}{"epic"}, 122 "force": float64(0.25), 123 }, 124 }, 125 }, 126 &NearTextParams{ 127 Values: []string{"c1", "c2", "c3"}, 128 Certainty: 0.89, 129 Limit: 500, 130 Network: false, 131 MoveTo: ExploreMove{ 132 Values: []string{"positive"}, 133 Force: 0.5, 134 }, 135 MoveAwayFrom: ExploreMove{ 136 Values: []string{"epic"}, 137 Force: 0.25, 138 }, 139 }, 140 }, 141 { 142 "Extract with moveTo, moveAwayFrom, distance (and objects)", 143 args{ 144 source: map[string]interface{}{ 145 "concepts": []interface{}{"c1", "c2", "c3"}, 146 "distance": float64(0.89), 147 "limit": 500, 148 "network": false, 149 "moveTo": map[string]interface{}{ 150 "concepts": []interface{}{"positive"}, 151 "force": float64(0.5), 152 "objects": []interface{}{ 153 map[string]interface{}{ 154 "id": "moveTo-uuid1", 155 }, 156 map[string]interface{}{ 157 "beacon": "weaviate://localhost/moveTo-uuid2", 158 }, 159 map[string]interface{}{ 160 "beacon": "weaviate://localhost/moveTo-uuid3", 161 }, 162 }, 163 }, 164 "moveAwayFrom": map[string]interface{}{ 165 "concepts": []interface{}{"epic"}, 166 "force": float64(0.25), 167 "objects": []interface{}{ 168 map[string]interface{}{ 169 "id": "moveAwayFrom-uuid1", 170 }, 171 map[string]interface{}{ 172 "id": "moveAwayFrom-uuid2", 173 }, 174 map[string]interface{}{ 175 "beacon": "weaviate://localhost/moveAwayFrom-uuid3", 176 }, 177 map[string]interface{}{ 178 "beacon": "weaviate://localhost/moveAwayFrom-uuid4", 179 }, 180 }, 181 }, 182 }, 183 }, 184 &NearTextParams{ 185 Values: []string{"c1", "c2", "c3"}, 186 Distance: 0.89, 187 WithDistance: true, 188 Limit: 500, 189 Network: false, 190 MoveTo: ExploreMove{ 191 Values: []string{"positive"}, 192 Force: 0.5, 193 Objects: []ObjectMove{ 194 {ID: "moveTo-uuid1"}, 195 {Beacon: "weaviate://localhost/moveTo-uuid2"}, 196 {Beacon: "weaviate://localhost/moveTo-uuid3"}, 197 }, 198 }, 199 MoveAwayFrom: ExploreMove{ 200 Values: []string{"epic"}, 201 Force: 0.25, 202 Objects: []ObjectMove{ 203 {ID: "moveAwayFrom-uuid1"}, 204 {ID: "moveAwayFrom-uuid2"}, 205 {Beacon: "weaviate://localhost/moveAwayFrom-uuid3"}, 206 {Beacon: "weaviate://localhost/moveAwayFrom-uuid4"}, 207 }, 208 }, 209 }, 210 }, 211 { 212 "Extract with moveTo, moveAwayFrom, certainty (and objects)", 213 args{ 214 source: map[string]interface{}{ 215 "concepts": []interface{}{"c1", "c2", "c3"}, 216 "certainty": float64(0.89), 217 "limit": 500, 218 "network": false, 219 "moveTo": map[string]interface{}{ 220 "concepts": []interface{}{"positive"}, 221 "force": float64(0.5), 222 "objects": []interface{}{ 223 map[string]interface{}{ 224 "id": "moveTo-uuid1", 225 }, 226 map[string]interface{}{ 227 "beacon": "weaviate://localhost/moveTo-uuid2", 228 }, 229 map[string]interface{}{ 230 "beacon": "weaviate://localhost/moveTo-uuid3", 231 }, 232 }, 233 }, 234 "moveAwayFrom": map[string]interface{}{ 235 "concepts": []interface{}{"epic"}, 236 "force": float64(0.25), 237 "objects": []interface{}{ 238 map[string]interface{}{ 239 "id": "moveAwayFrom-uuid1", 240 }, 241 map[string]interface{}{ 242 "id": "moveAwayFrom-uuid2", 243 }, 244 map[string]interface{}{ 245 "beacon": "weaviate://localhost/moveAwayFrom-uuid3", 246 }, 247 map[string]interface{}{ 248 "beacon": "weaviate://localhost/moveAwayFrom-uuid4", 249 }, 250 }, 251 }, 252 }, 253 }, 254 &NearTextParams{ 255 Values: []string{"c1", "c2", "c3"}, 256 Certainty: 0.89, 257 Limit: 500, 258 Network: false, 259 MoveTo: ExploreMove{ 260 Values: []string{"positive"}, 261 Force: 0.5, 262 Objects: []ObjectMove{ 263 {ID: "moveTo-uuid1"}, 264 {Beacon: "weaviate://localhost/moveTo-uuid2"}, 265 {Beacon: "weaviate://localhost/moveTo-uuid3"}, 266 }, 267 }, 268 MoveAwayFrom: ExploreMove{ 269 Values: []string{"epic"}, 270 Force: 0.25, 271 Objects: []ObjectMove{ 272 {ID: "moveAwayFrom-uuid1"}, 273 {ID: "moveAwayFrom-uuid2"}, 274 {Beacon: "weaviate://localhost/moveAwayFrom-uuid3"}, 275 {Beacon: "weaviate://localhost/moveAwayFrom-uuid4"}, 276 }, 277 }, 278 }, 279 }, 280 { 281 "Extract with moveTo, moveAwayFrom, distance (and doubled objects)", 282 args{ 283 source: map[string]interface{}{ 284 "concepts": []interface{}{"c1", "c2", "c3"}, 285 "distance": float64(0.89), 286 "limit": 500, 287 "network": false, 288 "moveTo": map[string]interface{}{ 289 "concepts": []interface{}{"positive"}, 290 "force": float64(0.5), 291 "objects": []interface{}{ 292 map[string]interface{}{ 293 "id": "moveTo-uuid1", 294 "beacon": "weaviate://localhost/moveTo-uuid2", 295 }, 296 map[string]interface{}{ 297 "id": "moveTo-uuid1", 298 "beacon": "weaviate://localhost/moveTo-uuid2", 299 }, 300 }, 301 }, 302 "moveAwayFrom": map[string]interface{}{ 303 "concepts": []interface{}{"epic"}, 304 "force": float64(0.25), 305 "objects": []interface{}{ 306 map[string]interface{}{ 307 "id": "moveAwayFrom-uuid1", 308 "beacon": "weaviate://localhost/moveAwayFrom-uuid1", 309 }, 310 map[string]interface{}{ 311 "id": "moveAwayFrom-uuid2", 312 "beacon": "weaviate://localhost/moveAwayFrom-uuid2", 313 }, 314 map[string]interface{}{ 315 "beacon": "weaviate://localhost/moveAwayFrom-uuid3", 316 }, 317 map[string]interface{}{ 318 "beacon": "weaviate://localhost/moveAwayFrom-uuid4", 319 }, 320 }, 321 }, 322 }, 323 }, 324 &NearTextParams{ 325 Values: []string{"c1", "c2", "c3"}, 326 Distance: 0.89, 327 WithDistance: true, 328 Limit: 500, 329 Network: false, 330 MoveTo: ExploreMove{ 331 Values: []string{"positive"}, 332 Force: 0.5, 333 Objects: []ObjectMove{ 334 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 335 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 336 }, 337 }, 338 MoveAwayFrom: ExploreMove{ 339 Values: []string{"epic"}, 340 Force: 0.25, 341 Objects: []ObjectMove{ 342 {ID: "moveAwayFrom-uuid1", Beacon: "weaviate://localhost/moveAwayFrom-uuid1"}, 343 {ID: "moveAwayFrom-uuid2", Beacon: "weaviate://localhost/moveAwayFrom-uuid2"}, 344 {Beacon: "weaviate://localhost/moveAwayFrom-uuid3"}, 345 {Beacon: "weaviate://localhost/moveAwayFrom-uuid4"}, 346 }, 347 }, 348 }, 349 }, 350 { 351 "Extract with moveTo, moveAwayFrom, certainty (and doubled objects)", 352 args{ 353 source: map[string]interface{}{ 354 "concepts": []interface{}{"c1", "c2", "c3"}, 355 "certainty": float64(0.89), 356 "limit": 500, 357 "network": false, 358 "moveTo": map[string]interface{}{ 359 "concepts": []interface{}{"positive"}, 360 "force": float64(0.5), 361 "objects": []interface{}{ 362 map[string]interface{}{ 363 "id": "moveTo-uuid1", 364 "beacon": "weaviate://localhost/moveTo-uuid2", 365 }, 366 map[string]interface{}{ 367 "id": "moveTo-uuid1", 368 "beacon": "weaviate://localhost/moveTo-uuid2", 369 }, 370 }, 371 }, 372 "moveAwayFrom": map[string]interface{}{ 373 "concepts": []interface{}{"epic"}, 374 "force": float64(0.25), 375 "objects": []interface{}{ 376 map[string]interface{}{ 377 "id": "moveAwayFrom-uuid1", 378 "beacon": "weaviate://localhost/moveAwayFrom-uuid1", 379 }, 380 map[string]interface{}{ 381 "id": "moveAwayFrom-uuid2", 382 "beacon": "weaviate://localhost/moveAwayFrom-uuid2", 383 }, 384 map[string]interface{}{ 385 "beacon": "weaviate://localhost/moveAwayFrom-uuid3", 386 }, 387 map[string]interface{}{ 388 "beacon": "weaviate://localhost/moveAwayFrom-uuid4", 389 }, 390 }, 391 }, 392 }, 393 }, 394 &NearTextParams{ 395 Values: []string{"c1", "c2", "c3"}, 396 Certainty: 0.89, 397 Limit: 500, 398 Network: false, 399 MoveTo: ExploreMove{ 400 Values: []string{"positive"}, 401 Force: 0.5, 402 Objects: []ObjectMove{ 403 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 404 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 405 }, 406 }, 407 MoveAwayFrom: ExploreMove{ 408 Values: []string{"epic"}, 409 Force: 0.25, 410 Objects: []ObjectMove{ 411 {ID: "moveAwayFrom-uuid1", Beacon: "weaviate://localhost/moveAwayFrom-uuid1"}, 412 {ID: "moveAwayFrom-uuid2", Beacon: "weaviate://localhost/moveAwayFrom-uuid2"}, 413 {Beacon: "weaviate://localhost/moveAwayFrom-uuid3"}, 414 {Beacon: "weaviate://localhost/moveAwayFrom-uuid4"}, 415 }, 416 }, 417 }, 418 }, 419 { 420 "Extract with concepts and targetVectors", 421 args{ 422 source: map[string]interface{}{ 423 "concepts": []interface{}{"c1", "c2", "c3"}, 424 "targetVectors": []interface{}{"targetVector"}, 425 }, 426 }, 427 &NearTextParams{ 428 Values: []string{"c1", "c2", "c3"}, 429 TargetVectors: []string{"targetVector"}, 430 }, 431 }, 432 } 433 434 testsWithAutocorrect := []struct { 435 name string 436 args args 437 want *NearTextParams 438 }{ 439 { 440 "Extract with concepts", 441 args{ 442 source: map[string]interface{}{ 443 "concepts": []interface{}{"c1", "c2", "c3"}, 444 "autocorrect": true, 445 }, 446 }, 447 &NearTextParams{ 448 Values: []string{"c1", "c2", "c3"}, 449 Autocorrect: true, 450 }, 451 }, 452 { 453 "Extract with concepts and perform autocorrect", 454 args{ 455 source: map[string]interface{}{ 456 "concepts": []interface{}{"transform this", "c2", "transform this"}, 457 "autocorrect": true, 458 }, 459 }, 460 &NearTextParams{ 461 Values: []string{"transformed text", "c2", "transformed text"}, 462 Autocorrect: true, 463 }, 464 }, 465 { 466 "Extract with moveTo, moveAwayFrom, distance (and doubled objects) and autocorrect", 467 args{ 468 source: map[string]interface{}{ 469 "concepts": []interface{}{"transform this", "c1", "c2", "c3", "transform this"}, 470 "distance": float64(0.89), 471 "limit": 500, 472 "network": false, 473 "autocorrect": true, 474 "moveTo": map[string]interface{}{ 475 "concepts": []interface{}{"positive"}, 476 "force": float64(0.5), 477 "objects": []interface{}{ 478 map[string]interface{}{ 479 "id": "moveTo-uuid1", 480 "beacon": "weaviate://localhost/moveTo-uuid2", 481 }, 482 map[string]interface{}{ 483 "id": "moveTo-uuid1", 484 "beacon": "weaviate://localhost/moveTo-uuid2", 485 }, 486 }, 487 }, 488 "moveAwayFrom": map[string]interface{}{ 489 "concepts": []interface{}{"epic"}, 490 "force": float64(0.25), 491 "objects": []interface{}{ 492 map[string]interface{}{ 493 "id": "moveAwayFrom-uuid1", 494 "beacon": "weaviate://localhost/moveAwayFrom-uuid1", 495 }, 496 map[string]interface{}{ 497 "id": "moveAwayFrom-uuid2", 498 "beacon": "weaviate://localhost/moveAwayFrom-uuid2", 499 }, 500 map[string]interface{}{ 501 "beacon": "weaviate://localhost/moveAwayFrom-uuid3", 502 }, 503 map[string]interface{}{ 504 "beacon": "weaviate://localhost/moveAwayFrom-uuid4", 505 }, 506 }, 507 }, 508 }, 509 }, 510 &NearTextParams{ 511 Values: []string{"transformed text", "c1", "c2", "c3", "transformed text"}, 512 Distance: 0.89, 513 WithDistance: true, 514 Limit: 500, 515 Network: false, 516 Autocorrect: true, 517 MoveTo: ExploreMove{ 518 Values: []string{"positive"}, 519 Force: 0.5, 520 Objects: []ObjectMove{ 521 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 522 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 523 }, 524 }, 525 MoveAwayFrom: ExploreMove{ 526 Values: []string{"epic"}, 527 Force: 0.25, 528 Objects: []ObjectMove{ 529 {ID: "moveAwayFrom-uuid1", Beacon: "weaviate://localhost/moveAwayFrom-uuid1"}, 530 {ID: "moveAwayFrom-uuid2", Beacon: "weaviate://localhost/moveAwayFrom-uuid2"}, 531 {Beacon: "weaviate://localhost/moveAwayFrom-uuid3"}, 532 {Beacon: "weaviate://localhost/moveAwayFrom-uuid4"}, 533 }, 534 }, 535 }, 536 }, 537 { 538 "Extract with moveTo, moveAwayFrom, certainty (and doubled objects) and autocorrect", 539 args{ 540 source: map[string]interface{}{ 541 "concepts": []interface{}{"transform this", "c1", "c2", "c3", "transform this"}, 542 "certainty": float64(0.89), 543 "limit": 500, 544 "network": false, 545 "autocorrect": true, 546 "moveTo": map[string]interface{}{ 547 "concepts": []interface{}{"positive"}, 548 "force": float64(0.5), 549 "objects": []interface{}{ 550 map[string]interface{}{ 551 "id": "moveTo-uuid1", 552 "beacon": "weaviate://localhost/moveTo-uuid2", 553 }, 554 map[string]interface{}{ 555 "id": "moveTo-uuid1", 556 "beacon": "weaviate://localhost/moveTo-uuid2", 557 }, 558 }, 559 }, 560 "moveAwayFrom": map[string]interface{}{ 561 "concepts": []interface{}{"epic"}, 562 "force": float64(0.25), 563 "objects": []interface{}{ 564 map[string]interface{}{ 565 "id": "moveAwayFrom-uuid1", 566 "beacon": "weaviate://localhost/moveAwayFrom-uuid1", 567 }, 568 map[string]interface{}{ 569 "id": "moveAwayFrom-uuid2", 570 "beacon": "weaviate://localhost/moveAwayFrom-uuid2", 571 }, 572 map[string]interface{}{ 573 "beacon": "weaviate://localhost/moveAwayFrom-uuid3", 574 }, 575 map[string]interface{}{ 576 "beacon": "weaviate://localhost/moveAwayFrom-uuid4", 577 }, 578 }, 579 }, 580 }, 581 }, 582 &NearTextParams{ 583 Values: []string{"transformed text", "c1", "c2", "c3", "transformed text"}, 584 Certainty: 0.89, 585 Limit: 500, 586 Network: false, 587 Autocorrect: true, 588 MoveTo: ExploreMove{ 589 Values: []string{"positive"}, 590 Force: 0.5, 591 Objects: []ObjectMove{ 592 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 593 {ID: "moveTo-uuid1", Beacon: "weaviate://localhost/moveTo-uuid2"}, 594 }, 595 }, 596 MoveAwayFrom: ExploreMove{ 597 Values: []string{"epic"}, 598 Force: 0.25, 599 Objects: []ObjectMove{ 600 {ID: "moveAwayFrom-uuid1", Beacon: "weaviate://localhost/moveAwayFrom-uuid1"}, 601 {ID: "moveAwayFrom-uuid2", Beacon: "weaviate://localhost/moveAwayFrom-uuid2"}, 602 {Beacon: "weaviate://localhost/moveAwayFrom-uuid3"}, 603 {Beacon: "weaviate://localhost/moveAwayFrom-uuid4"}, 604 }, 605 }, 606 }, 607 }, 608 { 609 "Extract with concepts and targetVectors", 610 args{ 611 source: map[string]interface{}{ 612 "concepts": []interface{}{"c1", "c2", "c3"}, 613 "targetVectors": []interface{}{"targetVector"}, 614 "autocorrect": true, 615 }, 616 }, 617 &NearTextParams{ 618 Values: []string{"c1", "c2", "c3"}, 619 TargetVectors: []string{"targetVector"}, 620 Autocorrect: true, 621 }, 622 }, 623 } 624 testsWithAutocorrect = append(testsWithAutocorrect, tests...) 625 626 t.Run("should extract values", func(t *testing.T) { 627 provider := New(nil) 628 for _, tt := range tests { 629 t.Run(tt.name, func(t *testing.T) { 630 if got := provider.extractNearTextFn(tt.args.source); !reflect.DeepEqual(got, tt.want) { 631 t.Errorf("extractNearTextFn() = %v, want %v", got, tt.want) 632 } 633 }) 634 } 635 }) 636 t.Run("should extract values with transformer", func(t *testing.T) { 637 provider := New(&fakeTransformer{}) 638 for _, tt := range testsWithAutocorrect { 639 t.Run(tt.name, func(t *testing.T) { 640 if got := provider.extractNearTextFn(tt.args.source); !reflect.DeepEqual(got, tt.want) { 641 t.Errorf("extractNearTextFn() = %v, want %v", got, tt.want) 642 } 643 }) 644 } 645 }) 646 }