test_task.py 3.0 KB

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