test_job.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2021-2024 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. def test_get_job(client, default_job):
  14. # test 404
  15. user_url = url_for("api.job_by_id", job_id="foo")
  16. rep = client.get(user_url)
  17. assert rep.status_code == 404
  18. with mock.patch("taipy.core.job._job_manager._JobManager._get") as manager_mock:
  19. manager_mock.return_value = default_job
  20. # test get_job
  21. rep = client.get(url_for("api.job_by_id", job_id="foo"))
  22. assert rep.status_code == 200
  23. def test_delete_job(client):
  24. # test 404
  25. user_url = url_for("api.job_by_id", job_id="foo")
  26. rep = client.get(user_url)
  27. assert rep.status_code == 404
  28. with mock.patch("taipy.core.job._job_manager._JobManager._delete"), mock.patch(
  29. "taipy.core.job._job_manager._JobManager._get"
  30. ):
  31. # test get_job
  32. rep = client.delete(url_for("api.job_by_id", job_id="foo"))
  33. assert rep.status_code == 200
  34. def test_create_job(client, default_task_config):
  35. # without config param
  36. jobs_url = url_for("api.jobs")
  37. rep = client.post(jobs_url)
  38. assert rep.status_code == 400
  39. with mock.patch("taipy.rest.api.resources.job.JobList.fetch_config") as config_mock:
  40. config_mock.return_value = default_task_config
  41. jobs_url = url_for("api.jobs", task_id="foo")
  42. rep = client.post(jobs_url)
  43. assert rep.status_code == 201
  44. def test_get_all_jobs(client, create_job_list):
  45. jobs_url = url_for("api.jobs")
  46. rep = client.get(jobs_url)
  47. assert rep.status_code == 200
  48. results = rep.get_json()
  49. assert len(results) == 10
  50. def test_cancel_job(client, default_job):
  51. # test 404
  52. from taipy.core._orchestrator._orchestrator_factory import _OrchestratorFactory
  53. _OrchestratorFactory._build_orchestrator()
  54. _OrchestratorFactory._build_dispatcher()
  55. user_url = url_for("api.job_cancel", job_id="foo")
  56. rep = client.post(user_url)
  57. assert rep.status_code == 404
  58. with mock.patch("taipy.core.job._job_manager._JobManager._get") as manager_mock:
  59. manager_mock.return_value = default_job
  60. # test get_job
  61. rep = client.post(url_for("api.job_cancel", job_id="foo"))
  62. assert rep.status_code == 200