test_reload.py 3.7 KB

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