test_context_is_deletable.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. from taipy 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_core_get(entity_id):
  22. if entity_id == a_scenario.id:
  23. return a_scenario
  24. if entity_id == a_job.id:
  25. return a_job
  26. if entity_id == a_datanode.id:
  27. return a_datanode
  28. return a_task
  29. def mock_is_deletable_false(entity_id):
  30. return False
  31. def mock_is_true(entity_id):
  32. return True
  33. class MockState:
  34. def __init__(self, **kwargs) -> None:
  35. self.assign = kwargs.get("assign")
  36. class TestGuiCoreContext_is_deletable:
  37. def test_crud_scenario(self):
  38. with (
  39. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  40. patch("taipy.gui_core._context.is_deletable", side_effect=mock_is_true),
  41. ):
  42. gui_core_context = _GuiCoreContext(Mock())
  43. assign = Mock()
  44. gui_core_context.crud_scenario(
  45. MockState(assign=assign),
  46. "",
  47. {
  48. "args": [
  49. "",
  50. "",
  51. "",
  52. True,
  53. True,
  54. {"name": "name", "id": a_scenario.id},
  55. ],
  56. "error_id": "error_var",
  57. },
  58. )
  59. assign.assert_called_once()
  60. assert assign.call_args.args[0] == "error_var"
  61. assert str(assign.call_args.args[1]).startswith("Error deleting Scenario.")
  62. with patch("taipy.gui_core._context.is_deletable", side_effect=mock_is_deletable_false):
  63. assign.reset_mock()
  64. gui_core_context.crud_scenario(
  65. MockState(assign=assign),
  66. "",
  67. {
  68. "args": [
  69. "",
  70. "",
  71. "",
  72. True,
  73. True,
  74. {"name": "name", "id": a_scenario.id},
  75. ],
  76. "error_id": "error_var",
  77. },
  78. )
  79. assign.assert_called_once()
  80. assert assign.call_args.args[0] == "error_var"
  81. assert "is not deletable" in str(assign.call_args.args[1])
  82. def test_act_on_jobs(self):
  83. with (
  84. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  85. patch("taipy.gui_core._context.is_deletable", side_effect=mock_is_true),
  86. ):
  87. gui_core_context = _GuiCoreContext(Mock())
  88. assign = Mock()
  89. gui_core_context.act_on_jobs(
  90. MockState(assign=assign),
  91. "",
  92. {
  93. "args": [
  94. {"id": [a_job.id], "action": "delete"},
  95. ],
  96. "error_id": "error_var",
  97. },
  98. )
  99. assign.assert_called_once()
  100. assert assign.call_args.args[0] == "error_var"
  101. assert str(assign.call_args.args[1]).find("is not deletable.") == -1
  102. assign.reset_mock()
  103. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_deletable_false):
  104. gui_core_context.act_on_jobs(
  105. MockState(assign=assign),
  106. "",
  107. {
  108. "args": [
  109. {"id": [a_job.id], "action": "delete"},
  110. ],
  111. "error_id": "error_var",
  112. },
  113. )
  114. assign.assert_called_once()
  115. assert assign.call_args.args[0] == "error_var"
  116. assert "is not readable" in str(assign.call_args.args[1])