test_context_is_editable.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright 2021-2024 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.config.common.scope import Scope
  13. from taipy.core import Job, JobId, Scenario, Task
  14. from taipy.core.data.pickle import PickleDataNode
  15. from taipy.gui import Gui
  16. from taipy.gui_core._context import _GuiCoreContext
  17. a_scenario = Scenario("scenario_config_id", None, {}, sequences={"sequence": {}})
  18. a_task = Task("task_config_id", {}, print)
  19. a_job = Job(JobId("JOB_job_id"), a_task, "submit_id", a_scenario.id)
  20. a_job.isfinished = lambda s: True # type: ignore[attr-defined]
  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_editable_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_editable:
  38. def test_crud_scenario(self):
  39. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  40. gui_core_context = _GuiCoreContext(Mock())
  41. assign = Mock()
  42. gui_core_context.crud_scenario(
  43. MockState(assign=assign),
  44. "",
  45. {
  46. "args": [
  47. True,
  48. False,
  49. {"name": "name", "id": a_scenario.id},
  50. ]
  51. },
  52. )
  53. assign.assert_not_called()
  54. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  55. assign.reset_mock()
  56. gui_core_context.crud_scenario(
  57. MockState(assign=assign),
  58. "",
  59. {
  60. "args": [
  61. True,
  62. False,
  63. {"name": "name", "id": a_scenario.id},
  64. ]
  65. },
  66. )
  67. assign.assert_called_once()
  68. assert assign.call_args.args[0] == "gui_core_sc_error"
  69. assert str(assign.call_args.args[1]).endswith("is not editable.")
  70. def test_edit_entity(self):
  71. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  72. gui_core_context = _GuiCoreContext(Mock())
  73. assign = Mock()
  74. gui_core_context.edit_entity(
  75. MockState(assign=assign),
  76. "",
  77. {
  78. "args": [
  79. {"name": "name", "id": a_scenario.id},
  80. ]
  81. },
  82. )
  83. assign.assert_called_once()
  84. assert assign.call_args.args[0] == "gui_core_sv_error"
  85. assert assign.call_args.args[1] == ""
  86. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  87. assign.reset_mock()
  88. gui_core_context.edit_entity(
  89. MockState(assign=assign),
  90. "",
  91. {
  92. "args": [
  93. {"name": "name", "id": a_scenario.id},
  94. ]
  95. },
  96. )
  97. assign.assert_called_once()
  98. assert assign.call_args.args[0] == "gui_core_sv_error"
  99. assert str(assign.call_args.args[1]).endswith("is not editable.")
  100. def test_act_on_jobs(self):
  101. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get), patch(
  102. "taipy.gui_core._context.is_deletable", side_effect=mock_is_true
  103. ):
  104. gui_core_context = _GuiCoreContext(Mock())
  105. assign = Mock()
  106. gui_core_context.act_on_jobs(
  107. MockState(assign=assign),
  108. "",
  109. {
  110. "args": [
  111. {"id": [a_job.id], "action": "cancel"},
  112. ]
  113. },
  114. )
  115. assign.assert_called_once()
  116. assert assign.call_args.args[0] == "gui_core_js_error"
  117. assert str(assign.call_args.args[1]).find("is not editable.") == -1
  118. assign.reset_mock()
  119. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_editable_false):
  120. gui_core_context.act_on_jobs(
  121. MockState(assign=assign),
  122. "",
  123. {
  124. "args": [
  125. {"id": [a_job.id], "action": "cancel"},
  126. ]
  127. },
  128. )
  129. assign.assert_called_once()
  130. assert assign.call_args.args[0] == "gui_core_js_error"
  131. assert str(assign.call_args.args[1]).endswith("is not readable.")
  132. def test_edit_data_node(self):
  133. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  134. gui_core_context = _GuiCoreContext(Mock())
  135. assign = Mock()
  136. gui_core_context.edit_data_node(
  137. MockState(assign=assign),
  138. "",
  139. {
  140. "args": [
  141. {"id": a_datanode.id},
  142. ]
  143. },
  144. )
  145. assign.assert_called_once()
  146. assert assign.call_args.args[0] == "gui_core_dv_error"
  147. assert assign.call_args.args[1] == ""
  148. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  149. assign.reset_mock()
  150. gui_core_context.edit_data_node(
  151. MockState(assign=assign),
  152. "",
  153. {
  154. "args": [
  155. {"id": a_datanode.id},
  156. ]
  157. },
  158. )
  159. assign.assert_called_once()
  160. assert assign.call_args.args[0] == "gui_core_dv_error"
  161. assert str(assign.call_args.args[1]).endswith("is not editable.")
  162. def test_lock_datanode_for_edit(self):
  163. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  164. mockGui = Mock(Gui)
  165. mockGui._get_client_id = lambda: "a_client_id"
  166. gui_core_context = _GuiCoreContext(mockGui)
  167. assign = Mock()
  168. gui_core_context.lock_datanode_for_edit(
  169. MockState(assign=assign),
  170. "",
  171. {
  172. "args": [
  173. {"id": a_datanode.id},
  174. ]
  175. },
  176. )
  177. assign.assert_called_once()
  178. assert assign.call_args.args[0] == "gui_core_dv_error"
  179. assert assign.call_args.args[1] == ""
  180. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  181. assign.reset_mock()
  182. gui_core_context.lock_datanode_for_edit(
  183. MockState(assign=assign),
  184. "",
  185. {
  186. "args": [
  187. {"id": a_datanode.id},
  188. ]
  189. },
  190. )
  191. assign.assert_called_once()
  192. assert assign.call_args.args[0] == "gui_core_dv_error"
  193. assert str(assign.call_args.args[1]).endswith("is not editable.")
  194. def test_update_data(self):
  195. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  196. mockGui = Mock(Gui)
  197. mockGui._get_client_id = lambda: "a_client_id"
  198. gui_core_context = _GuiCoreContext(mockGui)
  199. assign = Mock()
  200. gui_core_context.update_data(
  201. MockState(assign=assign),
  202. "",
  203. {
  204. "args": [
  205. {"id": a_datanode.id},
  206. ]
  207. },
  208. )
  209. assign.assert_called()
  210. assert assign.call_args_list[0].args[0] == "gui_core_dv_error"
  211. assert assign.call_args_list[0].args[1] == ""
  212. assign.reset_mock()
  213. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  214. gui_core_context.update_data(
  215. MockState(assign=assign),
  216. "",
  217. {
  218. "args": [
  219. {"id": a_datanode.id},
  220. ]
  221. },
  222. )
  223. assign.assert_called_once()
  224. assert assign.call_args.args[0] == "gui_core_dv_error"
  225. assert str(assign.call_args.args[1]).endswith("is not editable.")
  226. def test_tabular_data_edit(self):
  227. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  228. mockGui = Mock(Gui)
  229. mockGui._get_client_id = lambda: "a_client_id"
  230. gui_core_context = _GuiCoreContext(mockGui)
  231. assign = Mock()
  232. gui_core_context.tabular_data_edit(
  233. MockState(assign=assign),
  234. "",
  235. {
  236. "user_data": {"dn_id": a_datanode.id},
  237. },
  238. )
  239. assign.assert_called_once()
  240. assert assign.call_args_list[0].args[0] == "gui_core_dv_error"
  241. assert (
  242. assign.call_args_list[0].args[1]
  243. == "Error updating Datanode tabular value: type does not support at[] indexer."
  244. )
  245. assign.reset_mock()
  246. with patch("taipy.gui_core._context.is_editable", side_effect=mock_is_editable_false):
  247. gui_core_context.tabular_data_edit(
  248. MockState(assign=assign),
  249. "",
  250. {
  251. "user_data": {"dn_id": a_datanode.id},
  252. },
  253. )
  254. assign.assert_called_once()
  255. assert assign.call_args.args[0] == "gui_core_dv_error"
  256. assert str(assign.call_args.args[1]).endswith("is not editable.")