test_context_is_readable.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. import contextlib
  12. import typing as t
  13. from unittest.mock import Mock, patch
  14. from taipy.config.common.scope import Scope
  15. from taipy.core import Job, JobId, Scenario, Task
  16. from taipy.core.data.pickle import PickleDataNode
  17. from taipy.core.submission.submission import Submission, SubmissionStatus
  18. from taipy.gui import Gui
  19. from taipy.gui_core._context import _GuiCoreContext
  20. a_scenario = Scenario("scenario_config_id", None, {}, sequences={"sequence": {}})
  21. a_task = Task("task_config_id", {}, print)
  22. a_job = Job(t.cast(JobId, "JOB_job_id"), a_task, "submit_id", a_scenario.id)
  23. a_job.isfinished = lambda s: True # type: ignore[attr-defined]
  24. a_datanode = PickleDataNode("data_node_config_id", Scope.SCENARIO)
  25. a_submission = Submission(
  26. a_scenario.id,
  27. "Scenario",
  28. a_scenario.config_id,
  29. properties={"client_id": "client_id", "on_submission": "on_submission"},
  30. )
  31. def mock_is_readable_false(entity_id):
  32. return False
  33. def mock_is_true(entity_id):
  34. return True
  35. def mock_core_get(entity_id):
  36. if entity_id == a_scenario.id:
  37. return a_scenario
  38. if entity_id == a_job.id:
  39. return a_job
  40. if entity_id == a_datanode.id:
  41. return a_datanode
  42. if entity_id == a_submission.id:
  43. return a_submission
  44. return a_task
  45. class MockState:
  46. def __init__(self, **kwargs) -> None:
  47. self.assign = kwargs.get("assign")
  48. class TestGuiCoreContext_is_readable:
  49. def test_scenario_adapter(self):
  50. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  51. gui_core_context = _GuiCoreContext(Mock())
  52. outcome = gui_core_context.scenario_adapter(a_scenario)
  53. assert isinstance(outcome, tuple)
  54. assert outcome[0] == a_scenario.id
  55. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  56. outcome = gui_core_context.scenario_adapter(a_scenario)
  57. assert outcome is None
  58. def test_get_scenario_by_id(self):
  59. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  60. gui_core_context = _GuiCoreContext(Mock())
  61. outcome = gui_core_context.get_scenario_by_id(a_scenario.id)
  62. assert outcome is not None
  63. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  64. outcome = gui_core_context.get_scenario_by_id(a_scenario.id)
  65. assert outcome is None
  66. def test_crud_scenario(self):
  67. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  68. gui_core_context = _GuiCoreContext(Mock())
  69. assign = Mock()
  70. gui_core_context.crud_scenario(
  71. MockState(assign=assign),
  72. "",
  73. {
  74. "args": [
  75. "",
  76. "",
  77. True,
  78. False,
  79. {"name": "name", "id": a_scenario.id},
  80. ]
  81. },
  82. )
  83. assign.assert_not_called()
  84. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  85. assign.reset_mock()
  86. gui_core_context.crud_scenario(
  87. MockState(assign=assign),
  88. "",
  89. {
  90. "args": [
  91. "",
  92. "",
  93. True,
  94. False,
  95. {"name": "name", "id": a_scenario.id},
  96. ]
  97. },
  98. )
  99. assign.assert_called_once()
  100. assert assign.call_args.args[0] == "gui_core_sc_error"
  101. assert str(assign.call_args.args[1]).endswith("is not readable.")
  102. def test_edit_entity(self):
  103. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  104. gui_core_context = _GuiCoreContext(Mock())
  105. assign = Mock()
  106. gui_core_context.edit_entity(
  107. MockState(assign=assign),
  108. "",
  109. {
  110. "args": [
  111. {"name": "name", "id": a_scenario.id},
  112. ]
  113. },
  114. )
  115. assign.assert_called_once()
  116. assert assign.call_args.args[0] == "gui_core_sv_error"
  117. assert assign.call_args.args[1] == ""
  118. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  119. assign.reset_mock()
  120. gui_core_context.edit_entity(
  121. MockState(assign=assign),
  122. "",
  123. {
  124. "args": [
  125. {"name": "name", "id": a_scenario.id},
  126. ]
  127. },
  128. )
  129. assign.assert_called_once()
  130. assert assign.call_args.args[0] == "gui_core_sv_error"
  131. assert str(assign.call_args.args[1]).endswith("is not readable.")
  132. def test_submission_status_callback(self):
  133. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get) as mockget:
  134. mockget.reset_mock()
  135. mockGui = Mock(Gui)
  136. mockGui._get_autorization = lambda s: contextlib.nullcontext()
  137. gui_core_context = _GuiCoreContext(mockGui)
  138. def sub_cb():
  139. return True
  140. gui_core_context.client_submission[a_submission.id] = SubmissionStatus.UNDEFINED
  141. gui_core_context.submission_status_callback(a_submission.id)
  142. mockget.assert_called()
  143. found = False
  144. for call in mockget.call_args_list:
  145. if call.args[0] == a_scenario.id:
  146. found = True
  147. break
  148. assert found is True
  149. mockget.reset_mock()
  150. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  151. gui_core_context.submission_status_callback(a_submission.id)
  152. mockget.assert_not_called()
  153. def test_data_node_adapter(self):
  154. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  155. gui_core_context = _GuiCoreContext(Mock())
  156. outcome = gui_core_context.data_node_adapter(a_datanode)
  157. assert isinstance(outcome, tuple)
  158. assert outcome[0] == a_datanode.id
  159. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  160. outcome = gui_core_context.data_node_adapter(a_datanode)
  161. assert outcome is None
  162. def test_job_adapter(self):
  163. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  164. gui_core_context = _GuiCoreContext(Mock())
  165. outcome = gui_core_context.job_adapter(a_job)
  166. assert isinstance(outcome, tuple)
  167. assert outcome[0] == a_job.id
  168. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  169. outcome = gui_core_context.job_adapter(a_job)
  170. assert outcome is None
  171. def test_act_on_jobs(self):
  172. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get), patch(
  173. "taipy.gui_core._context.is_deletable", side_effect=mock_is_true
  174. ):
  175. gui_core_context = _GuiCoreContext(Mock())
  176. assign = Mock()
  177. gui_core_context.act_on_jobs(
  178. MockState(assign=assign),
  179. "",
  180. {
  181. "args": [
  182. {"id": [a_job.id], "action": "delete"},
  183. ]
  184. },
  185. )
  186. assign.assert_called_once()
  187. assert assign.call_args.args[0] == "gui_core_js_error"
  188. assert str(assign.call_args.args[1]).find("is not readable.") == -1
  189. assign.reset_mock()
  190. gui_core_context.act_on_jobs(
  191. MockState(assign=assign),
  192. "",
  193. {
  194. "args": [
  195. {"id": [a_job.id], "action": "cancel"},
  196. ]
  197. },
  198. )
  199. assign.assert_called_once()
  200. assert assign.call_args.args[0] == "gui_core_js_error"
  201. assert str(assign.call_args.args[1]).find("is not readable.") == -1
  202. assign.reset_mock()
  203. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  204. gui_core_context.act_on_jobs(
  205. MockState(assign=assign),
  206. "",
  207. {
  208. "args": [
  209. {"id": [a_job.id], "action": "delete"},
  210. ]
  211. },
  212. )
  213. assign.assert_called_once()
  214. assert assign.call_args.args[0] == "gui_core_js_error"
  215. assert str(assign.call_args.args[1]).endswith("is not readable.")
  216. assign.reset_mock()
  217. gui_core_context.act_on_jobs(
  218. MockState(assign=assign),
  219. "",
  220. {
  221. "args": [
  222. {"id": [a_job.id], "action": "cancel"},
  223. ]
  224. },
  225. )
  226. assign.assert_called_once()
  227. assert assign.call_args.args[0] == "gui_core_js_error"
  228. assert str(assign.call_args.args[1]).endswith("is not readable.")
  229. def test_edit_data_node(self):
  230. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  231. gui_core_context = _GuiCoreContext(Mock())
  232. assign = Mock()
  233. gui_core_context.edit_data_node(
  234. MockState(assign=assign),
  235. "",
  236. {
  237. "args": [
  238. {"id": a_datanode.id},
  239. ]
  240. },
  241. )
  242. assign.assert_called_once()
  243. assert assign.call_args.args[0] == "gui_core_dv_error"
  244. assert assign.call_args.args[1] == ""
  245. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  246. assign.reset_mock()
  247. gui_core_context.edit_data_node(
  248. MockState(assign=assign),
  249. "",
  250. {
  251. "args": [
  252. {"id": a_datanode.id},
  253. ]
  254. },
  255. )
  256. assign.assert_called_once()
  257. assert assign.call_args.args[0] == "gui_core_dv_error"
  258. assert str(assign.call_args.args[1]).endswith("is not readable.")
  259. def test_lock_datanode_for_edit(self):
  260. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  261. mockGui = Mock(Gui)
  262. mockGui._get_client_id = lambda: "a_client_id"
  263. gui_core_context = _GuiCoreContext(mockGui)
  264. assign = Mock()
  265. gui_core_context.lock_datanode_for_edit(
  266. MockState(assign=assign),
  267. "",
  268. {
  269. "args": [
  270. {"id": a_datanode.id},
  271. ]
  272. },
  273. )
  274. assign.assert_called_once()
  275. assert assign.call_args.args[0] == "gui_core_dv_error"
  276. assert assign.call_args.args[1] == ""
  277. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  278. assign.reset_mock()
  279. gui_core_context.lock_datanode_for_edit(
  280. MockState(assign=assign),
  281. "",
  282. {
  283. "args": [
  284. {"id": a_datanode.id},
  285. ]
  286. },
  287. )
  288. assign.assert_called_once()
  289. assert assign.call_args.args[0] == "gui_core_dv_error"
  290. assert str(assign.call_args.args[1]).endswith("is not readable.")
  291. def test_get_scenarios_for_owner(self):
  292. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get) as mockget:
  293. gui_core_context = _GuiCoreContext(Mock())
  294. gui_core_context.get_scenarios_for_owner(a_scenario.id, '')
  295. mockget.assert_called_once()
  296. mockget.reset_mock()
  297. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  298. gui_core_context.submission_status_callback(a_scenario.id)
  299. mockget.assert_not_called()
  300. def test_update_data(self):
  301. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  302. mockGui = Mock(Gui)
  303. mockGui._get_client_id = lambda: "a_client_id"
  304. gui_core_context = _GuiCoreContext(mockGui)
  305. assign = Mock()
  306. gui_core_context.update_data(
  307. MockState(assign=assign),
  308. "",
  309. {
  310. "args": [
  311. {"id": a_datanode.id},
  312. ]
  313. },
  314. )
  315. assign.assert_called()
  316. assert assign.call_args_list[0].args[0] == "gui_core_dv_error"
  317. assert assign.call_args_list[0].args[1] == ""
  318. assign.reset_mock()
  319. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  320. gui_core_context.update_data(
  321. MockState(assign=assign),
  322. "",
  323. {
  324. "args": [
  325. {"id": a_datanode.id},
  326. ]
  327. },
  328. )
  329. assign.assert_called_once()
  330. assert assign.call_args.args[0] == "gui_core_dv_error"
  331. assert str(assign.call_args.args[1]).endswith("is not readable.")
  332. def test_tabular_data_edit(self):
  333. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get):
  334. mockGui = Mock(Gui)
  335. mockGui._get_client_id = lambda: "a_client_id"
  336. gui_core_context = _GuiCoreContext(mockGui)
  337. assign = Mock()
  338. gui_core_context.tabular_data_edit(
  339. MockState(assign=assign),
  340. "",
  341. {
  342. "user_data": {"dn_id": a_datanode.id},
  343. },
  344. )
  345. assign.assert_called_once()
  346. assert assign.call_args_list[0].args[0] == "gui_core_dv_error"
  347. assert (
  348. assign.call_args_list[0].args[1]
  349. == "Error updating Datanode tabular value: type does not support at[] indexer."
  350. )
  351. assign.reset_mock()
  352. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  353. gui_core_context.tabular_data_edit(
  354. MockState(assign=assign),
  355. "",
  356. {
  357. "user_data": {"dn_id": a_datanode.id},
  358. },
  359. )
  360. assign.assert_called_once()
  361. assert assign.call_args.args[0] == "gui_core_dv_error"
  362. assert str(assign.call_args.args[1]).endswith("is not readable.")
  363. def test_get_data_node_tabular_data(self):
  364. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get) as mockget:
  365. gui_core_context = _GuiCoreContext(Mock())
  366. gui_core_context.get_data_node_tabular_data(a_datanode.id, "")
  367. mockget.assert_called_once()
  368. mockget.reset_mock()
  369. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  370. gui_core_context.get_data_node_tabular_data(a_datanode.id, "")
  371. mockget.assert_not_called()
  372. def test_get_data_node_tabular_columns(self):
  373. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get) as mockget:
  374. gui_core_context = _GuiCoreContext(Mock())
  375. gui_core_context.get_data_node_tabular_columns(a_datanode.id, "")
  376. mockget.assert_called_once()
  377. mockget.reset_mock()
  378. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  379. gui_core_context.get_data_node_tabular_columns(a_datanode, a_datanode.id)
  380. mockget.assert_not_called()
  381. def test_get_data_node_chart_config(self):
  382. with patch("taipy.gui_core._context.core_get", side_effect=mock_core_get) as mockget:
  383. gui_core_context = _GuiCoreContext(Mock())
  384. gui_core_context.get_data_node_chart_config(a_datanode.id, "")
  385. mockget.assert_called_once()
  386. mockget.reset_mock()
  387. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  388. gui_core_context.get_data_node_chart_config(a_datanode.id, "")
  389. mockget.assert_not_called()