test_sequence.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. from taipy.core.scenario._scenario_manager_factory import _ScenarioManagerFactory
  14. def test_get_sequence(client, default_sequence):
  15. # test 404
  16. user_url = url_for("api.sequence_by_id", sequence_id="foo")
  17. rep = client.get(user_url)
  18. assert rep.status_code == 404
  19. with mock.patch("taipy.core.sequence._sequence_manager._SequenceManager._get") as manager_mock:
  20. manager_mock.return_value = default_sequence
  21. # test get_sequence
  22. rep = client.get(url_for("api.sequence_by_id", sequence_id="foo"))
  23. assert rep.status_code == 200
  24. def test_delete_sequence(client):
  25. # test 404
  26. user_url = url_for("api.sequence_by_id", sequence_id="foo")
  27. rep = client.get(user_url)
  28. assert rep.status_code == 404
  29. with mock.patch("taipy.core.sequence._sequence_manager._SequenceManager._delete"), mock.patch(
  30. "taipy.core.sequence._sequence_manager._SequenceManager._get"
  31. ):
  32. # test get_sequence
  33. rep = client.delete(url_for("api.sequence_by_id", sequence_id="foo"))
  34. assert rep.status_code == 200
  35. def test_create_sequence(client, default_scenario):
  36. sequences_url = url_for("api.sequences")
  37. rep = client.post(sequences_url, json={})
  38. assert rep.status_code == 400
  39. assert rep.json == {"message": "Scenario id is missing."}
  40. sequences_url = url_for("api.sequences")
  41. rep = client.post(sequences_url, json={"scenario_id": "SCENARIO_scenario_id"})
  42. assert rep.status_code == 400
  43. assert rep.json == {"message": "Sequence name is missing."}
  44. sequences_url = url_for("api.sequences")
  45. rep = client.post(sequences_url, json={"scenario_id": "SCENARIO_scenario_id", "sequence_name": "sequence"})
  46. assert rep.status_code == 404
  47. _ScenarioManagerFactory._build_manager()._set(default_scenario)
  48. with mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._get") as config_mock:
  49. config_mock.return_value = default_scenario
  50. sequences_url = url_for("api.sequences")
  51. rep = client.post(
  52. sequences_url, json={"scenario_id": default_scenario.id, "sequence_name": "sequence", "tasks": []}
  53. )
  54. assert rep.status_code == 201
  55. def test_get_all_sequences(client, default_scenario_config_list):
  56. for ds in range(10):
  57. with mock.patch("taipy.rest.api.resources.scenario.ScenarioList.fetch_config") as config_mock:
  58. config_mock.return_value = default_scenario_config_list[ds]
  59. scenario_url = url_for("api.scenarios", config_id=config_mock.name)
  60. client.post(scenario_url)
  61. sequences_url = url_for("api.sequences")
  62. rep = client.get(sequences_url)
  63. assert rep.status_code == 200
  64. results = rep.get_json()
  65. assert len(results) == 10
  66. def test_execute_sequence(client, default_scenario):
  67. # test 404
  68. user_url = url_for("api.sequence_submit", sequence_id="foo")
  69. rep = client.post(user_url)
  70. assert rep.status_code == 404
  71. _ScenarioManagerFactory._build_manager()._set(default_scenario)
  72. with mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._get") as config_mock:
  73. config_mock.return_value = default_scenario
  74. sequences_url = url_for("api.sequences")
  75. seq = client.post(
  76. sequences_url, json={"scenario_id": default_scenario.id, "sequence_name": "sequence", "tasks": []}
  77. )
  78. # test submit
  79. rep = client.post(url_for("api.sequence_submit", sequence_id=seq.json["sequence"]["id"]))
  80. assert rep.status_code == 200