pipeline_tests.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import uuid
  2. from flask import url_for
  3. from pathlib import Path
  4. from pycs.database.Model import Model
  5. from pycs.database.Project import Project
  6. from tests.base import BaseTestCase
  7. from tests.base import pаtch_tpool_execute
  8. class PipelineTests(BaseTestCase):
  9. _sleep_time = .2
  10. def setupModels(self):
  11. super().setupModels()
  12. Model.discover("tests/client/test_models")
  13. self.model = Model.query.one()
  14. self.project = Project.new(
  15. name="test_project",
  16. description="Project for a test case",
  17. model=self.model,
  18. root_folder="project_folder",
  19. external_data=False,
  20. data_folder="project_folder/data",
  21. )
  22. root = Path(self.project.root_folder)
  23. data_root = Path(self.project.data_folder)
  24. for folder in [data_root, root / "temp"]:
  25. folder.mkdir(exist_ok=True, parents=True)
  26. file_uuid = str(uuid.uuid1())
  27. self.file, is_new = self.project.add_file(
  28. uuid=file_uuid,
  29. file_type="image",
  30. name="name",
  31. filename="image",
  32. extension=".jpg",
  33. size=32*1024,
  34. )
  35. self.assertTrue(is_new)
  36. with open(self.file.absolute_path, "wb") as f:
  37. f.write(b"some content")
  38. def tearDown(self):
  39. self.wait_for_bg_jobs(raise_errors=False)
  40. self.project.delete()
  41. super().tearDown()
  42. def test_predict_file_busy(self):
  43. url = url_for("predict_file", file_id=self.file.id)
  44. self.post(url, json=dict(predict=True))
  45. self.post(url, json=dict(predict=True), status_code=400)
  46. def test_predict_file_errors(self):
  47. self.post(url_for("predict_file", file_id=4242),
  48. status_code=404)
  49. url = url_for("predict_file", file_id=self.file.id)
  50. for data in [None, dict(), dict(predict=False)]:
  51. self.post(url, status_code=400, json=data)
  52. def test_predict_file(self):
  53. url = url_for("predict_file", file_id=self.file.id)
  54. self.assertEqual(0, self.file.results.count())
  55. self.post(url, json=dict(predict=True))
  56. self.wait_for_bg_jobs()
  57. self.assertEqual(1, self.file.results.count())
  58. def test_predict_file_multiple_times(self):
  59. url = url_for("predict_file", file_id=self.file.id)
  60. self.assertEqual(0, self.file.results.count())
  61. self.post(url, json=dict(predict=True))
  62. self.wait_for_bg_jobs()
  63. self.assertEqual(1, self.file.results.count())
  64. self.post(url, json=dict(predict=True))
  65. self.wait_for_bg_jobs()
  66. self.assertEqual(1, self.file.results.count())
  67. def test_predict_model_errors(self):
  68. self.post(url_for("predict_model", project_id=4242),
  69. status_code=404)
  70. url = url_for("predict_model", project_id=self.project.id)
  71. for data in [None, dict(), dict(predict=False), dict(predict=True), dict(predict="not new or all")]:
  72. self.post(url, status_code=400, json=data)
  73. def test_predict_model_busy(self):
  74. url = url_for("predict_model", project_id=self.project.id)
  75. self.post(url, json=dict(predict="new"))
  76. self.post(url, json=dict(predict="new"), status_code=400)
  77. def test_predict_model_for_new(self):
  78. url = url_for("predict_model", project_id=self.project.id)
  79. self.post(url, json=dict(predict="new"))
  80. def test_predict_model_for_all(self):
  81. url = url_for("predict_model", project_id=self.project.id)
  82. self.post(url, json=dict(predict="all"))
  83. def test_model_fit_errors(self):
  84. self.post(url_for("fit_model", project_id=4242),
  85. status_code=404)
  86. url = url_for("fit_model", project_id=self.project.id)
  87. for data in [None, dict(), dict(fit=False)]:
  88. self.post(url, status_code=400, json=data)
  89. def test_model_fit_busy(self):
  90. url = url_for("fit_model", project_id=self.project.id)
  91. self.post(url, json=dict(fit=True))
  92. self.post(url, json=dict(fit=True), status_code=400)
  93. def test_model_fit(self):
  94. url = url_for("fit_model", project_id=self.project.id)
  95. self.post(url, json=dict(fit=True))