1
0

test_context_is_submitable.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. from taipy import Scope
  13. from taipy.core import Job, JobId, Scenario, Task
  14. from taipy.core.data.pickle import PickleDataNode
  15. from taipy.core.reason import ReasonCollection
  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_is_submittable_reason(entity_id):
  23. reasons = ReasonCollection()
  24. reasons._add_reason(entity_id, "a reason")
  25. return reasons
  26. def mock_has_no_reason():
  27. return ReasonCollection()
  28. def mock_core_get(entity_id):
  29. if entity_id == a_scenario.id:
  30. return a_scenario
  31. if entity_id == a_job.id:
  32. return a_job
  33. if entity_id == a_datanode.id:
  34. return a_datanode
  35. return a_task
  36. class MockState:
  37. def __init__(self, **kwargs) -> None:
  38. self.assign = kwargs.get("assign")
  39. class TestGuiCoreContext_is_submittable:
  40. def test_submit_entity(self):
  41. with (
  42. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  43. patch("taipy.gui_core._context.is_submittable", side_effect=mock_has_no_reason),
  44. ):
  45. gui_core_context = _GuiCoreContext(Mock())
  46. assign = Mock()
  47. gui_core_context.submit_entity(
  48. MockState(assign=assign),
  49. "",
  50. {
  51. "args": [
  52. {"name": "name", "id": a_scenario.id},
  53. ],
  54. "error_id": "error_var",
  55. },
  56. )
  57. assign.assert_called_once()
  58. assert assign.call_args.args[0] == "error_var"
  59. assert str(assign.call_args.args[1]).startswith("Error submitting entity.")
  60. with patch("taipy.gui_core._context.is_submittable", side_effect=mock_is_submittable_reason):
  61. assign.reset_mock()
  62. gui_core_context.submit_entity(
  63. MockState(assign=assign),
  64. "",
  65. {
  66. "args": [
  67. {"name": "name", "id": a_scenario.id},
  68. ],
  69. "error_id": "error_var",
  70. },
  71. )
  72. assign.assert_called_once()
  73. assert assign.call_args.args[0] == "error_var"
  74. assert "is not submittable" in str(assign.call_args.args[1])