test_context_is_promotable.py 3.0 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_core_get(entity_id):
  23. if entity_id == a_scenario.id:
  24. return a_scenario
  25. if entity_id == a_job.id:
  26. return a_job
  27. if entity_id == a_datanode.id:
  28. return a_datanode
  29. return a_task
  30. def mock_is_promotable_false(entity_id):
  31. return False
  32. def mock_is_true(entity_id):
  33. return True
  34. class MockState:
  35. def __init__(self, **kwargs) -> None:
  36. self.assign = kwargs.get("assign")
  37. class TestGuiCoreContext_is_promotable:
  38. def test_edit_entity(self):
  39. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get), patch(
  40. "taipy.gui_core._context.is_promotable", side_effect=mock_is_true
  41. ):
  42. gui_core_context = _GuiCoreContext(Mock())
  43. assign = Mock()
  44. gui_core_context.edit_entity(
  45. MockState(assign=assign),
  46. "",
  47. {
  48. "args": [
  49. {"name": "name", "id": a_scenario.id, "primary": True},
  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]).endswith("to primary because it doesn't belong to a cycle.")
  56. assign.reset_mock()
  57. with patch("taipy.gui_core._context.is_promotable", side_effect=mock_is_promotable_false):
  58. gui_core_context.edit_entity(
  59. MockState(assign=assign),
  60. "",
  61. {
  62. "args": [
  63. {"name": "name", "id": a_scenario.id, "primary": True},
  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 promotable.")