Skip to content

Commit 6a9aa4f

Browse files
committed
Add tests
1 parent 70757fd commit 6a9aa4f

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

libs/labelbox/tests/data/annotation_import/test_relationships.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,62 @@ def test_relationship_readonly_explicit_true():
409409
),
410410
)
411411
assert relationship.value.readonly is True
412+
413+
414+
def test_relationship_source_ontology_name():
415+
"""Test that relationship can be created with source_ontology_name instead of source."""
416+
target = ObjectAnnotation(
417+
name="e2",
418+
value=TextEntity(start=30, end=35),
419+
)
420+
421+
relationship = RelationshipAnnotation(
422+
name="rel",
423+
value=Relationship(
424+
source_ontology_name="test_source",
425+
target=target,
426+
type=Relationship.Type.UNIDIRECTIONAL,
427+
),
428+
)
429+
assert relationship.value.source_ontology_name == "test_source"
430+
assert relationship.value.source is None
431+
432+
433+
def test_relationship_missing_source_validation():
434+
"""Test that relationship requires either source or source_ontology_name."""
435+
target = ObjectAnnotation(
436+
name="e2",
437+
value=TextEntity(start=30, end=35),
438+
)
439+
440+
with pytest.raises(ValueError, match="Either source or source_ontology_name must be provided"):
441+
RelationshipAnnotation(
442+
name="rel",
443+
value=Relationship(
444+
target=target,
445+
type=Relationship.Type.UNIDIRECTIONAL,
446+
),
447+
)
448+
449+
450+
def test_relationship_both_sources_validation():
451+
"""Test that relationship cannot have both source and source_ontology_name."""
452+
source = ObjectAnnotation(
453+
name="e1",
454+
value=TextEntity(start=10, end=12),
455+
)
456+
target = ObjectAnnotation(
457+
name="e2",
458+
value=TextEntity(start=30, end=35),
459+
)
460+
461+
with pytest.raises(ValueError, match="Only one of 'source' or 'source_ontology_name' may be provided"):
462+
RelationshipAnnotation(
463+
name="rel",
464+
value=Relationship(
465+
source=source,
466+
source_ontology_name="test_source",
467+
target=target,
468+
type=Relationship.Type.UNIDIRECTIONAL,
469+
),
470+
)

libs/labelbox/tests/data/serialization/ndjson/test_relationship.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
RelationshipAnnotation,
66
Relationship,
77
TextEntity,
8+
DocumentRectangle,
9+
RectangleUnit,
10+
Point,
811
)
912

1013

@@ -285,3 +288,133 @@ def test_readonly_relationships():
285288
non_readonly_rel_serialized["relationship"]["type"] == "bidirectional"
286289
)
287290
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

Comments
 (0)