1
0

test_chat.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. from taipy.gui import Gui, Icon
  12. def test_chat_md_1(gui: Gui, test_client, helpers):
  13. gui._bind_var_val(
  14. "messages",
  15. [
  16. ["1", "msg 1", "Fred"],
  17. ["2", "msg From Another unknown User", "Fredo"],
  18. ["3", "This from the sender User", "taipy"],
  19. ["4", "And from another known one", "Fredi"],
  20. ],
  21. )
  22. gui._bind_var_val(
  23. "chat_properties",
  24. {
  25. "users": [
  26. ["Fred", Icon("/images/favicon.png", "Fred.png")],
  27. ["Fredi", Icon("/images/fred.png", "Fred.png")],
  28. ],
  29. "sender_id": "sender",
  30. "on_action": "on_action",
  31. "with_input": False,
  32. "height": "50vh",
  33. },
  34. )
  35. md_string = "<|{messages}|chat|properties=chat_properties|>"
  36. expected_list = [
  37. "<Chat",
  38. 'defaultUsers="[[&quot;Fred&quot;, &#x7B;&quot;path&quot;: &quot;/images/favicon.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;], [&quot;Fredi&quot;, &#x7B;&quot;path&quot;: &quot;/images/fred.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;]]"', # noqa: E501
  39. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  40. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  41. 'senderId="sender"',
  42. 'onAction="on_action"',
  43. "defaultWithInput={false}",
  44. 'height="50vh"',
  45. ]
  46. helpers.test_control_md(gui, md_string, expected_list)
  47. def test_chat_md_2(gui: Gui, test_client, helpers):
  48. gui._bind_var_val(
  49. "messages",
  50. [
  51. ["1", "msg 1", "Fred"],
  52. ["2", "msg From Another unknown User", "Fredo"],
  53. ["3", "This from the sender User", "taipy"],
  54. ["4", "And from another known one", "Fredi"],
  55. ],
  56. )
  57. gui._bind_var_val(
  58. "users", [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]
  59. )
  60. gui._bind_var_val("winp", False)
  61. md_string = "<|{messages}|chat|users={users}|with_input={winp}|show_sender|>"
  62. expected_list = [
  63. "<Chat",
  64. 'defaultUsers="[[&quot;Fred&quot;, &#x7B;&quot;path&quot;: &quot;/images/favicon.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;], [&quot;Fredi&quot;, &#x7B;&quot;path&quot;: &quot;/images/fred.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;]]"', # noqa: E501
  65. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  66. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  67. 'updateVars="users=_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0"',
  68. "users={_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0}",
  69. "withInput={_TpB_tpec_TpExPr_winp_TPMDL_0}>",
  70. "defaultWithInput={false}",
  71. "showSender={true}",
  72. ]
  73. helpers.test_control_md(gui, md_string, expected_list)
  74. def test_chat_html_1_1(gui: Gui, test_client, helpers):
  75. gui._bind_var_val(
  76. "messages",
  77. [
  78. ["1", "msg 1", "Fred"],
  79. ["2", "msg From Another unknown User", "Fredo"],
  80. ["3", "This from the sender User", "taipy"],
  81. ["4", "And from another known one", "Fredi"],
  82. ],
  83. )
  84. gui._bind_var_val(
  85. "chat_properties",
  86. {"users": [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]},
  87. )
  88. html_string = '<taipy:chat messages="{messages}" properties="chat_properties"/>'
  89. expected_list = [
  90. "<Chat",
  91. 'defaultUsers="[[&quot;Fred&quot;, &#x7B;&quot;path&quot;: &quot;/images/favicon.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;], [&quot;Fredi&quot;, &#x7B;&quot;path&quot;: &quot;/images/fred.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;]]"', # noqa: E501
  92. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  93. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  94. ]
  95. helpers.test_control_html(gui, html_string, expected_list)
  96. def test_chat_html_1_2(gui: Gui, test_client, helpers):
  97. gui._bind_var_val(
  98. "messages",
  99. [
  100. ["1", "msg 1", "Fred"],
  101. ["2", "msg From Another unknown User", "Fredo"],
  102. ["3", "This from the sender User", "taipy"],
  103. ["4", "And from another known one", "Fredi"],
  104. ],
  105. )
  106. gui._bind_var_val(
  107. "chat_properties",
  108. {"users": [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]},
  109. )
  110. html_string = '<taipy:chat properties="chat_properties">{messages}</taipy:chat>'
  111. expected_list = [
  112. "<Chat",
  113. 'defaultUsers="[[&quot;Fred&quot;, &#x7B;&quot;path&quot;: &quot;/images/favicon.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;], [&quot;Fredi&quot;, &#x7B;&quot;path&quot;: &quot;/images/fred.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;]]"', # noqa: E501
  114. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  115. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  116. ]
  117. helpers.test_control_html(gui, html_string, expected_list)
  118. def test_chat_html_2_1(gui: Gui, test_client, helpers):
  119. gui._bind_var_val(
  120. "messages",
  121. [
  122. ["1", "msg 1", "Fred"],
  123. ["2", "msg From Another unknown User", "Fredo"],
  124. ["3", "This from the sender User", "taipy"],
  125. ["4", "And from another known one", "Fredi"],
  126. ],
  127. )
  128. gui._bind_var_val(
  129. "users", [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]
  130. )
  131. html_string = '<taipy:chat messages="{messages}" users="{users}" />'
  132. expected_list = [
  133. "<Chat",
  134. 'defaultUsers="[[&quot;Fred&quot;, &#x7B;&quot;path&quot;: &quot;/images/favicon.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;], [&quot;Fredi&quot;, &#x7B;&quot;path&quot;: &quot;/images/fred.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;]]"', # noqa: E501
  135. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  136. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  137. 'updateVars="users=_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0"',
  138. "users={_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0}",
  139. ]
  140. helpers.test_control_html(gui, html_string, expected_list)
  141. def test_chat_html_2_2(gui: Gui, test_client, helpers):
  142. gui._bind_var_val(
  143. "messages",
  144. [
  145. ["1", "msg 1", "Fred"],
  146. ["2", "msg From Another unknown User", "Fredo"],
  147. ["3", "This from the sender User", "taipy"],
  148. ["4", "And from another known one", "Fredi"],
  149. ],
  150. )
  151. gui._bind_var_val(
  152. "users", [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]
  153. )
  154. html_string = '<taipy:chat users="{users}">{messages}</taipy:chat>'
  155. expected_list = [
  156. "<Chat",
  157. 'defaultUsers="[[&quot;Fred&quot;, &#x7B;&quot;path&quot;: &quot;/images/favicon.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;], [&quot;Fredi&quot;, &#x7B;&quot;path&quot;: &quot;/images/fred.png&quot;, &quot;text&quot;: &quot;Fred.png&quot;&#x7D;]]"', # noqa: E501
  158. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  159. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  160. 'updateVars="users=_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0"',
  161. "users={_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0}",
  162. ]
  163. helpers.test_control_html(gui, html_string, expected_list)