test_context_is_submitable.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.mock import Mock, patch
  12. from taipy.config.common.scope import Scope
  13. from taipy.core import Job, JobId, Scenario, Task
  14. from taipy.core.data.pickle import PickleDataNode
  15. from taipy.gui_core._context import _GuiCoreContext
  16. a_scenario = Scenario("scenario_config_id", None, {}, sequences={"sequence": {}})
  17. a_task = Task("task_config_id", {}, print)
  18. a_job = Job(JobId("JOB_job_id"), a_task, "submit_id", a_scenario.id)
  19. a_job.isfinished = lambda s: True # type: ignore[attr-defined]
  20. a_datanode = PickleDataNode("data_node_config_id", Scope.SCENARIO)
  21. def mock_is_submittable_false(entity_id):
  22. return False
  23. def mock_is_true(entity_id):
  24. return True
  25. def mock_core_get(entity_id):
  26. if entity_id == a_scenario.id:
  27. return a_scenario
  28. if entity_id == a_job.id:
  29. return a_job
  30. if entity_id == a_datanode.id:
  31. return a_datanode
  32. return a_task
  33. class MockState:
  34. def __init__(self, **kwargs) -> None:
  35. self.assign = kwargs.get("assign")
  36. class TestGuiCoreContext_is_submittable:
  37. def test_submit_entity(self):
  38. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get), patch(
  39. "taipy.gui_core._context.is_submittable", side_effect=mock_is_true
  40. ):
  41. gui_core_context = _GuiCoreContext(Mock())
  42. assign = Mock()
  43. gui_core_context.submit_entity(
  44. MockState(assign=assign),
  45. "",
  46. {
  47. "args": [
  48. {"name": "name", "id": a_scenario.id},
  49. ]
  50. },
  51. )
  52. assign.assert_called_once()
  53. assert assign.call_args.args[0] == "gui_core_sv_error"
  54. assert str(assign.call_args.args[1]).startswith("Error submitting entity.")
  55. with patch("taipy.gui_core._context.is_submittable", side_effect=mock_is_submittable_false):
  56. assign.reset_mock()
  57. gui_core_context.submit_entity(
  58. MockState(assign=assign),
  59. "",
  60. {
  61. "args": [
  62. {"name": "name", "id": a_scenario.id},
  63. ]
  64. },
  65. )
  66. assign.assert_called_once()
  67. assert assign.call_args.args[0] == "gui_core_sv_error"
  68. assert str(assign.call_args.args[1]).endswith("is not submittable.")