1
0

test_map_dict.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright 2023 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. try:
  60. md = _MapDict("not_a_dict")
  61. assert False
  62. except Exception:
  63. assert True
  64. pass
  65. def test_map_dict_update():
  66. update_values = {}
  67. def update(k, v):
  68. update_values[0] = k
  69. update_values[1] = v
  70. pass
  71. d = {"a": 1, "b": "2"}
  72. md = _MapDict(d, update)
  73. md.__setitem__("a", 3)
  74. assert update_values[0] == "a"
  75. assert update_values[1] == 3
  76. pass
  77. def test_map_dict_update_full_dictionary_1():
  78. values = {"a": 1, "b": 2}
  79. update_values = {"a": 3, "b": 5}
  80. md = _MapDict(values)
  81. assert md["a"] == 1
  82. assert md["b"] == 2
  83. md.update(update_values)
  84. assert md["a"] == 3
  85. assert md["b"] == 5
  86. def test_map_dict_update_full_dictionary_2():
  87. temp_values = {}
  88. def update(k, v):
  89. temp_values[k] = v
  90. values = {"a": 1, "b": 2}
  91. update_values = {"a": 3, "b": 5}
  92. md = _MapDict(values, update)
  93. assert md["a"] == 1
  94. assert md["b"] == 2
  95. md.update(update_values)
  96. assert temp_values["a"] == 3
  97. assert temp_values["b"] == 5
  98. def test_map_dict_set(gui: Gui, test_client):
  99. d = {"a": 1} # noqa: F841
  100. # set gui frame
  101. gui._set_frame(inspect.currentframe())
  102. with patch("sys.argv", ["prog"]):
  103. gui.run(run_server=False, single_client=True)
  104. with gui.get_flask_app().app_context():
  105. assert isinstance(gui._Gui__state.d, _MapDict)
  106. gui._Gui__state.d = {"b": 2}
  107. assert isinstance(gui._Gui__state.d, _MapDict)
  108. assert len(gui._Gui__state.d) == 1
  109. assert gui._Gui__state.d.get("a", None) is None
  110. assert gui._Gui__state.d.get("b", None) == 2
  111. def test_map_dict_items():
  112. def update(k, v):
  113. pass
  114. values = {"a": 1, "b": {"c": "list c"}}
  115. md = _MapDict(values)
  116. mdu = _MapDict(values, update)
  117. assert md["a"] == 1
  118. assert isinstance(md["b"], _MapDict)
  119. assert isinstance(mdu["b"], _MapDict)
  120. assert md["b"]["c"] == "list c"
  121. assert mdu["b"]["c"] == "list c"
  122. del md["a"]
  123. with pytest.raises(KeyError):
  124. md["e"]
  125. setattr(md, "a", 1)
  126. assert md["a"] == 1