1- """
2- TODO:
3- test option.add_option
4- test classification.add_option
5- test tool.add_classification
6- consider testing and ensuring failed scenarios
7- """
81from typing import Any , Dict , List , Union
92
103import pytest
2114 "color" : "#FF0000" ,
2215 "tool" : "rectangle" ,
2316 "classifications" : []
24- }],
17+ }],
2518 "classifications" : [{
2619 "required" : True ,
2720 "instructions" : "This is a question." ,
@@ -71,9 +64,12 @@ def test_add_ontology_tool() -> None:
7164
7265 second_tool = Tool (tool = Tool .Type .SEGMENTATION , name = "segmentation" )
7366 o .add_tool (second_tool )
74-
7567 assert len (o .tools ) == 2
7668
69+ with pytest .raises (InconsistentOntologyException ) as exc :
70+ o .add_tool (Tool (tool = Tool .Type .BBOX , name = "bounding box" ))
71+ assert "Duplicate tool name" in str (exc .value )
72+
7773def test_add_ontology_classification () -> None :
7874 o = Ontology ()
7975 o .add_classification (Classification (
@@ -82,23 +78,56 @@ def test_add_ontology_classification() -> None:
8278 second_classification = Classification (
8379 class_type = Classification .Type .CHECKLIST , instructions = "checklist" )
8480 o .add_classification (second_classification )
85-
8681 assert len (o .classifications ) == 2
8782
83+ with pytest .raises (InconsistentOntologyException ) as exc :
84+ o .add_classification (Classification (
85+ class_type = Classification .Type .TEXT , instructions = "text" ))
86+ assert "Duplicate classification instructions" in str (exc .value )
87+
88+ def test_tool_add_classification () -> None :
89+ t = Tool (tool = Tool .Type .SEGMENTATION , name = "segmentation" )
90+ c = Classification (
91+ class_type = Classification .Type .TEXT , instructions = "text" )
92+ t .add_classification (c )
93+ assert t .classifications [0 ] == c
94+
95+ with pytest .raises (Exception ) as exc :
96+ t .add_classification (c )
97+ assert "Duplicate nested classification" in str (exc )
98+
99+ def test_classification_add_option () -> None :
100+ c = Classification (
101+ class_type = Classification .Type .RADIO , instructions = "radio" )
102+ o = Option (value = "option" )
103+ c .add_option (o )
104+ assert c .options [0 ] == o
105+
106+ with pytest .raises (InconsistentOntologyException ) as exc :
107+ c .add_option (Option (value = "option" ))
108+ assert "Duplicate option" in str (exc .value )
109+
110+ def test_option_add_option () -> None :
111+ o = Option (value = "option" )
112+ c = Classification (
113+ class_type = Classification .Type .TEXT , instructions = "text" )
114+ o .add_option (c )
115+ assert o .options [0 ] == c
116+
117+ with pytest .raises (InconsistentOntologyException ) as exc :
118+ o .add_option (c )
119+ assert "Duplicate nested classification" in str (exc .value )
120+
88121def test_ontology_asdict (project ) -> None :
89122 o = Ontology .from_project (project )
90123 assert o .asdict () == project .ontology ().normalized
91124
92125def test_from_project_ontology (client , project ) -> None :
93- frontend = list (
94- client .get_labeling_frontends (
95- where = LabelingFrontend .name == "Editor" ))[0 ]
126+ frontend = list (client .get_labeling_frontends (
127+ where = LabelingFrontend .name == "Editor" ))[0 ]
96128 project .setup (frontend , _SAMPLE_ONTOLOGY )
97129 o = Ontology .from_project (project )
98130
99131 assert o .tools [0 ].tool == Tool .Type .BBOX
100132 assert o .classifications [0 ].class_type == Classification .Type .RADIO
101- assert o .classifications [0 ].options [0 ].value .lower () == "yes"
102-
103-
104-
133+ assert o .classifications [0 ].options [0 ].value .lower () == "yes"
0 commit comments