test_context_is_submitable.py 2.9 KB

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