test_aggrid.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. from datetime import datetime, timedelta, timezone
  2. import pandas as pd
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from selenium.webdriver.common.keys import Keys
  5. from nicegui import ui
  6. from nicegui.testing import Screen
  7. def test_update_table(screen: Screen):
  8. grid = ui.aggrid({
  9. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  10. 'rowData': [{'name': 'Alice', 'age': 18}],
  11. })
  12. screen.open('/')
  13. screen.should_contain('Name')
  14. screen.should_contain('Age')
  15. screen.should_contain('Alice')
  16. screen.should_contain('18')
  17. grid.options['rowData'][0]['age'] = 42
  18. screen.wait(0.5) # HACK: try to fix flaky test
  19. grid.update()
  20. screen.wait(0.5) # HACK: try to fix flaky test
  21. screen.should_contain('42')
  22. def test_add_row(screen: Screen):
  23. grid = ui.aggrid({
  24. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  25. 'rowData': [],
  26. })
  27. ui.button('Update', on_click=grid.update)
  28. screen.open('/')
  29. grid.options['rowData'].append({'name': 'Alice', 'age': 18})
  30. screen.click('Update')
  31. screen.wait(0.5)
  32. screen.should_contain('Alice')
  33. screen.should_contain('18')
  34. grid.options['rowData'].append({'name': 'Bob', 'age': 21})
  35. screen.click('Update')
  36. screen.wait(0.5)
  37. screen.should_contain('Alice')
  38. screen.should_contain('18')
  39. screen.should_contain('Bob')
  40. screen.should_contain('21')
  41. def test_click_cell(screen: Screen):
  42. grid = ui.aggrid({
  43. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  44. 'rowData': [{'name': 'Alice', 'age': 18}],
  45. })
  46. grid.on('cellClicked', lambda e: ui.label(f'{e.args["data"]["name"]} has been clicked!'))
  47. screen.open('/')
  48. screen.click('Alice')
  49. screen.should_contain('Alice has been clicked!')
  50. def test_html_columns(screen: Screen):
  51. ui.aggrid({
  52. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  53. 'rowData': [{'name': '<span class="text-bold">Alice</span>', 'age': 18}],
  54. }, html_columns=[0])
  55. screen.open('/')
  56. screen.should_contain('Alice')
  57. screen.should_not_contain('<span')
  58. assert 'text-bold' in screen.find('Alice').get_attribute('class')
  59. def test_dynamic_method(screen: Screen):
  60. ui.aggrid({
  61. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  62. 'rowData': [{'name': 'Alice', 'age': '18'}, {'name': 'Bob', 'age': '21'}, {'name': 'Carol', 'age': '42'}],
  63. ':getRowHeight': 'params => params.data.age > 35 ? 50 : 25',
  64. })
  65. screen.open('/')
  66. trs = screen.find_all_by_class('ag-row')
  67. assert len(trs) == 3
  68. heights = [int(tr.get_attribute('clientHeight')) for tr in trs]
  69. assert 23 <= heights[0] <= 25
  70. assert 23 <= heights[1] <= 25
  71. assert 48 <= heights[2] <= 50
  72. def test_run_grid_method_with_argument(screen: Screen):
  73. grid = ui.aggrid({
  74. 'columnDefs': [{'field': 'name', 'filter': True}],
  75. 'rowData': [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}],
  76. })
  77. filter_model = {'name': {'filterType': 'text', 'type': 'equals', 'filter': 'Alice'}}
  78. ui.button('Filter', on_click=lambda: grid.run_grid_method('setFilterModel', filter_model))
  79. screen.open('/')
  80. screen.should_contain('Alice')
  81. screen.should_contain('Bob')
  82. screen.should_contain('Carol')
  83. screen.click('Filter')
  84. screen.should_contain('Alice')
  85. screen.should_not_contain('Bob')
  86. screen.should_not_contain('Carol')
  87. def test_run_column_method_with_argument(screen: Screen):
  88. grid = ui.aggrid({
  89. 'columnDefs': [{'field': 'name'}, {'field': 'age', 'hide': True}],
  90. 'rowData': [{'name': 'Alice', 'age': '18'}, {'name': 'Bob', 'age': '21'}, {'name': 'Carol', 'age': '42'}],
  91. })
  92. ui.button('Show Age', on_click=lambda: grid.run_column_method('setColumnVisible', 'age', True))
  93. screen.open('/')
  94. screen.should_contain('Alice')
  95. screen.should_not_contain('18')
  96. screen.click('Show Age')
  97. screen.should_contain('18')
  98. def test_get_selected_rows(screen: Screen):
  99. @ui.page('/')
  100. def page():
  101. grid = ui.aggrid({
  102. 'columnDefs': [{'field': 'name'}],
  103. 'rowData': [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}],
  104. 'rowSelection': 'multiple',
  105. })
  106. async def get_selected_rows():
  107. ui.label(str(await grid.get_selected_rows()))
  108. ui.button('Get selected rows', on_click=get_selected_rows)
  109. async def get_selected_row():
  110. ui.label(str(await grid.get_selected_row()))
  111. ui.button('Get selected row', on_click=get_selected_row)
  112. screen.open('/')
  113. screen.click('Alice')
  114. screen.find('Bob')
  115. ActionChains(screen.selenium).key_down(Keys.SHIFT).click(screen.find('Bob')).key_up(Keys.SHIFT).perform()
  116. screen.click('Get selected rows')
  117. screen.should_contain("[{'name': 'Alice'}, {'name': 'Bob'}]")
  118. screen.click('Get selected row')
  119. screen.should_contain("{'name': 'Alice'}")
  120. def test_replace_aggrid(screen: Screen):
  121. with ui.row().classes('w-full') as container:
  122. ui.aggrid({'columnDefs': [{'field': 'name'}], 'rowData': [{'name': 'Alice'}]})
  123. def replace():
  124. container.clear()
  125. with container:
  126. ui.aggrid({'columnDefs': [{'field': 'name'}], 'rowData': [{'name': 'Bob'}]})
  127. ui.button('Replace', on_click=replace)
  128. screen.open('/')
  129. screen.should_contain('Alice')
  130. screen.click('Replace')
  131. screen.should_contain('Bob')
  132. screen.should_not_contain('Alice')
  133. def test_create_from_pandas(screen: Screen):
  134. df = pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [18, 21], 42: 'answer'})
  135. ui.aggrid.from_pandas(df)
  136. screen.open('/')
  137. screen.should_contain('Alice')
  138. screen.should_contain('Bob')
  139. screen.should_contain('18')
  140. screen.should_contain('21')
  141. screen.should_contain('42')
  142. screen.should_contain('answer')
  143. def test_create_dynamically(screen: Screen):
  144. ui.button('Create', on_click=lambda: ui.aggrid({'columnDefs': [{'field': 'name'}], 'rowData': [{'name': 'Alice'}]}))
  145. screen.open('/')
  146. screen.click('Create')
  147. screen.should_contain('Alice')
  148. def test_api_method_after_creation(screen: Screen):
  149. options = {'columnDefs': [{'field': 'name'}], 'rowData': [{'name': 'Alice'}]}
  150. ui.button('Create', on_click=lambda: ui.aggrid(options).run_grid_method('selectAll'))
  151. screen.open('/')
  152. screen.click('Create')
  153. assert screen.find_by_class('ag-row-selected')
  154. def test_problematic_datatypes(screen: Screen):
  155. df = pd.DataFrame({
  156. 'datetime_col': [datetime(2020, 1, 1)],
  157. 'datetime_col_tz': [datetime(2020, 1, 1, tzinfo=timezone.utc)],
  158. 'timedelta_col': [timedelta(days=5)],
  159. 'complex_col': [1 + 2j],
  160. 'period_col': pd.Series([pd.Period('2021-01')]),
  161. })
  162. ui.aggrid.from_pandas(df)
  163. screen.open('/')
  164. screen.should_contain('Datetime_col')
  165. screen.should_contain('Datetime_col_tz')
  166. screen.should_contain('Timedelta_col')
  167. screen.should_contain('Complex_col')
  168. screen.should_contain('Period_col')
  169. screen.should_contain('2020-01-01')
  170. screen.should_contain('5 days')
  171. screen.should_contain('(1+2j)')
  172. screen.should_contain('2021-01')
  173. def test_run_row_method(screen: Screen):
  174. grid = ui.aggrid({
  175. 'columnDefs': [{'field': 'name'}, {'field': 'age'}],
  176. 'rowData': [{'name': 'Alice', 'age': 18}],
  177. ':getRowId': '(params) => params.data.name',
  178. })
  179. ui.button('Update', on_click=lambda: grid.run_row_method('Alice', 'setDataValue', 'age', 42))
  180. screen.open('/')
  181. screen.should_contain('Alice')
  182. screen.should_contain('18')
  183. screen.click('Update')
  184. screen.should_contain('Alice')
  185. screen.should_contain('42')
  186. def test_run_method_with_function(screen: Screen):
  187. @ui.page('/')
  188. def page():
  189. grid = ui.aggrid({'columnDefs': [{'field': 'name'}], 'rowData': [{'name': 'Alice'}, {'name': 'Bob'}]})
  190. async def print_row(index: int) -> None:
  191. ui.label(f'Row {index}: {await grid.run_grid_method(f"(g) => g.getDisplayedRowAtIndex({index}).data")}')
  192. ui.button('Print Row 0', on_click=lambda: print_row(0))
  193. screen.open('/')
  194. screen.click('Print Row 0')
  195. screen.should_contain("Row 0: {'name': 'Alice'}")