test_selector.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. from taipy.gui import Gui
  12. def test_selector_md_1(gui: Gui, test_client, helpers):
  13. gui._bind_var_val("selected_val", ["l1", "l2"])
  14. gui._bind_var_val("selector_properties", {"lov": [("l1", "v1"), ("l2", "v2"), ("l3", "v3")], "filter": True})
  15. md_string = "<|{selected_val}|selector|properties=selector_properties|multiple|>"
  16. expected_list = [
  17. "<Selector",
  18. 'defaultLov="[[&quot;l1&quot;, &quot;v1&quot;], [&quot;l2&quot;, &quot;v2&quot;], [&quot;l3&quot;, &quot;v3&quot;]]"',
  19. 'defaultValue="[&quot;l1&quot;, &quot;l2&quot;]"',
  20. "filter={true}",
  21. "multiple={true}",
  22. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  23. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  24. ]
  25. helpers.test_control_md(gui, md_string, expected_list)
  26. def test_selector_md_2(gui: Gui, test_client, helpers):
  27. gui._bind_var_val("selected_val", "Item 2")
  28. md_string = "<|{selected_val}|selector|lov=Item 1;Item 2; This is a another value|>"
  29. expected_list = [
  30. "<Selector",
  31. 'defaultLov="[&quot;Item 1&quot;, &quot;Item 2&quot;, &quot; This is a another value&quot;]"',
  32. 'defaultValue="[&quot;Item 2&quot;]"',
  33. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  34. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  35. ]
  36. helpers.test_control_md(gui, md_string, expected_list)
  37. def test_selector_md_3(gui: Gui, test_client, helpers):
  38. gui._bind_var_val("elt", None)
  39. gui._bind_var_val(
  40. "scenario_list",
  41. [{"id": "1", "name": "scenario 1"}, {"id": "3", "name": "scenario 3"}, {"id": "2", "name": "scenario 2"}],
  42. )
  43. gui._bind_var_val("selected_obj", {"id": "1", "name": "scenario 1"})
  44. md_string = '<|{selected_obj}|selector|lov={scenario_list}|type=Scenario|adapter={lambda elt: (elt["id"], elt["name"])}|not propagate|>'
  45. expected_list = [
  46. "<Selector",
  47. 'defaultLov="[[&quot;1&quot;, &quot;scenario 1&quot;], [&quot;3&quot;, &quot;scenario 3&quot;], [&quot;2&quot;, &quot;scenario 2&quot;]]"',
  48. 'defaultValue="[&quot;1&quot;]"',
  49. "lov={_TpL_tpec_TpExPr_scenario_list_TPMDL_0}",
  50. "propagate={false}",
  51. 'updateVars="lov=_TpL_tpec_TpExPr_scenario_list_TPMDL_0"',
  52. 'updateVarName="_TpLv_tpec_TpExPr_selected_obj_TPMDL_0"',
  53. "value={_TpLv_tpec_TpExPr_selected_obj_TPMDL_0}",
  54. ]
  55. helpers.test_control_md(gui, md_string, expected_list)
  56. def test_selector_html_1_1(gui: Gui, test_client, helpers):
  57. gui._bind_var_val("selected_val", ["l1", "l2"])
  58. gui._bind_var_val("selector_properties", {"lov": [("l1", "v1"), ("l2", "v2"), ("l3", "v3")], "filter": True})
  59. html_string = '<taipy:selector value="{selected_val}" properties="selector_properties" multiple="True"/>'
  60. expected_list = [
  61. "<Selector",
  62. 'defaultLov="[[&quot;l1&quot;, &quot;v1&quot;], [&quot;l2&quot;, &quot;v2&quot;], [&quot;l3&quot;, &quot;v3&quot;]]"',
  63. 'defaultValue="[&quot;l1&quot;, &quot;l2&quot;]"',
  64. "filter={true}",
  65. "multiple={true}",
  66. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  67. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  68. ]
  69. helpers.test_control_html(gui, html_string, expected_list)
  70. def test_selector_html_1_2(gui: Gui, test_client, helpers):
  71. gui._bind_var_val("selected_val", ["l1", "l2"])
  72. gui._bind_var_val("selector_properties", {"lov": [("l1", "v1"), ("l2", "v2"), ("l3", "v3")], "filter": True})
  73. html_string = '<taipy:selector properties="selector_properties" multiple="True">{selected_val}</taipy:selector>'
  74. expected_list = [
  75. "<Selector",
  76. 'defaultLov="[[&quot;l1&quot;, &quot;v1&quot;], [&quot;l2&quot;, &quot;v2&quot;], [&quot;l3&quot;, &quot;v3&quot;]]"',
  77. 'defaultValue="[&quot;l1&quot;, &quot;l2&quot;]"',
  78. "filter={true}",
  79. "multiple={true}",
  80. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  81. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  82. ]
  83. helpers.test_control_html(gui, html_string, expected_list)
  84. def test_selector_html_2_1(gui: Gui, test_client, helpers):
  85. gui._bind_var_val("selected_val", "Item 2")
  86. html_string = '<taipy:selector value="{selected_val}" lov="Item 1;Item 2; This is a another value" />'
  87. expected_list = [
  88. "<Selector",
  89. 'defaultLov="[&quot;Item 1&quot;, &quot;Item 2&quot;, &quot; This is a another value&quot;]"',
  90. 'defaultValue="[&quot;Item 2&quot;]"',
  91. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  92. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  93. ]
  94. helpers.test_control_html(gui, html_string, expected_list)
  95. def test_selector_html_2_2(gui: Gui, test_client, helpers):
  96. gui._bind_var_val("selected_val", "Item 2")
  97. html_string = '<taipy:selector lov="Item 1;Item 2; This is a another value">{selected_val}</taipy:selector>'
  98. expected_list = [
  99. "<Selector",
  100. 'defaultLov="[&quot;Item 1&quot;, &quot;Item 2&quot;, &quot; This is a another value&quot;]"',
  101. 'defaultValue="[&quot;Item 2&quot;]"',
  102. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  103. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  104. ]
  105. helpers.test_control_html(gui, html_string, expected_list)