test_context_is_deletable.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from unittest.mock import Mock, patch
  2. import pytest
  3. from src.taipy.gui_core._context import _GuiCoreContext
  4. from taipy.config.common.scope import Scope
  5. from taipy.core import Job, Scenario, Task
  6. from taipy.core.data.pickle import PickleDataNode
  7. a_scenario = Scenario("scenario_config_id", [], {}, sequences={"sequence": {}})
  8. a_task = Task("task_config_id", {}, print)
  9. a_job = Job("JOB_job_id", a_task, "submit_id", a_scenario.id)
  10. a_job.isfinished = lambda s: True
  11. a_datanode = PickleDataNode("data_node_config_id", Scope.SCENARIO)
  12. def mock_core_get(entity_id):
  13. if entity_id == a_scenario.id:
  14. return a_scenario
  15. if entity_id == a_job.id:
  16. return a_job
  17. if entity_id == a_datanode.id:
  18. return a_datanode
  19. return a_task
  20. def mock_is_deletable_false(entity_id):
  21. return False
  22. def mock_is_true(entity_id):
  23. return True
  24. class MockState:
  25. def __init__(self, **kwargs) -> None:
  26. self.assign = kwargs.get("assign")
  27. class TestGuiCoreContext_is_deletable:
  28. def test_crud_scenario(self):
  29. with patch("src.taipy.gui_core._context.core_get", side_effect=mock_core_get), patch(
  30. "src.taipy.gui_core._context.is_deletable", side_effect=mock_is_true
  31. ):
  32. gui_core_context = _GuiCoreContext(Mock())
  33. assign = Mock()
  34. gui_core_context.crud_scenario(
  35. MockState(assign=assign),
  36. "",
  37. {
  38. "args": [
  39. True,
  40. True,
  41. {"name": "name", "id": a_scenario.id},
  42. ]
  43. },
  44. )
  45. assign.assert_called_once()
  46. assert assign.call_args.args[0] == "gui_core_sc_error"
  47. assert str(assign.call_args.args[1]).startswith("Error deleting Scenario.")
  48. with patch("src.taipy.gui_core._context.is_deletable", side_effect=mock_is_deletable_false):
  49. assign.reset_mock()
  50. gui_core_context.crud_scenario(
  51. MockState(assign=assign),
  52. "",
  53. {
  54. "args": [
  55. True,
  56. True,
  57. {"name": "name", "id": a_scenario.id},
  58. ]
  59. },
  60. )
  61. assign.assert_called_once()
  62. assert assign.call_args.args[0] == "gui_core_sc_error"
  63. assert str(assign.call_args.args[1]).endswith("is not deletable.")
  64. def test_act_on_jobs(self):
  65. with patch("src.taipy.gui_core._context.core_get", side_effect=mock_core_get), patch(
  66. "src.taipy.gui_core._context.is_deletable", side_effect=mock_is_true
  67. ):
  68. gui_core_context = _GuiCoreContext(Mock())
  69. assign = Mock()
  70. gui_core_context.act_on_jobs(
  71. MockState(assign=assign),
  72. "",
  73. {
  74. "args": [
  75. {"id": [a_job.id], "action": "delete"},
  76. ]
  77. },
  78. )
  79. assign.assert_called_once()
  80. assert assign.call_args.args[0] == "gui_core_js_error"
  81. assert str(assign.call_args.args[1]).find("is not deletable.") == -1
  82. assign.reset_mock()
  83. with patch("src.taipy.gui_core._context.is_readable", side_effect=mock_is_deletable_false):
  84. gui_core_context.act_on_jobs(
  85. MockState(assign=assign),
  86. "",
  87. {
  88. "args": [
  89. {"id": [a_job.id], "action": "delete"},
  90. ]
  91. },
  92. )
  93. assign.assert_called_once()
  94. assert assign.call_args.args[0] == "gui_core_js_error"
  95. assert str(assign.call_args.args[1]).endswith("is not readable.")