test_reload.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 datetime import datetime
  12. import pytest
  13. import taipy as tp
  14. from taipy import Config
  15. from taipy.core._entity._reload import _Reloader
  16. @pytest.fixture(scope="function", autouse=True)
  17. def reset_reloader():
  18. """Reset the _Reloader singleton between tests."""
  19. _Reloader._instance = None
  20. _Reloader._no_reload_context = False
  21. _Reloader._context_depth = 0
  22. yield
  23. _Reloader._instance = None
  24. _Reloader._no_reload_context = False
  25. _Reloader._context_depth = 0
  26. class TestReloader:
  27. def test_initial_state(self):
  28. reloader = _Reloader()
  29. assert reloader._context_depth == 0
  30. assert not reloader._no_reload_context
  31. def test_single_context(self):
  32. dn = tp.create_global_data_node(Config.configure_data_node("dn1", scope=tp.Scope.GLOBAL))
  33. assert len(dn.edits) == 0
  34. edit = {"comments": "inside", "timestamp": datetime.now()}
  35. dn._edits.append(edit) # add a new edit in memory without saving the data node
  36. reloader = _Reloader()
  37. with reloader:
  38. assert reloader._context_depth == 1
  39. assert reloader._no_reload_context
  40. assert len(dn.edits) == 1 # The dn is not reloaded so the edit is still there
  41. assert reloader._context_depth == 0
  42. assert not reloader._no_reload_context
  43. assert len(dn.edits) == 0 # The dn is reloaded so the edit is removed
  44. def test_nested_contexts(self):
  45. dn = tp.create_global_data_node(Config.configure_data_node("dn1", scope=tp.Scope.GLOBAL))
  46. assert len(dn.edits) == 0
  47. edit = {"comments": "inside", "timestamp": datetime.now()}
  48. dn._edits.append(edit) # add a new edit in memory without saving the data node
  49. reloader = _Reloader()
  50. with reloader:
  51. assert reloader._context_depth == 1
  52. assert reloader._no_reload_context
  53. assert len(dn.edits) == 1 # The dn is not reloaded so the edit is still there
  54. with reloader:
  55. assert reloader._context_depth == 2
  56. assert reloader._no_reload_context
  57. assert len(dn.edits) == 1 # The dn is not reloaded so the edit is still there
  58. with reloader:
  59. assert reloader._context_depth == 3
  60. assert reloader._no_reload_context
  61. assert len(dn.edits) == 1 # The dn is not reloaded so the edit is still there
  62. assert reloader._context_depth == 2
  63. assert reloader._no_reload_context
  64. assert len(dn.edits) == 1 # The dn is not reloaded so the edit is still there
  65. assert reloader._context_depth == 1
  66. assert reloader._no_reload_context
  67. assert len(dn.edits) == 1 # The dn is not reloaded so the edit is still there
  68. assert reloader._context_depth == 0
  69. assert not reloader._no_reload_context
  70. assert len(dn.edits) == 0 # The dn is reloaded so the edit is removed
  71. def test_exception_handling(self):
  72. reloader = _Reloader()
  73. try:
  74. with reloader:
  75. assert reloader._context_depth == 1
  76. assert reloader._no_reload_context
  77. raise ValueError("Test exception")
  78. except ValueError:
  79. pass
  80. assert reloader._context_depth == 0
  81. assert not reloader._no_reload_context