test_job.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from unittest import mock
  12. from flask import url_for
  13. from taipy.core._orchestrator._orchestrator_factory import _OrchestratorFactory
  14. from taipy.core.job._job_manager_factory import _JobManagerFactory
  15. def test_get_job(client, default_job):
  16. # test 404
  17. user_url = url_for("api.job_by_id", job_id="foo")
  18. rep = client.get(user_url)
  19. assert rep.status_code == 404
  20. with mock.patch("taipy.core.job._job_manager._JobManager._get") as manager_mock:
  21. manager_mock.return_value = default_job
  22. # test get_job
  23. rep = client.get(url_for("api.job_by_id", job_id="foo"))
  24. assert rep.status_code == 200
  25. def test_delete_job(client):
  26. # test 404
  27. user_url = url_for("api.job_by_id", job_id="foo")
  28. rep = client.get(user_url)
  29. assert rep.status_code == 404
  30. with (
  31. mock.patch("taipy.core.job._job_manager._JobManager._delete"),
  32. mock.patch("taipy.core.job._job_manager._JobManager._get"),
  33. ):
  34. # test get_job
  35. rep = client.delete(url_for("api.job_by_id", job_id="foo"))
  36. assert rep.status_code == 200
  37. def test_create_job(client, default_task_config):
  38. # without config param
  39. jobs_url = url_for("api.jobs")
  40. rep = client.post(jobs_url)
  41. assert rep.status_code == 400
  42. with mock.patch("taipy.rest.api.resources.job.JobList.fetch_config") as config_mock:
  43. config_mock.return_value = default_task_config
  44. jobs_url = url_for("api.jobs", task_id="foo")
  45. rep = client.post(jobs_url)
  46. assert rep.status_code == 201
  47. def test_get_all_jobs(client, create_job_list):
  48. jobs_url = url_for("api.jobs")
  49. rep = client.get(jobs_url)
  50. assert rep.status_code == 200
  51. results = rep.get_json()
  52. assert len(results) == 10
  53. def test_cancel_job(client, default_job):
  54. # test 404
  55. _OrchestratorFactory._build_orchestrator()
  56. _OrchestratorFactory._build_dispatcher()
  57. _JobManagerFactory._build_manager()._repository._save(default_job)
  58. user_url = url_for("api.job_cancel", job_id="foo")
  59. rep = client.post(user_url)
  60. assert rep.status_code == 404
  61. with mock.patch("taipy.core.job._job_manager._JobManager._get") as manager_mock:
  62. manager_mock.return_value = default_job
  63. # test get_job
  64. rep = client.post(url_for("api.job_cancel", job_id="foo"))
  65. assert rep.status_code == 200