test_map_dict.py 3.8 KB

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