test_context_is_readable.py 20 KB

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