import uuid from flask import url_for from pycs.database.Label import Label from pycs.database.Result import Result from tests.client.file_tests import _BaseFileTests class _BaseResultTests(_BaseFileTests): def setupModels(self): super().setupModels() file_uuid = str(uuid.uuid1()) self.file, is_new = self.project.add_file( uuid=file_uuid, file_type="image", name=f"name", filename=f"image", extension=".jpg", size=32*1024, ) assert is_new, "the created file should be new!" class ResultCreationTests(_BaseResultTests): def test_missing_file(self): url = url_for("create_result", file_id=4242) self.post(url, status_code=404) def test_missing_flags(self): url = url_for("create_result", file_id=self.file.id) bad_data = [ None, # no request data at all dict(), # type missing dict(type="something"), # should be "labeled-image" or "bounding-box" dict(type="labeled_image"), # should be with "-" dict(type="bounding_box"), # should be with "-" dict(type="labeled-image"), # label is missing dict(type="bounding-box"), # data is missing ] for data in bad_data: self.assertEqual(0, Result.query.count()) self.post(url, status_code=400, json=data) self.assertEqual(0, Result.query.count()) def test_file_label(self): url = url_for("create_result", file_id=self.file.id) label, is_new = self.project.create_label(name="label", reference="some_label") self.assertTrue(is_new) self.assertEqual(0, Result.query.count()) self.post(url, json=dict(type="labeled-image", label=label.id)) self.assertEqual(1, Result.query.count()) result = Result.query.one_or_none() self.assertIsNotNone(result) self.assertEqual("user", result.origin) self.assertEqual(self.file.id, result.file_id) self.assertEqual(label.id, result.label_id) self.assertEqual(label.name, result.label.name) self.assertIsNone(result.data_encoded) self.assertIsNone(result.data) def test_bounding_box(self): url = url_for("create_result", file_id=self.file.id) self.assertEqual(0, Result.query.count()) box = dict(x=0, y=0.5, w=1/3, h=1/4) self.post(url, json=dict(type="bounding-box", data=box)) self.assertEqual(1, Result.query.count()) result = Result.query.one_or_none() self.assertIsNotNone(result) self.assertEqual("user", result.origin) self.assertEqual(self.file.id, result.file_id) self.assertIsNotNone(result.data_encoded) self.assertDictEqual(box, result.data) self.assertIsNone(result.label_id) class ResultGettingTests(_BaseResultTests): def test_missing_file(self): url = url_for("get_results", file_id=4242) self.get(url, status_code=404) def test_getting_of_results(self): n = 5 self.assertEqual(0, Result.query.count()) results = {} for i in range(n): box = dict(x=0, y=0, w=0.9, h=1.0) res = self.file.create_result("user", "bounding-box", data=box) results[res.id] = res self.assertEqual(5, Result.query.count()) file_uuid = str(uuid.uuid1()) another_file, is_new = self.project.add_file( uuid=file_uuid, file_type="image", name=f"name2", filename=f"image2", extension=".jpg", size=32*1024, ) self.assertTrue(is_new) for i in range(n): box = dict(x=0, y=0, w=0.9, h=1.0) another_file.create_result("user", "bounding-box", data=box) self.assertEqual(10, Result.query.count()) url = url_for("get_results", file_id=self.file.id) response = self.get(url) self.assertTrue(response.is_json) content = response.json self.assertEqual(5, len(content)) for entry in content: res = results[entry["id"]] self.assertDictEqual(res.serialize(), entry)