result_tests.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import uuid
  2. from flask import url_for
  3. from pycs.database.Label import Label
  4. from pycs.database.Result import Result
  5. from tests.client.file_tests import _BaseFileTests
  6. class _BaseResultTests(_BaseFileTests):
  7. def setupModels(self):
  8. super().setupModels()
  9. file_uuid = str(uuid.uuid1())
  10. self.file, is_new = self.project.add_file(
  11. uuid=file_uuid,
  12. file_type="image",
  13. name=f"name",
  14. filename=f"image",
  15. extension=".jpg",
  16. size=32*1024,
  17. )
  18. assert is_new, "the created file should be new!"
  19. class ResultCreationTests(_BaseResultTests):
  20. def test_missing_file(self):
  21. url = url_for("create_result", file_id=4242)
  22. self.post(url, status_code=404)
  23. def test_missing_flags(self):
  24. url = url_for("create_result", file_id=self.file.id)
  25. bad_data = [
  26. None, # no request data at all
  27. dict(), # type missing
  28. dict(type="something"), # should be "labeled-image" or "bounding-box"
  29. dict(type="labeled_image"), # should be with "-"
  30. dict(type="bounding_box"), # should be with "-"
  31. dict(type="labeled-image"), # label is missing
  32. dict(type="bounding-box"), # data is missing
  33. ]
  34. for data in bad_data:
  35. self.assertEqual(0, Result.query.count())
  36. self.post(url, status_code=400, json=data)
  37. self.assertEqual(0, Result.query.count())
  38. def test_file_label(self):
  39. url = url_for("create_result", file_id=self.file.id)
  40. label, is_new = self.project.create_label(name="label", reference="some_label")
  41. self.assertTrue(is_new)
  42. self.assertEqual(0, Result.query.count())
  43. self.post(url, json=dict(type="labeled-image", label=label.id))
  44. self.assertEqual(1, Result.query.count())
  45. result = Result.query.one_or_none()
  46. self.assertIsNotNone(result)
  47. self.assertEqual("user", result.origin)
  48. self.assertEqual(self.file.id, result.file_id)
  49. self.assertEqual(label.id, result.label_id)
  50. self.assertEqual(label.name, result.label.name)
  51. self.assertIsNone(result.data_encoded)
  52. self.assertIsNone(result.data)
  53. def test_bounding_box(self):
  54. url = url_for("create_result", file_id=self.file.id)
  55. self.assertEqual(0, Result.query.count())
  56. box = dict(x=0, y=0.5, w=1/3, h=1/4)
  57. self.post(url, json=dict(type="bounding-box", data=box))
  58. self.assertEqual(1, Result.query.count())
  59. result = Result.query.one_or_none()
  60. self.assertIsNotNone(result)
  61. self.assertEqual("user", result.origin)
  62. self.assertEqual(self.file.id, result.file_id)
  63. self.assertIsNotNone(result.data_encoded)
  64. self.assertDictEqual(box, result.data)
  65. self.assertIsNone(result.label_id)
  66. class ResultGettingTests(_BaseResultTests):
  67. def test_missing_file(self):
  68. url = url_for("get_results", file_id=4242)
  69. self.get(url, status_code=404)
  70. def test_getting_of_results(self):
  71. n = 5
  72. self.assertEqual(0, Result.query.count())
  73. results = {}
  74. for i in range(n):
  75. box = dict(x=0, y=0, w=0.9, h=1.0)
  76. res = self.file.create_result("user", "bounding-box", data=box)
  77. results[res.id] = res
  78. self.assertEqual(5, Result.query.count())
  79. file_uuid = str(uuid.uuid1())
  80. another_file, is_new = self.project.add_file(
  81. uuid=file_uuid,
  82. file_type="image",
  83. name=f"name2",
  84. filename=f"image2",
  85. extension=".jpg",
  86. size=32*1024,
  87. )
  88. self.assertTrue(is_new)
  89. for i in range(n):
  90. box = dict(x=0, y=0, w=0.9, h=1.0)
  91. another_file.create_result("user", "bounding-box", data=box)
  92. self.assertEqual(10, Result.query.count())
  93. url = url_for("get_results", file_id=self.file.id)
  94. response = self.get(url)
  95. self.assertTrue(response.is_json)
  96. content = response.json
  97. self.assertEqual(5, len(content))
  98. for entry in content:
  99. res = results[entry["id"]]
  100. self.assertDictEqual(res.serialize(), entry)