test_scenario.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright 2023 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. import pytest
  13. from flask import url_for
  14. def test_get_scenario(client, default_scenario):
  15. # test 404
  16. user_url = url_for("api.scenario_by_id", scenario_id="foo")
  17. rep = client.get(user_url)
  18. assert rep.status_code == 404
  19. with mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._get") as manager_mock:
  20. manager_mock.return_value = default_scenario
  21. # test get_scenario
  22. rep = client.get(url_for("api.scenario_by_id", scenario_id="foo"))
  23. assert rep.status_code == 200
  24. def test_delete_scenario(client):
  25. # test 404
  26. user_url = url_for("api.scenario_by_id", scenario_id="foo")
  27. rep = client.get(user_url)
  28. assert rep.status_code == 404
  29. with mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._delete"), mock.patch(
  30. "taipy.core.scenario._scenario_manager._ScenarioManager._get"
  31. ):
  32. # test get_scenario
  33. rep = client.delete(url_for("api.scenario_by_id", scenario_id="foo"))
  34. assert rep.status_code == 200
  35. def test_create_scenario(client, default_scenario_config):
  36. # without config param
  37. scenarios_url = url_for("api.scenarios")
  38. rep = client.post(scenarios_url)
  39. assert rep.status_code == 400
  40. # config does not exist
  41. scenarios_url = url_for("api.scenarios", config_id="foo")
  42. rep = client.post(scenarios_url)
  43. assert rep.status_code == 404
  44. with mock.patch("src.taipy.rest.api.resources.scenario.ScenarioList.fetch_config") as config_mock:
  45. config_mock.return_value = default_scenario_config
  46. scenarios_url = url_for("api.scenarios", config_id="bar")
  47. rep = client.post(scenarios_url)
  48. assert rep.status_code == 201
  49. def test_get_all_scenarios(client, default_sequence, default_scenario_config_list):
  50. for ds in range(10):
  51. with mock.patch("src.taipy.rest.api.resources.scenario.ScenarioList.fetch_config") as config_mock:
  52. config_mock.return_value = default_scenario_config_list[ds]
  53. scenarios_url = url_for("api.scenarios", config_id=config_mock.name)
  54. client.post(scenarios_url)
  55. rep = client.get(scenarios_url)
  56. assert rep.status_code == 200
  57. results = rep.get_json()
  58. assert len(results) == 10
  59. @pytest.mark.xfail()
  60. def test_execute_scenario(client, default_scenario):
  61. # test 404
  62. user_url = url_for("api.scenario_submit", scenario_id="foo")
  63. rep = client.post(user_url)
  64. assert rep.status_code == 404
  65. with mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._get") as manager_mock:
  66. manager_mock.return_value = default_scenario
  67. # test get_scenario
  68. rep = client.post(url_for("api.scenario_submit", scenario_id="foo"))
  69. assert rep.status_code == 200