1
0

test_task.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.task._task_manager_factory import _TaskManagerFactory
  14. def test_get_task(client, default_task):
  15. # test 404
  16. user_url = url_for("api.task_by_id", task_id="foo")
  17. rep = client.get(user_url)
  18. assert rep.status_code == 404
  19. with mock.patch("taipy.core.task._task_manager._TaskManager._get") as manager_mock:
  20. manager_mock.return_value = default_task
  21. # test get_task
  22. rep = client.get(url_for("api.task_by_id", task_id="foo"))
  23. assert rep.status_code == 200
  24. def test_delete_task(client):
  25. # test 404
  26. user_url = url_for("api.task_by_id", task_id="foo")
  27. rep = client.get(user_url)
  28. assert rep.status_code == 404
  29. with (
  30. mock.patch("taipy.core.task._task_manager._TaskManager._delete"),
  31. mock.patch("taipy.core.task._task_manager._TaskManager._get"),
  32. ):
  33. # test get_task
  34. rep = client.delete(url_for("api.task_by_id", task_id="foo"))
  35. assert rep.status_code == 200
  36. def test_create_task(client, default_task_config):
  37. # without config param
  38. tasks_url = url_for("api.tasks")
  39. rep = client.post(tasks_url)
  40. assert rep.status_code == 400
  41. # config does not exist
  42. tasks_url = url_for("api.tasks", config_id="foo")
  43. rep = client.post(tasks_url)
  44. assert rep.status_code == 404
  45. with mock.patch("taipy.rest.api.resources.task.TaskList.fetch_config") as config_mock:
  46. config_mock.return_value = default_task_config
  47. tasks_url = url_for("api.tasks", config_id="bar")
  48. rep = client.post(tasks_url)
  49. assert rep.status_code == 201
  50. def test_get_all_tasks(client, task_data, default_task_config_list):
  51. for ds in range(10):
  52. with mock.patch("taipy.rest.api.resources.task.TaskList.fetch_config") as config_mock:
  53. config_mock.return_value = default_task_config_list[ds]
  54. tasks_url = url_for("api.tasks", config_id=default_task_config_list[ds].name)
  55. client.post(tasks_url)
  56. rep = client.get(tasks_url)
  57. assert rep.status_code == 200
  58. results = rep.get_json()
  59. assert len(results) == 10
  60. def test_execute_task(client, default_task):
  61. _TaskManagerFactory._build_manager()._repository._save(default_task)
  62. # test 404
  63. user_url = url_for("api.task_submit", task_id="foo")
  64. rep = client.post(user_url)
  65. assert rep.status_code == 404
  66. with mock.patch("taipy.core.task._task_manager._TaskManager._get") as manager_mock:
  67. manager_mock.return_value = default_task
  68. # test get_task
  69. rep = client.post(url_for("api.task_submit", task_id="foo"))
  70. assert rep.status_code == 200