|
6 | 6 | #want to import ontology_generator.py properly, not the bad way we are currently doing |
7 | 7 | from labelbox.schema.ontology_generator import Ontology, Tool, Classification, Option, InconsistentOntologyException |
8 | 8 |
|
9 | | - |
10 | 9 | def sample_ontology() -> Dict[str, Any]: |
11 | 10 | return { |
12 | 11 | "tools": [{ |
@@ -35,12 +34,9 @@ def sample_ontology() -> Dict[str, Any]: |
35 | 34 | }] |
36 | 35 | } |
37 | 36 |
|
38 | | -#want to create base case tests, each indiv tool, classification, option |
39 | | -#want to then do nested objects inside each |
40 | | -#do we want to test colors, bool, etc? |
41 | | -#test inside methods? like asdict/fromdict? |
42 | | -#test ontology.from_project? |
43 | | -#test ontology.build? |
| 37 | +#test asdict |
| 38 | +#test ontology.asdict |
| 39 | +#test nested classifications |
44 | 40 | """ |
45 | 41 | Tool tests |
46 | 42 | """ |
@@ -119,50 +115,130 @@ def test_create_string_option() -> None: |
119 | 115 | Ontology tests |
120 | 116 | """ |
121 | 117 | def test_create_ontology() -> None: |
| 118 | + """ |
| 119 | + Tests the initial structure of an Ontology object |
| 120 | + """ |
122 | 121 | o = Ontology() |
| 122 | + assert type(o) == Ontology |
123 | 123 | assert(o.tools == []) |
124 | 124 | assert(o.classifications == []) |
125 | 125 |
|
126 | | -def test_create_ontology(client, project) -> None: |
127 | | - """ Tests that the ontology that a project was set up with can be grabbed.""" |
| 126 | +def test_add_ontology_tool() -> None: |
| 127 | + """ |
| 128 | + Tests the possible ways to add a tool to an ontology |
| 129 | + """ |
| 130 | + o = Ontology() |
| 131 | + o.add_tool(Tool(tool = Tool.Type.BBOX, name = "bounding box")) |
| 132 | + |
| 133 | + second_tool = Tool(tool = Tool.Type.SEGMENTATION, name = "segmentation") |
| 134 | + o.add_tool(second_tool) |
| 135 | + |
| 136 | + for tool in o.tools: |
| 137 | + assert type(tool) == Tool |
| 138 | + |
| 139 | +def test_add_ontology_classification() -> None: |
| 140 | + """ |
| 141 | + Tests the possible ways to add a classification to an ontology |
| 142 | + """ |
| 143 | + o = Ontology() |
| 144 | + o.add_classification(Classification( |
| 145 | + class_type = Classification.Type.TEXT, instructions = "text")) |
| 146 | + |
| 147 | + second_classification = Classification( |
| 148 | + class_type = Classification.Type.CHECKLIST, instructions = "checklist") |
| 149 | + o.add_classification(second_classification) |
| 150 | + |
| 151 | + for classification in o.classifications: |
| 152 | + assert type(classification) == Classification |
| 153 | + |
| 154 | +def test_ontology_asdict(project) -> None: |
| 155 | + """ |
| 156 | + Tests the asdict() method to ensure that it matches the format |
| 157 | + of a project ontology |
| 158 | + """ |
| 159 | + from_project_ontology = project.ontology().normalized |
| 160 | + |
| 161 | + o = Ontology.from_project(project) |
| 162 | + assert o.asdict() == from_project_ontology |
| 163 | + |
| 164 | +def test_from_project_ontology(client, project) -> None: |
| 165 | + """ |
| 166 | + Tests the ability to correctly get an existing project's ontology |
| 167 | + and if it can correctly convert it to the right object types |
| 168 | + """ |
128 | 169 | frontend = list( |
129 | 170 | client.get_labeling_frontends( |
130 | 171 | where=LabelingFrontend.name == "Editor"))[0] |
131 | 172 | project.setup(frontend, sample_ontology()) |
132 | | - normalized_ontology = project.ontology().normalized |
133 | | - |
134 | | - def _remove_schema_ids( |
135 | | - ontology_part: Union[List, Dict[str, Any]]) -> Dict[str, Any]: |
136 | | - """ Recursively scrub the normalized ontology of any schema information.""" |
137 | | - removals = {'featureSchemaId', 'schemaNodeId'} |
138 | | - |
139 | | - if isinstance(ontology_part, list): |
140 | | - return [_remove_schema_ids(part) for part in ontology_part] |
141 | | - if isinstance(ontology_part, dict): |
142 | | - return { |
143 | | - key: _remove_schema_ids(value) |
144 | | - for key, value in ontology_part.items() |
145 | | - if key not in removals |
146 | | - } |
147 | | - return ontology_part |
148 | | - |
149 | | - removed = _remove_schema_ids(normalized_ontology) |
150 | | - assert removed == sample_ontology() |
151 | | - |
152 | | - ontology = project.ontology() |
153 | | - |
154 | | - tools = ontology.tools() |
155 | | - assert tools |
156 | | - for tool in tools: |
157 | | - assert tool.feature_schema_id |
158 | | - assert tool.schema_node_id |
159 | | - |
160 | | - classifications = ontology.classifications() |
161 | | - assert classifications |
162 | | - for classification in classifications: |
163 | | - assert classification.feature_schema_id |
164 | | - assert classification.schema_node_id |
165 | | - for option in classification.options: |
166 | | - assert option.feature_schema_id |
167 | | - assert option.schema_node_id |
| 173 | + |
| 174 | + ontology = Ontology.from_project(project) |
| 175 | + assert len(ontology.tools) == 1 |
| 176 | + assert ontology.tools[0].tool == Tool.Type.BBOX |
| 177 | + for tool in ontology.tools: |
| 178 | + assert type(tool) == Tool |
| 179 | + |
| 180 | + assert len(ontology.classifications) == 1 |
| 181 | + assert ontology.classifications[0].class_type == Classification.Type.RADIO |
| 182 | + for classification in ontology.classifications: |
| 183 | + assert type(classification) == Classification |
| 184 | + |
| 185 | + assert len(ontology.classifications[0].options) == 2 |
| 186 | + assert ontology.classifications[0].options[0].value.lower() == "yes" |
| 187 | + assert ontology.classifications[0].options[0].label.lower() == "yes" |
| 188 | + for option in ontology.classifications[0].options: |
| 189 | + assert type(option) == Option |
| 190 | + |
| 191 | +# def test_ontology_asdict() -> None: |
| 192 | +# o = Ontology() |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +""" |
| 200 | +Old ontology file test |
| 201 | +""" |
| 202 | +# def test_create_ontology(client, project) -> None: |
| 203 | +# """ Tests that the ontology that a project was set up with can be grabbed.""" |
| 204 | +# frontend = list( |
| 205 | +# client.get_labeling_frontends( |
| 206 | +# where=LabelingFrontend.name == "Editor"))[0] |
| 207 | +# project.setup(frontend, sample_ontology()) |
| 208 | +# normalized_ontology = project.ontology().normalized |
| 209 | + |
| 210 | +# def _remove_schema_ids( |
| 211 | +# ontology_part: Union[List, Dict[str, Any]]) -> Dict[str, Any]: |
| 212 | +# """ Recursively scrub the normalized ontology of any schema information.""" |
| 213 | +# removals = {'featureSchemaId', 'schemaNodeId'} |
| 214 | + |
| 215 | +# if isinstance(ontology_part, list): |
| 216 | +# return [_remove_schema_ids(part) for part in ontology_part] |
| 217 | +# if isinstance(ontology_part, dict): |
| 218 | +# return { |
| 219 | +# key: _remove_schema_ids(value) |
| 220 | +# for key, value in ontology_part.items() |
| 221 | +# if key not in removals |
| 222 | +# } |
| 223 | +# return ontology_part |
| 224 | + |
| 225 | +# removed = _remove_schema_ids(normalized_ontology) |
| 226 | +# assert removed == sample_ontology() |
| 227 | + |
| 228 | +# ontology = project.ontology() |
| 229 | + |
| 230 | +# tools = ontology.tools() |
| 231 | +# assert tools |
| 232 | +# for tool in tools: |
| 233 | +# assert tool.feature_schema_id |
| 234 | +# assert tool.schema_node_id |
| 235 | + |
| 236 | +# classifications = ontology.classifications() |
| 237 | +# assert classifications |
| 238 | +# for classification in classifications: |
| 239 | +# assert classification.feature_schema_id |
| 240 | +# assert classification.schema_node_id |
| 241 | +# for option in classification.options: |
| 242 | +# assert option.feature_schema_id |
| 243 | +# assert option.schema_node_id |
168 | 244 |
|
0 commit comments