test_context_dn_properties.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import typing as t
  12. from unittest.mock import Mock, patch
  13. from taipy import Scope
  14. from taipy.core.data.pickle import PickleDataNode
  15. from taipy.gui_core._context import _GuiCoreContext
  16. a_datanode = PickleDataNode("data_node_config_id", Scope.SCENARIO)
  17. def mock_core_get(entity_id):
  18. if entity_id == a_datanode.id:
  19. return a_datanode
  20. return None
  21. def mock_is_readable_false(entity_id):
  22. return False
  23. def mock_is_true(entity_id):
  24. return True
  25. class MockState:
  26. def __init__(self, **kwargs) -> None:
  27. self.assign = kwargs.get("assign")
  28. class TestGuiCoreContext_data_node_properties:
  29. def test_get_data_node_properties(self):
  30. with (
  31. patch("taipy.gui_core._context.core_get", side_effect=mock_core_get),
  32. patch("taipy.gui_core._context.is_readable", side_effect=mock_is_true),
  33. ):
  34. gui_core_context = _GuiCoreContext(Mock())
  35. assert gui_core_context.get_data_node_properties("") is None
  36. assert gui_core_context.get_data_node_properties("not a datanode") is None
  37. with patch("taipy.gui_core._context.is_readable", side_effect=mock_is_readable_false):
  38. assert gui_core_context.get_data_node_properties(a_datanode.id) is None
  39. assert gui_core_context.get_data_node_properties(a_datanode.id) is not None
  40. assert isinstance(gui_core_context.get_data_node_properties(a_datanode.id), list)
  41. assert len(t.cast(list, gui_core_context.get_data_node_properties(a_datanode.id))) == 0