test_expression.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 pandas as pd # type: ignore
  13. from taipy.gui import Gui
  14. def test_expression_text_control_str(gui: Gui, test_client, helpers):
  15. gui._bind_var_val("x", "Hello World!")
  16. md_string = "<|{x}|>"
  17. expected_list = ["<Field", 'dataType="str"', 'defaultValue="Hello World!"', "value={tpec_TpExPr_x_TPMDL_0}"]
  18. helpers.test_control_md(gui, md_string, expected_list)
  19. def test_expression_text_control_int(gui: Gui, test_client, helpers):
  20. gui._bind_var_val("x", 10)
  21. md_string = "<|{x}|>"
  22. expected_list = ["<Field", 'dataType="int"', 'defaultValue="10"', "value={tpec_TpExPr_x_TPMDL_0}"]
  23. helpers.test_control_md(gui, md_string, expected_list)
  24. def test_expression_text_control_1(gui: Gui, test_client, helpers):
  25. gui._set_frame(inspect.currentframe())
  26. gui._bind_var_val("x", 10)
  27. gui._bind_var_val("y", 20)
  28. md_string = "<|{x + y}|>"
  29. expected_list = [
  30. "<Field",
  31. 'dataType="int"',
  32. 'defaultValue="30"',
  33. "value={tp_TpExPr_x_y_TPMDL_0_0}",
  34. ]
  35. helpers.test_control_md(gui, md_string, expected_list)
  36. def test_expression_text_control_2(gui: Gui, test_client, helpers):
  37. gui._set_frame(inspect.currentframe())
  38. gui._bind_var_val("x", 10)
  39. gui._bind_var_val("y", 20)
  40. md_string = "<|x + y = {x + y}|>"
  41. expected_list = [
  42. "<Field",
  43. 'dataType="str"',
  44. 'defaultValue="x + y = 30"',
  45. "value={tp_TpExPr_x_y_x_y_TPMDL_0_0}",
  46. ]
  47. helpers.test_control_md(gui, md_string, expected_list)
  48. def test_expression_text_control_3(gui: Gui, test_client, helpers):
  49. gui._set_frame(inspect.currentframe())
  50. gui._bind_var_val("x", "Mickey Mouse")
  51. gui._bind_var_val("y", "Donald Duck")
  52. md_string = "<|Hello {x} and {y}|>"
  53. expected_list = [
  54. "<Field",
  55. 'dataType="str"',
  56. 'defaultValue="Hello Mickey Mouse and Donald Duck"',
  57. "value={tp_TpExPr_Hello_x_and_y_TPMDL_0_0}",
  58. ]
  59. helpers.test_control_md(gui, md_string, expected_list)
  60. def test_expression_text_gt_operator(gui: Gui, test_client, helpers):
  61. gui._set_frame(inspect.currentframe())
  62. gui._bind_var_val("x", 0)
  63. md_string = "<|{x > 0}|>"
  64. expected_list = [
  65. "<Field",
  66. 'dataType="bool"',
  67. 'defaultValue="false"',
  68. "value={tp_TpExPr_x_0_TPMDL_0_0}",
  69. ]
  70. helpers.test_control_md(gui, md_string, expected_list)
  71. def test_expression_button_control(gui: Gui, test_client, helpers):
  72. gui._bind_var_val("label", "A button label")
  73. md_string = "<|button|label={label}|>"
  74. expected_list = ["<Button", 'defaultLabel="A button label"', "label={tpec_TpExPr_label_TPMDL_0}"]
  75. helpers.test_control_md(gui, md_string, expected_list)
  76. def test_expression_table_control(gui: Gui, test_client, helpers):
  77. gui._set_frame(inspect.currentframe())
  78. gui._bind_var_val("pd", pd)
  79. gui._bind_var_val("series_1", pd.Series(["a", "b", "c"], name="Letters"))
  80. gui._bind_var_val("series_2", pd.Series([1, 2, 3], name="Numbers"))
  81. md_string = "<|{pd.concat([series_1, series_2], axis=1)}|table|columns=Letters;Numbers|>"
  82. expected_list = [
  83. "<Table",
  84. 'defaultColumns="{&quot;Letters&quot;: &#x7B;&quot;index&quot;: 0, &quot;type&quot;: &quot;object&quot;, &quot;dfid&quot;: &quot;Letters&quot;&#x7D;, &quot;Numbers&quot;: &#x7B;&quot;index&quot;: 1, &quot;type&quot;: &quot;int&quot;, &quot;dfid&quot;: &quot;Numbers&quot;&#x7D;}"', # noqa: E501
  85. 'updateVarName="_TpD_tp_TpExPr_pd_concat_series_1_series_2_axis_1_TPMDL_0_0"',
  86. "data={_TpD_tp_TpExPr_pd_concat_series_1_series_2_axis_1_TPMDL_0_0}",
  87. ]
  88. helpers.test_control_md(gui, md_string, expected_list)
  89. assert isinstance(gui._get_data_scope().tp_TpExPr_pd_concat_series_1_series_2_axis_1_TPMDL_0_0, pd.DataFrame)
  90. def test_lambda_expression_selector(gui: Gui, test_client, helpers):
  91. gui._bind_var_val(
  92. "lov",
  93. [
  94. {"id": "1", "name": "scenario 1"},
  95. {"id": "3", "name": "scenario 3"},
  96. {"id": "2", "name": "scenario 2"},
  97. ],
  98. )
  99. gui._bind_var_val("sel", {"id": "1", "name": "scenario 1"})
  100. md_string = "<|{sel}|selector|lov={lov}|type=test|adapter={lambda elt: (elt['id'], elt['name'])}|>"
  101. expected_list = [
  102. "<Selector",
  103. 'defaultLov="[[&quot;1&quot;, &quot;scenario 1&quot;], [&quot;3&quot;, &quot;scenario 3&quot;], [&quot;2&quot;, &quot;scenario 2&quot;]]"', # noqa: E501
  104. 'defaultValue="[&quot;1&quot;]"',
  105. 'updateVars="lov=_TpL_tpec_TpExPr_lov_TPMDL_0"',
  106. "lov={_TpL_tpec_TpExPr_lov_TPMDL_0}",
  107. 'updateVarName="_TpLv_tpec_TpExPr_sel_TPMDL_0"',
  108. "value={_TpLv_tpec_TpExPr_sel_TPMDL_0}",
  109. ]
  110. helpers.test_control_md(gui, md_string, expected_list)