test_mock_state.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2021-2024 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
  12. from taipy.gui import Gui, State
  13. from taipy.gui.mock.mock_state import MockState
  14. from taipy.gui.utils import _MapDict
  15. def test_gui():
  16. gui = Gui("")
  17. ms = MockState(gui)
  18. assert ms.get_gui() is gui
  19. assert ms._gui is gui
  20. def test_read_attr():
  21. gui = Gui("")
  22. ms = MockState(gui, a=1)
  23. assert ms is not None
  24. assert ms.a == 1
  25. assert ms.b is None
  26. def test_read_context():
  27. ms = MockState(Gui(""), a=1)
  28. assert ms["b"] is not None
  29. assert ms["b"].a == 1
  30. def test_write_attr():
  31. ms = MockState(Gui(""), a=1)
  32. ms.a = 2
  33. assert ms.a == 2
  34. ms.b = 3
  35. assert ms.b == 3
  36. ms.a += 1
  37. assert ms.a == 3
  38. def test_dict():
  39. ms = MockState(Gui(""))
  40. a_dict = {"a": 1}
  41. ms.d = a_dict
  42. assert isinstance(ms.d, _MapDict)
  43. assert ms.d._dict is a_dict
  44. def test_write_context():
  45. ms = MockState(Gui(""), a=1)
  46. ms["page"].a = 2
  47. assert ms["page"].a == 2
  48. ms["page"].b = 3
  49. assert ms["page"].b == 3
  50. def test_assign():
  51. ms = MockState(Gui(""), a=1)
  52. ms.assign("a", 2)
  53. assert ms.a == 2
  54. ms.assign("b", 1)
  55. assert ms.b == 1
  56. def test_refresh():
  57. ms = MockState(Gui(""), a=1)
  58. ms.refresh("a")
  59. assert ms.a == 1
  60. ms.a = 2
  61. ms.refresh("a")
  62. assert ms.a == 2
  63. def test_context_manager():
  64. with MockState(Gui(""), a=1) as ms:
  65. assert ms is not None
  66. ms.a = 2
  67. assert ms.a == 2
  68. def test_broadcast():
  69. ms = MockState(Gui(""), a=1)
  70. ms.broadcast("a", 2)
  71. def test_set_favicon():
  72. gui = Gui("")
  73. gui.set_favicon = Mock()
  74. ms = MockState(gui, a=1)
  75. ms.set_favicon("a_path")
  76. gui.set_favicon.assert_called_once()
  77. def test_callback():
  78. def on_action(state: State):
  79. state.assign("a", 2)
  80. ms = MockState(Gui(""), a=1)
  81. on_action(ms)
  82. assert ms.a == 2
  83. def test_false():
  84. ms = MockState(Gui(""), a=False)
  85. assert ms.a is False