@@ -504,3 +504,56 @@ class TestDocument(Document):
504504 ),
505505 ):
506506 doc = TestDocument (text = "text1" )
507+
508+
509+ def test_annotation_list_targets ():
510+ @dataclasses .dataclass
511+ class TestDocument (Document ):
512+ text : str
513+ entities1 : AnnotationList [LabeledSpan ] = annotation_field (target = "text" )
514+ entities2 : AnnotationList [LabeledSpan ] = annotation_field (target = "text" )
515+ relations1 : AnnotationList [BinaryRelation ] = annotation_field (target = "entities1" )
516+ relations2 : AnnotationList [BinaryRelation ] = annotation_field (
517+ targets = ["entities1" , "entities2" ]
518+ )
519+
520+ doc = TestDocument (text = "text1" )
521+
522+ # test getting all targets
523+ assert doc .entities1 .targets == {"text" : doc .text }
524+ assert doc .entities2 .targets == {"text" : doc .text }
525+ assert doc .relations1 .targets == {"entities1" : doc .entities1 }
526+ assert doc .relations2 .targets == {"entities1" : doc .entities1 , "entities2" : doc .entities2 }
527+
528+ # test getting a single target
529+ assert doc .entities1 .target == doc .text
530+ assert doc .entities2 .target == doc .text
531+ assert doc .relations1 .target == doc .entities1
532+ # check that the target of relations2 is not set because it has more than one target
533+ with pytest .raises (ValueError ) as excinfo :
534+ doc .relations2 .target
535+ assert (
536+ str (excinfo .value )
537+ == "The annotation layer has more or less than one target: ['entities1', 'entities2']"
538+ )
539+
540+ # test getting all target layers
541+ assert doc .entities1 .target_layers == {}
542+ assert doc .entities2 .target_layers == {}
543+ assert doc .relations1 .target_layers == {"entities1" : doc .entities1 }
544+ assert doc .relations2 .target_layers == {"entities1" : doc .entities1 , "entities2" : doc .entities2 }
545+
546+ # test getting a single target layer
547+ with pytest .raises (ValueError ) as excinfo :
548+ doc .entities1 .target_layer
549+ assert str (excinfo .value ) == "The annotation layer has more or less than one target layer: []"
550+ with pytest .raises (ValueError ) as excinfo :
551+ doc .entities2 .target_layer
552+ assert str (excinfo .value ) == "The annotation layer has more or less than one target layer: []"
553+ assert doc .relations1 .target_layer == doc .entities1
554+ with pytest .raises (ValueError ) as excinfo :
555+ doc .relations2 .target_layer
556+ assert (
557+ str (excinfo .value )
558+ == "The annotation layer has more or less than one target layer: ['entities1', 'entities2']"
559+ )
0 commit comments