1- import pytest
1+ """
2+ TODO:
3+ test option.add_option
4+ test all classes' asdicts (what is the best way...)
5+ test classification.add_option
6+ test tool.add_classification
7+ consider testing and ensuring failed scenarios
8+ """
29from typing import Any , Dict , List , Union
10+
11+ import pytest
12+
313from labelbox import LabelingFrontend
14+ from labelbox .schema .ontology_generator import Ontology , \
15+ Tool , Classification , Option , InconsistentOntologyException
416
5- from labelbox .schema .ontology_generator import Ontology , Tool , Classification , Option , InconsistentOntologyException
617
7- def sample_ontology () -> Dict [str , Any ]:
8- return {
18+ _SAMPLE_ONTOLOGY = {
919 "tools" : [{
1020 "required" : False ,
1121 "name" : "Dog" ,
1222 "color" : "#FF0000" ,
1323 "tool" : "rectangle" ,
1424 "classifications" : []
15- }],
25+ }],
1626 "classifications" : [{
17- "required" :
18- True ,
19- "instructions" :
20- "This is a question." ,
21- "name" :
22- "this_is_a_question." ,
23- "type" :
24- "radio" ,
27+ "required" : True ,
28+ "instructions" : "This is a question." ,
29+ "name" : "This is a question." ,
30+ "type" : "radio" ,
2531 "options" : [{
26- "label" : "Yes " ,
32+ "label" : "yes " ,
2733 "value" : "yes"
2834 }, {
29- "label" : "No " ,
35+ "label" : "no " ,
3036 "value" : "no"
3137 }]
3238 }]
3339 }
3440
35- #test asdict
36- #test nested classifications
37- """
38- Tool tests
39- """
40- # def test_create_tool(client, project) -> None:
41- def test_create_bbox_tool () -> None :
42- t = Tool (tool = Tool .Type .BBOX , name = "box tool" )
43- assert (t .tool == Tool .Type .BBOX )
44- assert (t .name == "box tool" )
45-
46- def test_create_point_tool () -> None :
47- t = Tool (tool = Tool .Type .POINT , name = "point tool" )
48- assert (t .tool == Tool .Type .POINT )
49- assert (t .name == "point tool" )
50-
51- def test_create_polygon_tool () -> None :
52- t = Tool (tool = Tool .Type .POLYGON , name = "polygon tool" )
53- assert (t .tool == Tool .Type .POLYGON )
54- assert (t .name == "polygon tool" )
55-
56- def test_create_ner_tool () -> None :
57- t = Tool (tool = Tool .Type .NER , name = "ner tool" )
58- assert (t .tool == Tool .Type .NER )
59- assert (t .name == "ner tool" )
60-
61- def test_create_segment_tool () -> None :
62- t = Tool (tool = Tool .Type .SEGMENTATION , name = "segment tool" )
63- assert (t .tool == Tool .Type .SEGMENTATION )
64- assert (t .name == "segment tool" )
65-
66- def test_create_line_tool () -> None :
67- t = Tool (tool = Tool .Type .LINE , name = "line tool" )
68- assert (t .tool == Tool .Type .LINE )
69- assert (t .name == "line tool" )
70-
71- """
72- Classification tests
73- """
74- def test_create_text_classification () -> None :
75- c = Classification (class_type = Classification .Type .TEXT , instructions = "text" )
76- assert (c .class_type == Classification .Type .TEXT )
77- assert (c .instructions == "text" )
78- assert (c .class_type not in c ._REQUIRES_OPTIONS )
79-
80- def test_create_radio_classification () -> None :
81- c = Classification (class_type = Classification .Type .RADIO , instructions = "radio" )
82- assert (c .class_type == Classification .Type .RADIO )
83- assert (c .instructions == "radio" )
84- assert (c .class_type in c ._REQUIRES_OPTIONS )
85-
86- def test_create_checklist_classification () -> None :
87- c = Classification (class_type = Classification .Type .CHECKLIST , instructions = "checklist" )
88- assert (c .class_type == Classification .Type .CHECKLIST )
89- assert (c .instructions == "checklist" )
90- assert (c .class_type in c ._REQUIRES_OPTIONS )
91-
92- def test_create_dropdown_classification () -> None :
93- c = Classification (class_type = Classification .Type .DROPDOWN , instructions = "dropdown" )
94- assert (c .class_type == Classification .Type .DROPDOWN )
95- assert (c .instructions == "dropdown" )
96- assert (c .class_type in c ._REQUIRES_OPTIONS )
97-
98- """
99- Option tests
100- """
101- def test_create_int_option () -> None :
102- o = Option (value = 3 )
103- assert (o .value == 3 )
104- assert (type (o .value ) == int )
105-
106- def test_create_string_option () -> None :
107- o = Option (value = "3" )
108- assert (o .value == "3" )
109- assert (type (o .value )== str )
110-
111- """
112- Ontology tests
113- """
114- def test_create_ontology () -> None :
115- """
116- Tests the initial structure of an Ontology object
117- """
41+ @pytest .mark .parametrize ("tool_type" , list (Tool .Type ))
42+ @pytest .mark .parametrize ("tool_name" , ["tool" ])
43+ def test_create_tool (tool_type , tool_name ) -> None :
44+ t = Tool (tool = tool_type , name = tool_name )
45+ assert (t .tool == tool_type )
46+ assert (t .name == tool_name )
47+
48+ @pytest .mark .parametrize ("class_type" , list (Classification .Type ))
49+ @pytest .mark .parametrize ("class_instr" , ["classification" ])
50+ def test_create_classification (class_type , class_instr ) -> None :
51+ c = Classification (class_type = class_type , instructions = class_instr )
52+ assert (c .class_type == class_type )
53+ assert (c .instructions == class_instr )
54+ assert (c .name == c .instructions )
55+
56+ @pytest .mark .parametrize (
57+ "value, expected_value, typing" ,[(3 ,3 , int ),("string" ,"string" , str )])
58+ def test_create_option (value , expected_value , typing ) -> None :
59+ o = Option (value = value )
60+ assert (o .value == expected_value )
61+ assert (o .value == o .label )
62+ assert (type (o .value ) == typing )
63+
64+ def test_create_empty_ontology () -> None :
11865 o = Ontology ()
119- assert type (o ) == Ontology
12066 assert (o .tools == [])
12167 assert (o .classifications == [])
12268
12369def test_add_ontology_tool () -> None :
124- """
125- Tests the possible ways to add a tool to an ontology
126- """
12770 o = Ontology ()
12871 o .add_tool (Tool (tool = Tool .Type .BBOX , name = "bounding box" ))
12972
13073 second_tool = Tool (tool = Tool .Type .SEGMENTATION , name = "segmentation" )
13174 o .add_tool (second_tool )
13275
133- for tool in o .tools :
134- assert type (tool ) == Tool
76+ assert len (o .tools ) == 2
13577
13678def test_add_ontology_classification () -> None :
137- """
138- Tests the possible ways to add a classification to an ontology
139- """
14079 o = Ontology ()
14180 o .add_classification (Classification (
14281 class_type = Classification .Type .TEXT , instructions = "text" ))
@@ -145,94 +84,22 @@ def test_add_ontology_classification() -> None:
14584 class_type = Classification .Type .CHECKLIST , instructions = "checklist" )
14685 o .add_classification (second_classification )
14786
148- for classification in o .classifications :
149- assert type (classification ) == Classification
87+ assert len (o .classifications ) == 2
15088
15189def test_ontology_asdict (project ) -> None :
152- """
153- Tests the asdict() method to ensure that it matches the format
154- of a project ontology
155- """
156- from_project_ontology = project .ontology ().normalized
157-
15890 o = Ontology .from_project (project )
159- assert o .asdict () == from_project_ontology
91+ assert o .asdict () == project . ontology (). normalized
16092
16193def test_from_project_ontology (client , project ) -> None :
162- """
163- Tests the ability to correctly get an existing project's ontology
164- and if it can correctly convert it to the right object types
165- """
16694 frontend = list (
16795 client .get_labeling_frontends (
16896 where = LabelingFrontend .name == "Editor" ))[0 ]
169- project .setup (frontend , sample_ontology ())
170-
171- ontology = Ontology .from_project (project )
172- assert len (ontology .tools ) == 1
173- assert ontology .tools [0 ].tool == Tool .Type .BBOX
174- for tool in ontology .tools :
175- assert type (tool ) == Tool
176-
177- assert len (ontology .classifications ) == 1
178- assert ontology .classifications [0 ].class_type == Classification .Type .RADIO
179- for classification in ontology .classifications :
180- assert type (classification ) == Classification
181-
182- assert len (ontology .classifications [0 ].options ) == 2
183- assert ontology .classifications [0 ].options [0 ].value .lower () == "yes"
184- assert ontology .classifications [0 ].options [0 ].label .lower () == "yes"
185- for option in ontology .classifications [0 ].options :
186- assert type (option ) == Option
187-
188-
97+ project .setup (frontend , _SAMPLE_ONTOLOGY )
98+ o = Ontology .from_project (project )
18999
100+ assert o .tools [0 ].tool == Tool .Type .BBOX
101+ assert o .classifications [0 ].class_type == Classification .Type .RADIO
102+ assert o .classifications [0 ].options [0 ].value .lower () == "yes"
190103
191104
192-
193- """
194- Old ontology file test
195- """
196- # def test_create_ontology(client, project) -> None:
197- # """ Tests that the ontology that a project was set up with can be grabbed."""
198- # frontend = list(
199- # client.get_labeling_frontends(
200- # where=LabelingFrontend.name == "Editor"))[0]
201- # project.setup(frontend, sample_ontology())
202- # normalized_ontology = project.ontology().normalized
203-
204- # def _remove_schema_ids(
205- # ontology_part: Union[List, Dict[str, Any]]) -> Dict[str, Any]:
206- # """ Recursively scrub the normalized ontology of any schema information."""
207- # removals = {'featureSchemaId', 'schemaNodeId'}
208-
209- # if isinstance(ontology_part, list):
210- # return [_remove_schema_ids(part) for part in ontology_part]
211- # if isinstance(ontology_part, dict):
212- # return {
213- # key: _remove_schema_ids(value)
214- # for key, value in ontology_part.items()
215- # if key not in removals
216- # }
217- # return ontology_part
218-
219- # removed = _remove_schema_ids(normalized_ontology)
220- # assert removed == sample_ontology()
221-
222- # ontology = project.ontology()
223-
224- # tools = ontology.tools()
225- # assert tools
226- # for tool in tools:
227- # assert tool.feature_schema_id
228- # assert tool.schema_node_id
229-
230- # classifications = ontology.classifications()
231- # assert classifications
232- # for classification in classifications:
233- # assert classification.feature_schema_id
234- # assert classification.schema_node_id
235- # for option in classification.options:
236- # assert option.feature_schema_id
237- # assert option.schema_node_id
238-
105+
0 commit comments