|
5 | 5 | RelationshipAnnotation,
|
6 | 6 | Relationship,
|
7 | 7 | TextEntity,
|
| 8 | + DocumentRectangle, |
| 9 | + RectangleUnit, |
| 10 | + Point, |
8 | 11 | )
|
9 | 12 |
|
10 | 13 |
|
@@ -285,3 +288,133 @@ def test_readonly_relationships():
|
285 | 288 | non_readonly_rel_serialized["relationship"]["type"] == "bidirectional"
|
286 | 289 | )
|
287 | 290 | assert non_readonly_rel_serialized["relationship"]["readonly"] is False
|
| 291 | + |
| 292 | + |
| 293 | +def test_source_ontology_name_relationship(): |
| 294 | + ner_source = ObjectAnnotation( |
| 295 | + name="e1", |
| 296 | + value=TextEntity(start=10, end=12), |
| 297 | + ) |
| 298 | + |
| 299 | + ner_target = ObjectAnnotation( |
| 300 | + name="e2", |
| 301 | + value=TextEntity(start=30, end=35), |
| 302 | + ) |
| 303 | + |
| 304 | + # Test relationship with source |
| 305 | + regular_relationship = RelationshipAnnotation( |
| 306 | + name="regular_rel", |
| 307 | + value=Relationship( |
| 308 | + source=ner_source, |
| 309 | + target=ner_target, |
| 310 | + type=Relationship.Type.UNIDIRECTIONAL, |
| 311 | + ), |
| 312 | + ) |
| 313 | + |
| 314 | + # Test relationship with source_ontology_name for PDF target |
| 315 | + pdf_target = ObjectAnnotation( |
| 316 | + name="pdf_region", |
| 317 | + value=DocumentRectangle( |
| 318 | + start=Point(x=0.5, y=0.5), |
| 319 | + end=Point(x=0.7, y=0.7), |
| 320 | + page=1, |
| 321 | + unit=RectangleUnit.PERCENT, |
| 322 | + ), |
| 323 | + ) |
| 324 | + |
| 325 | + ontology_relationship = RelationshipAnnotation( |
| 326 | + name="ontology_rel", |
| 327 | + value=Relationship( |
| 328 | + source_ontology_name="Person", |
| 329 | + target=pdf_target, |
| 330 | + type=Relationship.Type.UNIDIRECTIONAL, |
| 331 | + ), |
| 332 | + ) |
| 333 | + |
| 334 | + label = Label( |
| 335 | + data={"uid": "clqbkpy236syk07978v3pscw1"}, |
| 336 | + annotations=[ |
| 337 | + ner_source, |
| 338 | + ner_target, |
| 339 | + pdf_target, |
| 340 | + regular_relationship, |
| 341 | + ontology_relationship, |
| 342 | + ], |
| 343 | + ) |
| 344 | + |
| 345 | + serialized_label = list(NDJsonConverter.serialize([label])) |
| 346 | + |
| 347 | + ner_source_serialized = next( |
| 348 | + annotation |
| 349 | + for annotation in serialized_label |
| 350 | + if annotation["name"] == ner_source.name |
| 351 | + ) |
| 352 | + ner_target_serialized = next( |
| 353 | + annotation |
| 354 | + for annotation in serialized_label |
| 355 | + if annotation["name"] == ner_target.name |
| 356 | + ) |
| 357 | + pdf_target_serialized = next( |
| 358 | + annotation |
| 359 | + for annotation in serialized_label |
| 360 | + if annotation["name"] == pdf_target.name |
| 361 | + ) |
| 362 | + regular_rel_serialized = next( |
| 363 | + annotation |
| 364 | + for annotation in serialized_label |
| 365 | + if annotation["name"] == regular_relationship.name |
| 366 | + ) |
| 367 | + ontology_rel_serialized = next( |
| 368 | + annotation |
| 369 | + for annotation in serialized_label |
| 370 | + if annotation["name"] == ontology_relationship.name |
| 371 | + ) |
| 372 | + |
| 373 | + # Verify regular relationship |
| 374 | + assert ( |
| 375 | + regular_rel_serialized["relationship"]["source"] |
| 376 | + == ner_source_serialized["uuid"] |
| 377 | + ) |
| 378 | + assert ( |
| 379 | + regular_rel_serialized["relationship"]["target"] |
| 380 | + == ner_target_serialized["uuid"] |
| 381 | + ) |
| 382 | + assert regular_rel_serialized["relationship"]["type"] == "unidirectional" |
| 383 | + |
| 384 | + # Verify relationship with source_ontology_name |
| 385 | + assert ( |
| 386 | + ontology_rel_serialized["relationship"]["sourceOntologyName"] == "Person" |
| 387 | + ) |
| 388 | + assert ( |
| 389 | + ontology_rel_serialized["relationship"]["target"] |
| 390 | + == pdf_target_serialized["uuid"] |
| 391 | + ) |
| 392 | + assert ontology_rel_serialized["relationship"]["type"] == "unidirectional" |
| 393 | + |
| 394 | + # Test that providing both source and source_ontology_name raises an error |
| 395 | + try: |
| 396 | + RelationshipAnnotation( |
| 397 | + name="invalid_rel", |
| 398 | + value=Relationship( |
| 399 | + source=ner_source, |
| 400 | + source_ontology_name="Person", |
| 401 | + target=pdf_target, |
| 402 | + type=Relationship.Type.UNIDIRECTIONAL, |
| 403 | + ), |
| 404 | + ) |
| 405 | + assert False, "Expected ValueError for providing both source and source_ontology_name" |
| 406 | + except Exception as e: |
| 407 | + assert "Value error, Only one of 'source' or 'source_ontology_name' may be provided" in str(e) |
| 408 | + |
| 409 | + # Test that providing neither source nor source_ontology_name raises an error |
| 410 | + try: |
| 411 | + RelationshipAnnotation( |
| 412 | + name="invalid_rel", |
| 413 | + value=Relationship( |
| 414 | + target=pdf_target, |
| 415 | + type=Relationship.Type.UNIDIRECTIONAL, |
| 416 | + ), |
| 417 | + ) |
| 418 | + assert False, "Expected ValueError for providing neither source nor source_ontology_name" |
| 419 | + except Exception as e: |
| 420 | + assert "Value error, Either source or source_ontology_name must be provided" in str(e) |
0 commit comments