test_map_dict.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. import inspect
  12. import pytest
  13. from taipy.gui.gui import Gui
  14. from taipy.gui.utils import _MapDict
  15. def test_map_dict():
  16. d = {"a": 1, "b": 2, "c": 3}
  17. md = _MapDict(d)
  18. md_copy = _MapDict(d).copy()
  19. assert len(md) == 3
  20. assert md.__getitem__("a") == d["a"]
  21. md.__setitem__("a", 4)
  22. assert md.__getitem__("a") == 4
  23. assert d["a"] == 4
  24. v1 = d["b"]
  25. v2 = md.pop("b")
  26. assert v1 == v2
  27. assert "b" not in d.keys()
  28. assert "c" in md
  29. assert len(md) == 2
  30. v1 = d["c"]
  31. v2 = md.popitem()
  32. assert v2 == ("c", v1)
  33. assert len(md) == 1
  34. md.clear()
  35. assert len(md) == 0
  36. assert len(d) == 0
  37. assert len(md_copy) == 3
  38. v1 = ""
  39. for k in md_copy:
  40. v1 += k
  41. assert v1 == "abc"
  42. v1 = ""
  43. for k in md_copy.keys():
  44. v1 += k
  45. assert v1 == "abc"
  46. v1 = ""
  47. for k in md_copy.__reversed__():
  48. v1 += k
  49. assert v1 == "cba"
  50. v1 = 0
  51. for k in md_copy.values():
  52. v1 += k
  53. assert v1 == 6 # 1+2+3
  54. v1 = md_copy.setdefault("a", 5)
  55. assert v1 == 1
  56. v1 = md_copy.setdefault("d", 5)
  57. assert v1 == 5
  58. def test_map_dict_update():
  59. update_values = {}
  60. def update(k, v):
  61. update_values[0] = k
  62. update_values[1] = v
  63. pass
  64. d = {"a": 1, "b": "2"}
  65. md = _MapDict(d, update)
  66. md.__setitem__("a", 3)
  67. assert update_values[0] == "a"
  68. assert update_values[1] == 3
  69. pass
  70. def test_map_dict_update_full_dictionary_1():
  71. values = {"a": 1, "b": 2}
  72. update_values = {"a": 3, "b": 5}
  73. md = _MapDict(values)
  74. assert md["a"] == 1
  75. assert md["b"] == 2
  76. md.update(update_values)
  77. assert md["a"] == 3
  78. assert md["b"] == 5
  79. def test_map_dict_update_full_dictionary_2():
  80. temp_values = {}
  81. def update(k, v):
  82. temp_values[k] = v
  83. values = {"a": 1, "b": 2}
  84. update_values = {"a": 3, "b": 5}
  85. md = _MapDict(values, update)
  86. assert md["a"] == 1
  87. assert md["b"] == 2
  88. md.update(update_values)
  89. assert temp_values["a"] == 3
  90. assert temp_values["b"] == 5
  91. def test_map_dict_set(gui: Gui, test_client):
  92. d = {"a": 1} # noqa: F841
  93. # set gui frame
  94. gui._set_frame(inspect.currentframe())
  95. gui.run(run_server=False, single_client=True)
  96. with gui.get_flask_app().app_context():
  97. assert isinstance(gui._Gui__state.d, _MapDict) # type: ignore[attr-defined]
  98. gui._Gui__state.d = {"b": 2} # type: ignore[attr-defined]
  99. assert isinstance(gui._Gui__state.d, _MapDict) # type: ignore[attr-defined]
  100. assert len(gui._Gui__state.d) == 1 # type: ignore[attr-defined]
  101. assert gui._Gui__state.d.get("a", None) is None # type: ignore[attr-defined]
  102. assert gui._Gui__state.d.get("b", None) == 2 # type: ignore[attr-defined]
  103. def test_map_dict_items():
  104. def update(k, v):
  105. pass
  106. values = {"a": 1, "b": {"c": "list c"}}
  107. md = _MapDict(values)
  108. mdu = _MapDict(values, update)
  109. assert md["a"] == 1
  110. assert isinstance(md["b"], _MapDict)
  111. assert isinstance(mdu["b"], _MapDict)
  112. assert md["b"]["c"] == "list c"
  113. assert mdu["b"]["c"] == "list c"
  114. del md["a"]
  115. with pytest.raises(KeyError):
  116. md["e"]
  117. setattr(md, "a", 1) # noqa: B010
  118. assert md["a"] == 1