test_context_crud_scenario.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. import typing as t
  12. from unittest.mock import Mock, patch
  13. from taipy import Scope
  14. from taipy.core import DataNode, Scenario
  15. from taipy.core.data.pickle import PickleDataNode
  16. from taipy.gui.mock.mock_state import MockState
  17. from taipy.gui_core._context import _GuiCoreContext
  18. scenario_a = Scenario("scenario_a_config_id", None, {"a_prop": "a"})
  19. scenario_b = Scenario("scenario_b_config_id", None, {"a_prop": "b"})
  20. scenarios: t.List[t.Union[t.List, Scenario, None]] = [scenario_a, scenario_b]
  21. datanode_a = PickleDataNode("datanode_a_config_id", Scope.SCENARIO)
  22. datanode_b = PickleDataNode("datanode_b_config_id", Scope.SCENARIO)
  23. datanodes: t.List[t.Union[t.List, DataNode, None]] = [datanode_a, datanode_b]
  24. def mock_core_get(entity_id):
  25. if entity_id == datanode_a.id:
  26. return datanode_a
  27. if entity_id == datanode_b.id:
  28. return datanode_b
  29. return None
  30. def mock_is_true(entity_id):
  31. return True
  32. class TestGuiCoreContext_crud_scenario:
  33. def test_crud_scenario_delete(self):
  34. gui_core_context = _GuiCoreContext(Mock())
  35. state = MockState(Mock())
  36. mock_core_delete = Mock()
  37. with (
  38. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  39. patch("taipy.gui_core._context.is_deletable", side_effect=mock_is_true),
  40. patch("taipy.gui_core._context.core_delete", side_effect=mock_core_delete)
  41. ):
  42. gui_core_context.crud_scenario(
  43. state,
  44. "id",
  45. t.cast(dict, {"args": [None, None, None, True, True, {"id": "a_scenario_id"}], "error_id": "error_id"}),
  46. )
  47. mock_core_delete.assert_called_once()