1
0

test_chat.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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}|>"
  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. ]
  72. helpers.test_control_md(gui, md_string, expected_list)
  73. def test_chat_html_1_1(gui: Gui, test_client, helpers):
  74. gui._bind_var_val(
  75. "messages",
  76. [
  77. ["1", "msg 1", "Fred"],
  78. ["2", "msg From Another unknown User", "Fredo"],
  79. ["3", "This from the sender User", "taipy"],
  80. ["4", "And from another known one", "Fredi"],
  81. ],
  82. )
  83. gui._bind_var_val(
  84. "chat_properties",
  85. {"users": [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]},
  86. )
  87. html_string = '<taipy:chat messages="{messages}" properties="chat_properties"/>'
  88. expected_list = [
  89. "<Chat",
  90. '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
  91. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  92. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  93. ]
  94. helpers.test_control_html(gui, html_string, expected_list)
  95. def test_chat_html_1_2(gui: Gui, test_client, helpers):
  96. gui._bind_var_val(
  97. "messages",
  98. [
  99. ["1", "msg 1", "Fred"],
  100. ["2", "msg From Another unknown User", "Fredo"],
  101. ["3", "This from the sender User", "taipy"],
  102. ["4", "And from another known one", "Fredi"],
  103. ],
  104. )
  105. gui._bind_var_val(
  106. "chat_properties",
  107. {"users": [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]},
  108. )
  109. html_string = '<taipy:chat properties="chat_properties">{messages}</taipy:chat>'
  110. expected_list = [
  111. "<Chat",
  112. '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
  113. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  114. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  115. ]
  116. helpers.test_control_html(gui, html_string, expected_list)
  117. def test_chat_html_2_1(gui: Gui, test_client, helpers):
  118. gui._bind_var_val(
  119. "messages",
  120. [
  121. ["1", "msg 1", "Fred"],
  122. ["2", "msg From Another unknown User", "Fredo"],
  123. ["3", "This from the sender User", "taipy"],
  124. ["4", "And from another known one", "Fredi"],
  125. ],
  126. )
  127. gui._bind_var_val(
  128. "users", [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]
  129. )
  130. html_string = '<taipy:chat messages="{messages}" users="{users}" />'
  131. expected_list = [
  132. "<Chat",
  133. '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
  134. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  135. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  136. 'updateVars="users=_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0"',
  137. "users={_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0}",
  138. ]
  139. helpers.test_control_html(gui, html_string, expected_list)
  140. def test_chat_html_2_2(gui: Gui, test_client, helpers):
  141. gui._bind_var_val(
  142. "messages",
  143. [
  144. ["1", "msg 1", "Fred"],
  145. ["2", "msg From Another unknown User", "Fredo"],
  146. ["3", "This from the sender User", "taipy"],
  147. ["4", "And from another known one", "Fredi"],
  148. ],
  149. )
  150. gui._bind_var_val(
  151. "users", [["Fred", Icon("/images/favicon.png", "Fred.png")], ["Fredi", Icon("/images/fred.png", "Fred.png")]]
  152. )
  153. html_string = '<taipy:chat users="{users}">{messages}</taipy:chat>'
  154. expected_list = [
  155. "<Chat",
  156. '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
  157. "messages={_TpD_tpec_TpExPr_messages_TPMDL_0}",
  158. 'updateVarName="_TpD_tpec_TpExPr_messages_TPMDL_0"',
  159. 'updateVars="users=_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0"',
  160. "users={_TpL_tp_TpExPr_gui_get_adapted_lov_users_list_TPMDL_0_0}",
  161. ]
  162. helpers.test_control_html(gui, html_string, expected_list)