test_context_is_promotable.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2021-2025 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 import Scope
  14. from taipy.core import Job, JobId, Scenario, Task
  15. from taipy.core.data._data_manager_factory import _DataManagerFactory
  16. from taipy.core.data.pickle import PickleDataNode
  17. from taipy.core.job._job_manager_factory import _JobManagerFactory
  18. from taipy.core.scenario._scenario_manager_factory import _ScenarioManagerFactory
  19. from taipy.core.task._task_manager_factory import _TaskManagerFactory
  20. from taipy.gui_core._context import _GuiCoreContext
  21. a_scenario = Scenario("scenario_config_id", None, {}, sequences={"sequence": {}})
  22. a_task = Task("task_config_id", {}, print)
  23. a_job = Job(JobId("JOB_job_id"), a_task, "submit_id", a_scenario.id)
  24. a_job.isfinished = lambda s: True # type: ignore[attr-defined]
  25. a_datanode = PickleDataNode("data_node_config_id", Scope.SCENARIO)
  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. def mock_is_promotable_false(entity_id):
  35. return False
  36. def mock_is_true(entity_id):
  37. return True
  38. class MockState:
  39. def __init__(self, **kwargs) -> None:
  40. self.assign = kwargs.get("assign")
  41. class TestGuiCoreContext_is_promotable:
  42. @pytest.fixture(scope="class", autouse=True)
  43. def set_entity(self):
  44. _ScenarioManagerFactory._build_manager()._repository._save(a_scenario)
  45. _TaskManagerFactory._build_manager()._repository._save(a_task)
  46. _JobManagerFactory._build_manager()._repository._save(a_job)
  47. _DataManagerFactory._build_manager()._repository._save(a_datanode)
  48. def test_edit_entity(self):
  49. with (
  50. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  51. patch("taipy.gui_core._context.is_promotable", side_effect=mock_is_true),
  52. ):
  53. gui_core_context = _GuiCoreContext(Mock())
  54. assign = Mock()
  55. gui_core_context.edit_entity(
  56. MockState(assign=assign),
  57. "",
  58. {
  59. "args": [
  60. {"name": "name", "id": a_scenario.id, "primary": True},
  61. ],
  62. "error_id": "error_var",
  63. },
  64. )
  65. assign.assert_called_once()
  66. assert assign.call_args.args[0] == "error_var"
  67. assert "to primary because it doesn't belong to a cycle" in assign.call_args.args[1]
  68. assign.reset_mock()
  69. with patch("taipy.gui_core._context.is_promotable", side_effect=mock_is_promotable_false):
  70. gui_core_context.edit_entity(
  71. MockState(assign=assign),
  72. "",
  73. {
  74. "args": [
  75. {"name": "name", "id": a_scenario.id, "primary": True},
  76. ],
  77. "error_id": "error_var",
  78. },
  79. )
  80. assign.assert_called_once()
  81. assert assign.call_args.args[0] == "error_var"
  82. assert "is not promotable" in assign.call_args.args[1]