test_du.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 pytest
  13. from taipy.gui import Gui, Markdown
  14. from taipy.gui.servers.request import RequestAccessor
  15. asserted_content = (
  16. "MU",
  17. "_TpD_tpec_TpExPr_csvdata_TPMDL_0",
  18. {
  19. "data": [
  20. {
  21. "Code": "AUT",
  22. "Day_str": "2020-04-01T00:00:00.000000Z",
  23. "Daily hospital occupancy": 856,
  24. "Entity": "Austria",
  25. "_tp_index": 0,
  26. },
  27. {
  28. "Code": "AUT",
  29. "Day_str": "2020-04-02T00:00:00.000000Z",
  30. "Daily hospital occupancy": 823,
  31. "Entity": "Austria",
  32. "_tp_index": 1,
  33. },
  34. {
  35. "Code": "AUT",
  36. "Day_str": "2020-04-03T00:00:00.000000Z",
  37. "Daily hospital occupancy": 829,
  38. "Entity": "Austria",
  39. "_tp_index": 2,
  40. },
  41. {
  42. "Code": "AUT",
  43. "Day_str": "2020-04-04T00:00:00.000000Z",
  44. "Daily hospital occupancy": 826,
  45. "Entity": "Austria",
  46. "_tp_index": 3,
  47. },
  48. {
  49. "Code": "AUT",
  50. "Day_str": "2020-04-05T00:00:00.000000Z",
  51. "Daily hospital occupancy": 712,
  52. "Entity": "Austria",
  53. "_tp_index": 4,
  54. },
  55. {
  56. "Code": "AUT",
  57. "Day_str": "2020-04-06T00:00:00.000000Z",
  58. "Daily hospital occupancy": 824,
  59. "Entity": "Austria",
  60. "_tp_index": 5,
  61. },
  62. {
  63. "Code": "AUT",
  64. "Day_str": "2020-04-07T00:00:00.000000Z",
  65. "Daily hospital occupancy": 857,
  66. "Entity": "Austria",
  67. "_tp_index": 6,
  68. },
  69. {
  70. "Code": "AUT",
  71. "Day_str": "2020-04-08T00:00:00.000000Z",
  72. "Daily hospital occupancy": 829,
  73. "Entity": "Austria",
  74. "_tp_index": 7,
  75. },
  76. {
  77. "Code": "AUT",
  78. "Day_str": "2020-04-09T00:00:00.000000Z",
  79. "Daily hospital occupancy": 820,
  80. "Entity": "Austria",
  81. "_tp_index": 8,
  82. },
  83. {
  84. "Code": "AUT",
  85. "Day_str": "2020-04-10T00:00:00.000000Z",
  86. "Daily hospital occupancy": 771,
  87. "Entity": "Austria",
  88. "_tp_index": 9,
  89. },
  90. ],
  91. "rowcount": 14477,
  92. "start": 0,
  93. "format": "JSON",
  94. },
  95. )
  96. @pytest.mark.skip_if_not_server("flask")
  97. def test_du_table_data_fetched(gui: Gui, helpers, csvdata):
  98. # Bind test variables
  99. csvdata = csvdata
  100. # set gui frame
  101. gui._set_frame(inspect.currentframe())
  102. Gui._set_timezone("UTC")
  103. # Bind a page so that the variable will be evaluated as expression
  104. gui.add_page(
  105. "test",
  106. Markdown(
  107. "<|{csvdata}|table|page_size=10|page_size_options=10;30;100|columns=Day;Entity;Code;Daily hospital occupancy|date_format=eee dd MMM yyyy|>" # noqa: E501
  108. ),
  109. )
  110. gui.run(run_server=False)
  111. server_client = gui._server.test_client()
  112. # WS client and emit
  113. ws_client = gui._server._ws.test_client(gui._server.get_server_instance())
  114. sid = helpers.create_scope_and_get_sid(gui)
  115. # Get the jsx once so that the page will be evaluated -> variable will be registered
  116. server_client.get(f"/taipy-jsx/test?client_id={sid}")
  117. ws_client.emit(
  118. "message",
  119. {
  120. "client_id": sid,
  121. "type": "DU",
  122. "name": "_TpD_tpec_TpExPr_csvdata_TPMDL_0",
  123. "payload": {
  124. "columns": ["Day", "Entity", "Code", "Daily hospital occupancy"],
  125. "pagekey": "0-100-asc",
  126. "start": 0,
  127. "end": 9,
  128. "orderby": "",
  129. "sort": "asc",
  130. },
  131. },
  132. )
  133. # assert for received message (message that would be sent to the front-end client)
  134. received_messages = ws_client.get_received()
  135. assert received_messages
  136. helpers.assert_outward_ws_message(received_messages[0], *asserted_content)
  137. @pytest.mark.skip_if_not_server("fastapi")
  138. @pytest.mark.teste2e
  139. def test_du_table_data_fetched_fastapi(gui: Gui, helpers, csvdata):
  140. # Bind test variables
  141. csvdata = csvdata
  142. # set gui frame
  143. gui._set_frame(inspect.currentframe())
  144. Gui._set_timezone("UTC")
  145. # Bind a page so that the variable will be evaluated as expression
  146. gui.add_page(
  147. "test",
  148. Markdown(
  149. "<|{csvdata}|table|page_size=10|page_size_options=10;30;100|columns=Day;Entity;Code;Daily hospital occupancy|date_format=eee dd MMM yyyy|>" # noqa: E501
  150. ),
  151. )
  152. helpers.run_e2e_multi_client(gui)
  153. ws_client = helpers.get_socketio_test_client()
  154. sid = helpers.create_scope_and_get_sid(gui)
  155. RequestAccessor.set_sid(ws_client.get_sid())
  156. ws_client.get(f"/taipy-jsx/test?client_id={sid}")
  157. ws_client.emit(
  158. "message",
  159. {
  160. "client_id": sid,
  161. "type": "DU",
  162. "name": "_TpD_tpec_TpExPr_csvdata_TPMDL_0",
  163. "payload": {
  164. "columns": ["Day", "Entity", "Code", "Daily hospital occupancy"],
  165. "pagekey": "0-100-asc",
  166. "start": 0,
  167. "end": 9,
  168. "orderby": "",
  169. "sort": "asc",
  170. },
  171. },
  172. )
  173. # assert for received message (message that would be sent to the front-end client)
  174. received_messages = ws_client.get_received()
  175. try:
  176. assert received_messages
  177. helpers.assert_outward_ws_message(received_messages[0], *asserted_content)
  178. finally:
  179. ws_client.disconnect()