test_context_is_editable.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 import Gui
  21. from taipy.gui_core._context import _GuiCoreContext
  22. a_scenario = Scenario("scenario_config_id", None, {}, sequences={"sequence": {}})
  23. a_task = Task("task_config_id", {}, print)
  24. a_job = Job(JobId("JOB_job_id"), a_task, "submit_id", a_scenario.id)
  25. a_job.isfinished = lambda s: True # type: ignore[attr-defined]
  26. a_datanode = PickleDataNode("data_node_config_id", Scope.SCENARIO)
  27. def mock_core_get(entity_id):
  28. if entity_id == a_scenario.id:
  29. return a_scenario
  30. if entity_id == a_job.id:
  31. return a_job
  32. if entity_id == a_datanode.id:
  33. return a_datanode
  34. return a_task
  35. def mock_is_editable_false(entity_id):
  36. return False
  37. def mock_is_true(entity_id):
  38. return True
  39. class MockState:
  40. def __init__(self, **kwargs) -> None:
  41. self.assign = kwargs.get("assign")
  42. class TestGuiCoreContext_is_editable:
  43. @pytest.fixture(scope="class", autouse=True)
  44. def set_entity(self):
  45. _ScenarioManagerFactory._build_manager()._repository._save(a_scenario)
  46. _TaskManagerFactory._build_manager()._repository._save(a_task)
  47. _JobManagerFactory._build_manager()._repository._save(a_job)
  48. _DataManagerFactory._build_manager()._repository._save(a_datanode)
  49. def test_crud_scenario(self):
  50. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  51. gui_core_context = _GuiCoreContext(Mock())
  52. assign = Mock()
  53. gui_core_context.crud_scenario(
  54. MockState(assign=assign),
  55. "",
  56. {
  57. "args": [
  58. "",
  59. "",
  60. "",
  61. True,
  62. False,
  63. {"name": "name", "id": a_scenario.id},
  64. ],
  65. "error_id": "error_var",
  66. },
  67. )
  68. assign.assert_not_called()
  69. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  70. assign.reset_mock()
  71. gui_core_context.crud_scenario(
  72. MockState(assign=assign),
  73. "",
  74. {
  75. "args": [
  76. "",
  77. "",
  78. "",
  79. True,
  80. False,
  81. {"name": "name", "id": a_scenario.id},
  82. ],
  83. "error_id": "error_var",
  84. },
  85. )
  86. assign.assert_called_once()
  87. assert assign.call_args.args[0] == "error_var"
  88. assert "is not editable" in str(assign.call_args.args[1])
  89. def test_edit_entity(self):
  90. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  91. gui_core_context = _GuiCoreContext(Mock())
  92. assign = Mock()
  93. gui_core_context.edit_entity(
  94. MockState(assign=assign),
  95. "",
  96. {
  97. "args": [
  98. {"name": "name", "id": a_scenario.id},
  99. ],
  100. "error_id": "error_var",
  101. },
  102. )
  103. assign.assert_called_once()
  104. assert assign.call_args.args[0] == "error_var"
  105. assert assign.call_args.args[1] == ""
  106. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  107. assign.reset_mock()
  108. gui_core_context.edit_entity(
  109. MockState(assign=assign),
  110. "",
  111. {
  112. "args": [
  113. {"name": "name", "id": a_scenario.id},
  114. ],
  115. "error_id": "error_var",
  116. },
  117. )
  118. assign.assert_called_once()
  119. assert assign.call_args.args[0] == "error_var"
  120. assert "is not editable" in str(assign.call_args.args[1])
  121. def test_act_on_jobs(self):
  122. with (
  123. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  124. patch("taipy.gui_core._context.is_deletable", side_effect=mock_is_true),
  125. ):
  126. gui_core_context = _GuiCoreContext(Mock())
  127. assign = Mock()
  128. gui_core_context.act_on_jobs(
  129. MockState(assign=assign),
  130. "",
  131. {
  132. "args": [
  133. {"id": [a_job.id], "action": "cancel"},
  134. ],
  135. "error_id": "error_var",
  136. },
  137. )
  138. assign.assert_called_once()
  139. assert assign.call_args.args[0] == "error_var"
  140. assert "is not editable" not in assign.call_args.args[1]
  141. assign.reset_mock()
  142. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_editable_false):
  143. gui_core_context.act_on_jobs(
  144. MockState(assign=assign),
  145. "",
  146. {
  147. "args": [
  148. {"id": [a_job.id], "action": "cancel"},
  149. ],
  150. "error_id": "error_var",
  151. },
  152. )
  153. assign.assert_called_once()
  154. assert assign.call_args.args[0] == "error_var"
  155. assert "is not readable" in assign.call_args.args[1]
  156. def test_edit_data_node(self):
  157. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  158. gui_core_context = _GuiCoreContext(Mock())
  159. assign = Mock()
  160. gui_core_context.edit_data_node(
  161. MockState(assign=assign),
  162. "",
  163. {
  164. "args": [
  165. {"id": a_datanode.id},
  166. ],
  167. "error_id": "error_var",
  168. },
  169. )
  170. assign.assert_called_once()
  171. assert assign.call_args.args[0] == "error_var"
  172. assert assign.call_args.args[1] == ""
  173. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  174. assign.reset_mock()
  175. gui_core_context.edit_data_node(
  176. MockState(assign=assign),
  177. "",
  178. {
  179. "args": [
  180. {"id": a_datanode.id},
  181. ],
  182. "error_id": "error_var",
  183. },
  184. )
  185. assign.assert_called_once()
  186. assert assign.call_args.args[0] == "error_var"
  187. assert "is not editable" in assign.call_args.args[1]
  188. def test_lock_datanode_for_edit(self):
  189. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  190. mockGui = Mock(Gui)
  191. mockGui._get_client_id = lambda: "a_client_id"
  192. gui_core_context = _GuiCoreContext(mockGui)
  193. assign = Mock()
  194. gui_core_context.lock_datanode_for_edit(
  195. MockState(assign=assign),
  196. "",
  197. {
  198. "args": [
  199. {"id": a_datanode.id},
  200. ],
  201. "error_id": "error_var",
  202. },
  203. )
  204. assign.assert_called_once()
  205. assert assign.call_args.args[0] == "error_var"
  206. assert assign.call_args.args[1] == ""
  207. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  208. assign.reset_mock()
  209. gui_core_context.lock_datanode_for_edit(
  210. MockState(assign=assign),
  211. "",
  212. {
  213. "args": [
  214. {"id": a_datanode.id},
  215. ],
  216. "error_id": "error_var",
  217. },
  218. )
  219. assign.assert_called_once()
  220. assert assign.call_args.args[0] == "error_var"
  221. assert "is not editable" in assign.call_args.args[1]
  222. def test_update_data(self):
  223. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  224. mockGui = Mock(Gui)
  225. mockGui._get_client_id = lambda: "a_client_id"
  226. gui_core_context = _GuiCoreContext(mockGui)
  227. assign = Mock()
  228. gui_core_context.update_data(
  229. MockState(assign=assign),
  230. "",
  231. {
  232. "args": [{"id": a_datanode.id, "error_id": "error_var"}],
  233. },
  234. )
  235. assign.assert_called()
  236. assert assign.call_args_list[0].args[0] == "error_var"
  237. assert assign.call_args_list[0].args[1] == ""
  238. assign.reset_mock()
  239. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  240. gui_core_context.update_data(
  241. MockState(assign=assign),
  242. "",
  243. {"args": [{"id": a_datanode.id, "error_id": "error_var"}]},
  244. )
  245. assign.assert_called_once()
  246. assert assign.call_args.args[0] == "error_var"
  247. assert "is not editable" in assign.call_args.args[1]
  248. def test_tabular_data_edit(self):
  249. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  250. mockGui = Mock(Gui)
  251. mockGui._get_client_id = lambda: "a_client_id"
  252. gui_core_context = _GuiCoreContext(mockGui)
  253. assign = Mock()
  254. gui_core_context.tabular_data_edit(
  255. MockState(assign=assign),
  256. "",
  257. {
  258. "user_data": {"dn_id": a_datanode.id},
  259. "error_id": "error_var",
  260. },
  261. )
  262. assign.assert_called_once()
  263. assert assign.call_args_list[0].args[0] == "error_var"
  264. assert "tabular value: type does not support at[] indexer" in assign.call_args_list[0].args[1]
  265. assign.reset_mock()
  266. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  267. gui_core_context.tabular_data_edit(
  268. MockState(assign=assign),
  269. "",
  270. {
  271. "user_data": {"dn_id": a_datanode.id},
  272. "error_id": "error_var",
  273. },
  274. )
  275. assign.assert_called_once()
  276. assert assign.call_args.args[0] == "error_var"
  277. assert "is not editable" in assign.call_args.args[1]