test_selector.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 pandas as pd
  12. import taipy.gui.builder as tgb
  13. from taipy.gui import Gui
  14. def test_selector_builder_1(gui: Gui, test_client, helpers):
  15. gui._bind_var_val("selected_val", ["l1", "l2"])
  16. gui._bind_var_val("selector_properties", {"lov": [("l1", "v1"), ("l2", "v2"), ("l3", "v3")], "filter": True})
  17. with tgb.Page(frame=None) as page:
  18. tgb.selector(value="{selected_val}", properties="{selector_properties}", multiple=True) # type: ignore[attr-defined]
  19. expected_list = [
  20. "<Selector",
  21. 'defaultLov="[[&quot;l1&quot;, &quot;v1&quot;], [&quot;l2&quot;, &quot;v2&quot;], [&quot;l3&quot;, &quot;v3&quot;]]"', # noqa: E501
  22. 'defaultValue="[&quot;l1&quot;, &quot;l2&quot;]"',
  23. "filter={true}",
  24. "multiple={true}",
  25. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  26. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  27. ]
  28. helpers.test_control_builder(gui, page, expected_list)
  29. def test_selector_builder_2(gui: Gui, test_client, helpers):
  30. gui._bind_var_val("selected_val", "Item 2")
  31. with tgb.Page(frame=None) as page:
  32. tgb.selector(value="{selected_val}", lov="Item 1;Item 2; This is a another value") # type: ignore[attr-defined]
  33. expected_list = [
  34. "<Selector",
  35. 'defaultLov="[&quot;Item 1&quot;, &quot;Item 2&quot;, &quot; This is a another value&quot;]"',
  36. 'defaultValue="[&quot;Item 2&quot;]"',
  37. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  38. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  39. ]
  40. helpers.test_control_builder(gui, page, expected_list)
  41. def test_selector_builder_3(gui: Gui, test_client, helpers):
  42. gui._bind_var_val("elt", None)
  43. gui._bind_var_val(
  44. "scenario_list",
  45. [{"id": "1", "name": "scenario 1"}, {"id": "3", "name": "scenario 3"}, {"id": "2", "name": "scenario 2"}],
  46. )
  47. gui._bind_var_val("selected_obj", {"id": "1", "name": "scenario 1"})
  48. with tgb.Page(frame=None) as page:
  49. tgb.selector( # type: ignore[attr-defined]
  50. value="{selected_obj}",
  51. lov="{scenario_list}",
  52. adapter="{lambda elt: (elt['id'], elt['name'])}",
  53. propagate=False,
  54. )
  55. expected_list = [
  56. "<Selector",
  57. 'defaultLov="[[&quot;1&quot;, &quot;scenario 1&quot;], [&quot;3&quot;, &quot;scenario 3&quot;], [&quot;2&quot;, &quot;scenario 2&quot;]]"', # noqa: E501
  58. 'defaultValue="[&quot;1&quot;]"',
  59. "lov={_TpL_tp_TpExPr_gui_get_adapted_lov_scenario_list_dict_TPMDL_0_0}",
  60. "propagate={false}",
  61. 'updateVars="lov=_TpL_tp_TpExPr_gui_get_adapted_lov_scenario_list_dict_TPMDL_0_0"',
  62. 'updateVarName="_TpLv_tpec_TpExPr_selected_obj_TPMDL_0"',
  63. "value={_TpLv_tpec_TpExPr_selected_obj_TPMDL_0}",
  64. ]
  65. helpers.test_control_builder(gui, page, expected_list)
  66. def test_selector_pandas_series(gui: Gui, test_client, helpers):
  67. pd_series = pd.Series(["l1", "l2", "l3"])
  68. gui._bind_var_val("selected_val", "l1")
  69. gui._bind_var_val("selector_properties", pd_series)
  70. with tgb.Page(frame=None) as page:
  71. tgb.selector(value="{selected_val}", lov="{selector_properties}") # type: ignore[attr-defined]
  72. expected_list = [
  73. "<Selector",
  74. 'defaultLov="[&quot;l1&quot;, &quot;l2&quot;, &quot;l3&quot;]"',
  75. 'defaultValue="[&quot;l1&quot;]"',
  76. 'updateVarName="_TpLv_tpec_TpExPr_selected_val_TPMDL_0"',
  77. "value={_TpLv_tpec_TpExPr_selected_val_TPMDL_0}",
  78. ]
  79. helpers.test_control_builder(gui, page, expected_list)