Skip to content

Commit 7dad6f1

Browse files
committed
Add tests
1 parent c3eba39 commit 7dad6f1

File tree

1 file changed

+100
-82
lines changed

1 file changed

+100
-82
lines changed

tests/test_validators.py

Lines changed: 100 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,74 @@ def test_validate_document_annotation_without_classname(self):
5858
self.assertTrue(validator.is_valid())
5959

6060

61+
def test_validate_annotation_with_wrong_bbox(self):
62+
with tempfile.TemporaryDirectory() as tmpdir_name:
63+
with open(f"{tmpdir_name}/vector.json", "w") as vector_json:
64+
vector_json.write(
65+
"""
66+
{
67+
"metadata": {
68+
"name": "example_image_1.jpg",
69+
"width": 1024,
70+
"height": 683,
71+
"status": "Completed",
72+
"pinned": false,
73+
"isPredicted": null,
74+
"projectId": null,
75+
"annotatorEmail": null,
76+
"qaEmail": null
77+
},
78+
"instances": [
79+
{
80+
"type": "bbox",
81+
"classId": 72274,
82+
"probability": 100,
83+
"points": {
84+
85+
"x2": 465.23,
86+
"y1": 341.5,
87+
"y2": 357.09
88+
},
89+
"groupId": 0,
90+
"pointLabels": {},
91+
"locked": false,
92+
"visible": false,
93+
"attributes": [
94+
{
95+
"id": 117845,
96+
"groupId": 28230,
97+
"name": "2",
98+
"groupName": "Num doors"
99+
}
100+
],
101+
"trackingId": "aaa97f80c9e54a5f2dc2e920fc92e5033d9af45b",
102+
"error": null,
103+
"createdAt": null,
104+
"createdBy": null,
105+
"creationType": null,
106+
"updatedAt": null,
107+
"updatedBy": null,
108+
"className": "Personal vehicle"
109+
}
110+
]
111+
}
112+
113+
"""
114+
115+
116+
)
117+
118+
with open(f"{tmpdir_name}/vector.json", "r") as f:
119+
data = json.loads(f.read())
120+
validator = AnnotationValidators.get_validator("vector")(data)
121+
self.assertFalse(validator.is_valid())
122+
self.assertEqual(len(validator.generate_report()), 63)
61123

62-
# @patch('builtins.print')
63-
# def test_validate_annotation_with_wrong_bbox(self, mock_print):
64-
# with tempfile.TemporaryDirectory() as tmpdir_name:
65-
# with open(f"{tmpdir_name}/vector.json", "w") as vector_json:
66-
# vector_json.write(self.ANNOTATION)
67-
# sa.validate_annotations("Vector", os.path.join(self.vector_folder_path, f"{tmpdir_name}/vector.json"))
68-
# mock_print.assert_any_call(
69-
# "instances[0].type invalid type, valid types are bbox, "
70-
# "template, cuboid, polygon, point, polyline, ellipse, rbbox"
71-
# )
124+
# sa.validate_annotations("Vector", os.path.join(self.vector_folder_path, f"{tmpdir_name}/vector.json"))
125+
# mock_print.assert_any_call(
126+
# "instances[0].type invalid type, valid types are bbox, "
127+
# "template, cuboid, polygon, point, polyline, ellipse, rbbox"
128+
# )
72129

73130
def test_validate_document_annotation(self):
74131
with tempfile.TemporaryDirectory() as tmpdir_name:
@@ -100,9 +157,6 @@ def test_validate_document_annotation(self):
100157
validator = AnnotationValidators.get_validator("document")(data)
101158
self.assertTrue(validator.is_valid())
102159

103-
# self.assertTrue(
104-
# sa.validate_annotations("Document", os.path.join(self.vector_folder_path, f"{tmpdir_name}/doc.json")))
105-
106160
def test_validate_pixel_annotation(self):
107161
with tempfile.TemporaryDirectory() as tmpdir_name:
108162
with open(f"{tmpdir_name}/pixel.json", "w") as pix_json:
@@ -137,9 +191,6 @@ def test_validate_pixel_annotation(self):
137191
validator = AnnotationValidators.get_validator("pixel")(data)
138192
self.assertTrue(validator.is_valid())
139193

140-
# self.assertTrue(
141-
# sa.validate_annotations("Pixel", os.path.join(self.vector_folder_path, f"{tmpdir_name}/pixel.json")))
142-
143194
def test_validate_video_export_annotation(self):
144195
with tempfile.TemporaryDirectory() as tmpdir_name:
145196
with open(f"{tmpdir_name}/video_export.json", "w") as video_export:
@@ -172,8 +223,6 @@ def test_validate_video_export_annotation(self):
172223
data = json.loads(f.read())
173224
validator = AnnotationValidators.get_validator("video")(data)
174225
self.assertTrue(validator.is_valid())
175-
# self.assertTrue(sa.validate_annotations("Video", os.path.join(self.vector_folder_path,
176-
# f"{tmpdir_name}/video_export.json")))
177226

178227
def test_validate_vector_empty_annotation(self):
179228
with tempfile.TemporaryDirectory() as tmpdir_name:
@@ -207,11 +256,9 @@ def test_validate_vector_empty_annotation(self):
207256
data = json.loads(f.read())
208257
validator = AnnotationValidators.get_validator("video")(data)
209258
self.assertTrue(validator.is_valid())
210-
# self.assertTrue(sa.validate_annotations("Vector", os.path.join(self.vector_folder_path,
211-
# f"{tmpdir_name}/vector_empty.json")))
212259

213-
@patch('builtins.print')
214-
def test_validate_error_message_format(self, mock_print):
260+
261+
def test_validate_error_message_format(self):
215262
with tempfile.TemporaryDirectory() as tmpdir_name:
216263
with open(f"{tmpdir_name}/test_validate_error_message_format.json",
217264
"w") as test_validate_error_message_format:
@@ -229,10 +276,6 @@ def test_validate_error_message_format(self, mock_print):
229276
self.assertFalse(validator.is_valid())
230277
self.assertEqual(validator.generate_report(),"metadata.name field required")
231278

232-
# sa.validate_annotations("Vector", os.path.join(self.vector_folder_path,
233-
# f"{tmpdir_name}/test_validate_error_message_format.json"))
234-
# mock_print.assert_any_call("metadata.name field required")
235-
236279
@patch('builtins.print')
237280
def test_validate_document_annotation_wrong_class_id(self, mock_print):
238281
with tempfile.TemporaryDirectory() as tmpdir_name:
@@ -284,10 +327,6 @@ def test_validate_document_annotation_wrong_class_id(self, mock_print):
284327
self.assertEqual(validator.generate_report(),
285328
"instances[0].classId value is not a valid integer")
286329

287-
# sa.validate_annotations("Document", os.path.join(self.vector_folder_path,
288-
# f"{tmpdir_name}/test_validate_document_annotation_wrong_class_id.json"))
289-
# mock_print.assert_any_call("instances[0].classId value is not a valid integer")
290-
291330
def test_validate_document_annotation_with_null_created_at(self):
292331
with tempfile.TemporaryDirectory() as tmpdir_name:
293332
with open(f"{tmpdir_name}/test_validate_document_annotation_with_null_created_at.json",
@@ -335,8 +374,6 @@ def test_validate_document_annotation_with_null_created_at(self):
335374
data = json.loads(f.read())
336375
validator = AnnotationValidators.get_validator("document")(data)
337376
self.assertTrue(validator.is_valid())
338-
# self.assertTrue(sa.validate_annotations("Document", os.path.join(self.vector_folder_path,
339-
# f"{tmpdir_name}/test_validate_document_annotation_with_null_created_at.json")))
340377

341378
@patch('builtins.print')
342379
def test_validate_vector_instance_type_and_attr_annotation(self, mock_print):
@@ -405,8 +442,6 @@ def test_validate_vector_instance_type_and_attr_annotation(self, mock_print):
405442
self.assertFalse(validator.is_valid())
406443
self.assertEqual(validator.generate_report(),
407444
"instances[0].type field required")
408-
# sa.validate_annotations("Vector", os.path.join(self.vector_folder_path, f"{tmpdir_name}/{json_name}"))
409-
# mock_print.assert_any_call("instances[0].type field required")
410445

411446
@patch('builtins.print')
412447
def test_validate_vector_invalid_instance_type_and_attr_annotation(self, mock_print):
@@ -474,19 +509,8 @@ def test_validate_vector_invalid_instance_type_and_attr_annotation(self, mock_pr
474509
data = json.loads(f.read())
475510
validator = AnnotationValidators.get_validator("vector")(data)
476511
self.assertFalse(validator.is_valid())
512+
self.assertEqual(len(validator.generate_report()),143)
477513

478-
# TODO: assert messgae
479-
# self.assertEqual(validator.generate_report(),
480-
# "instances[0].type field required")
481-
# sa.validate_annotations(
482-
# "Vector",
483-
# os.path.join(self.vector_folder_path,
484-
# f"{tmpdir_name}/test_validate_vector_invalid_instace_type_and_attr_annotation.json")
485-
# )
486-
# mock_print.assert_any_call(
487-
# "instances[0].type invalid type, valid types are bbox, "
488-
# "template, cuboid, polygon, point, polyline, ellipse, rbbox"
489-
# )
490514

491515
@patch('builtins.print')
492516
def test_validate_video_invalid_instance_type_and_attr_annotation(self, mock_print):
@@ -804,12 +828,6 @@ def test_validate_video_invalid_instance_type_and_attr_annotation(self, mock_pri
804828
self.assertEqual(validator.generate_report(),
805829
"instances[2].meta.type invalid type, valid types are bbox, event")
806830

807-
# sa.validate_annotations("Video", os.path.join(self.vector_folder_path,
808-
# f"{tmpdir_name}/test_validate_video_invalid_instace_type_and_attr_annotation.json"))
809-
# mock_print.assert_any_call(
810-
# "instances[2].meta.type invalid type, valid types are bbox, event"
811-
# )
812-
813831
@patch('builtins.print')
814832
def test_validate_video_invalid_instance_without_type_and_attr_annotation(self, mock_print):
815833
json_name = "test.json"
@@ -1117,10 +1135,13 @@ def test_validate_video_invalid_instance_without_type_and_attr_annotation(self,
11171135
}
11181136
'''
11191137
)
1120-
sa.validate_annotations("Video", os.path.join(self.vector_folder_path, f"{tmpdir_name}/{json_name}"))
1121-
mock_print.assert_any_call(
1122-
"instances[2].meta.type field required"
1123-
)
1138+
1139+
with open(f"{tmpdir_name}/{json_name}", "r") as f:
1140+
data = json.loads(f.read())
1141+
validator = AnnotationValidators.get_validator("video")(data)
1142+
self.assertFalse(validator.is_valid())
1143+
self.assertEqual(validator.generate_report(),
1144+
"instances[2].meta.type field required")
11241145

11251146
@patch('builtins.print')
11261147
def test_validate_vector_template_polygon_polyline_min_annotation(self, mock_print):
@@ -1271,16 +1292,15 @@ def test_validate_vector_template_polygon_polyline_min_annotation(self, mock_pri
12711292
}
12721293
'''
12731294
)
1274-
sa.validate_annotations("Vector", os.path.join(self.vector_folder_path,
1275-
f"{tmpdir_name}/{json_name}"))
1276-
mock_print.assert_any_call(
1277-
"metadata.width value is not a valid integer\n"
1278-
"instances[0].points ensure this value has at least 1 items\n"
1279-
"instances[1].points ensure this value has at least 3 items"
1280-
)
12811295

1282-
@patch('builtins.print')
1283-
def test_validate_video_point_labels(self, mock_print):
1296+
with open(f"{tmpdir_name}/{json_name}", "r") as f:
1297+
data = json.loads(f.read())
1298+
validator = AnnotationValidators.get_validator("vector")(data)
1299+
self.assertFalse(validator.is_valid())
1300+
self.assertEqual(len(validator.generate_report()),253)
1301+
1302+
1303+
def test_validate_video_point_labels(self):
12841304
with tempfile.TemporaryDirectory() as tmpdir_name:
12851305
with open(f"{tmpdir_name}/test_validate_video_point_labels.json",
12861306
"w") as test_validate_video_point_labels:
@@ -1586,14 +1606,15 @@ def test_validate_video_point_labels(self, mock_print):
15861606
'''
15871607
)
15881608

1589-
sa.validate_annotations("Video", os.path.join(self.vector_folder_path,
1590-
f"{tmpdir_name}/test_validate_video_point_labels.json"))
1591-
mock_print.assert_any_call(
1592-
"instances[0].meta.pointLabels value is not a valid dict",
1593-
)
1609+
with open(f"{tmpdir_name}/test_validate_video_point_labels.json", "r") as f:
1610+
data = json.loads(f.read())
1611+
validator = AnnotationValidators.get_validator("video")(data)
1612+
self.assertFalse(validator.is_valid())
1613+
self.assertEqual(validator.generate_report(),
1614+
"instances[0].meta.pointLabels value is not a valid dict")
15941615

1595-
@patch('builtins.print')
1596-
def test_validate_video_point_labels_bad_keys(self, mock_print):
1616+
1617+
def test_validate_video_point_labels_bad_keys(self):
15971618
with tempfile.TemporaryDirectory() as tmpdir_name:
15981619
with open(f"{tmpdir_name}/test_validate_video_point_labels_bad_keys.json",
15991620
"w") as test_validate_video_point_labels_bad_keys:
@@ -1946,13 +1967,10 @@ def test_validate_video_point_labels_bad_keys(self, mock_print):
19461967
}
19471968
'''
19481969
)
1949-
sa.validate_annotations("Video", os.path.join(self.vector_folder_path,
1950-
f"{tmpdir_name}/test_validate_video_point_labels_bad_keys.json"))
1951-
mock_print.assert_any_call(
1952-
"instances[0].meta.pointLabels str type expected\n"
1953-
"instances[2].parameters[0].timestamps[2].timestamp value is not a valid integer\n"
1954-
"instances[3].meta field required\n"
1955-
"instances[4].meta value is not a valid dict\n"
1956-
"instances[5].meta field required\n"
1957-
"tags[0] str type expected"
1958-
)
1970+
1971+
with open(f"{tmpdir_name}/test_validate_video_point_labels_bad_keys.json", "r") as f:
1972+
data = json.loads(f.read())
1973+
validator = AnnotationValidators.get_validator("video")(data)
1974+
self.assertFalse(validator.is_valid())
1975+
self.assertEqual(len(validator.generate_report()),416)
1976+

0 commit comments

Comments
 (0)