test_table.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2021-2025 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 taipy.gui.builder as tgb
  13. from taipy.gui import Gui
  14. def test_table_builder_1(gui: Gui, helpers, csvdata):
  15. with tgb.Page(frame=None) as page:
  16. tgb.table( # type: ignore[attr-defined]
  17. data="{csvdata}",
  18. page_size=10,
  19. page_size_options=[10, 30, 100],
  20. columns=["Day", "Entity", "Code", "Daily hospital occupancy"],
  21. date_format="eee dd MMM yyyy",
  22. )
  23. expected_list = [
  24. "<Table",
  25. 'defaultColumns="{&quot;Entity&quot;: &#x7B;&quot;type&quot;: &quot;object&quot;, &quot;index&quot;: 1, &quot;dfid&quot;: &quot;Entity&quot;&#x7D;, &quot;Code&quot;: &#x7B;&quot;type&quot;: &quot;object&quot;, &quot;index&quot;: 2, &quot;dfid&quot;: &quot;Code&quot;&#x7D;, &quot;Daily hospital occupancy&quot;: &#x7B;&quot;type&quot;: &quot;int&quot;, &quot;index&quot;: 3, &quot;dfid&quot;: &quot;Daily hospital occupancy&quot;&#x7D;, &quot;Day_str&quot;: &#x7B;&quot;type&quot;: &quot;datetime&quot;, &quot;index&quot;: 0, &quot;format&quot;: &quot;eee dd MMM yyyy&quot;, &quot;dfid&quot;: &quot;Day&quot;&#x7D;}"', # noqa: E501
  26. 'height="80vh"',
  27. 'width="100%"',
  28. 'pageSizeOptions="[10, 30, 100]"',
  29. "pageSize={10.0}",
  30. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  31. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  32. ]
  33. gui._set_frame(inspect.currentframe())
  34. helpers.test_control_builder(gui, page, expected_list)
  35. def test_table_reset_builder(gui: Gui, helpers, csvdata):
  36. with tgb.Page(frame=None) as page:
  37. tgb.table( # type: ignore[attr-defined]
  38. data="{csvdata}",
  39. rebuild=True,
  40. page_size=10,
  41. page_size_options="10;30;100",
  42. columns="Day;Entity;Code;Daily hospital occupancy",
  43. date_format="eee dd MMM yyyy",
  44. )
  45. expected_list = [
  46. "<Table",
  47. 'defaultColumns="{&quot;Entity&quot;: &#x7B;&quot;type&quot;: &quot;object&quot;, &quot;index&quot;: 1, &quot;dfid&quot;: &quot;Entity&quot;&#x7D;, &quot;Code&quot;: &#x7B;&quot;type&quot;: &quot;object&quot;, &quot;index&quot;: 2, &quot;dfid&quot;: &quot;Code&quot;&#x7D;, &quot;Daily hospital occupancy&quot;: &#x7B;&quot;type&quot;: &quot;int&quot;, &quot;index&quot;: 3, &quot;dfid&quot;: &quot;Daily hospital occupancy&quot;&#x7D;, &quot;Day_str&quot;: &#x7B;&quot;type&quot;: &quot;datetime&quot;, &quot;index&quot;: 0, &quot;format&quot;: &quot;eee dd MMM yyyy&quot;, &quot;dfid&quot;: &quot;Day&quot;&#x7D;}"', # noqa: E501
  48. 'height="80vh"',
  49. 'width="100%"',
  50. 'pageSizeOptions="[10, 30, 100]"',
  51. "pageSize={10.0}",
  52. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  53. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  54. "columns={tp_TpExPr_gui_tbl_cols_True_None_7B_22columns_22_3A_20_22Day_3BEntity_3BCode_3BDaily_20hospital_20occupancy_22_2C_20_22date_format_22_3A_20_22eee_20dd_20MMM_20yyyy_22_7D_7B_22data_22_3A_20_22tpec_TpExPr_csvdata_TPMDL_0_22_7D_tpec_TpExPr_csvdata_TPMDL_0_csvdata_TPMDL_0_0}",
  55. ]
  56. gui._set_frame(inspect.currentframe())
  57. helpers.test_control_builder(gui, page, expected_list)
  58. def test_table_builder_2(gui: Gui, helpers, csvdata):
  59. table_properties = { # noqa: F841
  60. "page_size": 10,
  61. "page_size_options": [10, 50, 100, 500],
  62. "allow_all_rows": True,
  63. "columns": {
  64. "Day": {"index": 0, "format": "dd/MM/yyyy", "title": "Date of measure"},
  65. "Entity": {"index": 1},
  66. "Code": {"index": 2},
  67. "Daily hospital occupancy": {"index": 3},
  68. },
  69. "date_format": "eee dd MMM yyyy",
  70. "number_format": "%.3f",
  71. "width": "60vw",
  72. "height": "60vh",
  73. }
  74. with tgb.Page(frame=None) as page:
  75. tgb.table(data="{csvdata}", properties="table_properties", auto_loading=True, editable=True) # type: ignore[attr-defined]
  76. expected_list = [
  77. "<Table",
  78. "allowAllRows={true}",
  79. "autoLoading={true}",
  80. "editable={true}",
  81. 'onEdit="__gui.table_on_edit',
  82. 'onDelete="__gui.table_on_delete',
  83. 'onAdd="__gui.table_on_add',
  84. 'defaultColumns="{&quot;Entity&quot;: &#x7B;&quot;index&quot;: 1, &quot;type&quot;: &quot;object&quot;, &quot;dfid&quot;: &quot;Entity&quot;&#x7D;, &quot;Code&quot;: &#x7B;&quot;index&quot;: 2, &quot;type&quot;: &quot;object&quot;, &quot;dfid&quot;: &quot;Code&quot;&#x7D;, &quot;Daily hospital occupancy&quot;: &#x7B;&quot;index&quot;: 3, &quot;format&quot;: &quot;%.3f&quot;, &quot;type&quot;: &quot;int&quot;, &quot;dfid&quot;: &quot;Daily hospital occupancy&quot;&#x7D;, &quot;Day_str&quot;: &#x7B;&quot;index&quot;: 0, &quot;format&quot;: &quot;dd/MM/yyyy&quot;, &quot;title&quot;: &quot;Date of measure&quot;, &quot;type&quot;: &quot;datetime&quot;, &quot;dfid&quot;: &quot;Day&quot;&#x7D;}"', # noqa: E501
  85. 'height="60vh"',
  86. 'width="60vw"',
  87. 'pageSizeOptions="[10, 50, 100, 500]"',
  88. "pageSize={10}",
  89. 'updateVarName="_TpD_tpec_TpExPr_csvdata_TPMDL_0"',
  90. "data={_TpD_tpec_TpExPr_csvdata_TPMDL_0}",
  91. ]
  92. gui._set_frame(inspect.currentframe())
  93. helpers.test_control_builder(gui, page, expected_list)