test_variable_binding.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 unittest.mock import patch
  12. from taipy.gui import Gui, Markdown
  13. def test_variable_binding(helpers):
  14. """
  15. Tests the binding of a few variables and a function
  16. """
  17. def another_function(gui):
  18. pass
  19. x = 10
  20. y = 20
  21. z = "button label"
  22. gui = Gui()
  23. gui.add_page("test", Markdown("<|{x}|> | <|{y}|> | <|{z}|button|on_action=another_function|>"))
  24. with patch("sys.argv", ["prog"]):
  25. gui.run(run_server=False, single_client=True)
  26. client = gui._server.test_client()
  27. jsx = client.get("/taipy-jsx/test").json["jsx"]
  28. for expected in ["<Button", 'defaultLabel="button label"', "label={tpec_TpExPr_z_TPMDL_0}"]:
  29. assert expected in jsx
  30. assert gui._bindings().x == x
  31. assert gui._bindings().y == y
  32. assert gui._bindings().z == z
  33. with gui.get_flask_app().app_context():
  34. assert callable(gui._get_user_function("another_function"))
  35. helpers.test_cleanup()
  36. def test_properties_binding(helpers):
  37. gui = Gui()
  38. modifier = "nice " # noqa: F841
  39. button_properties = {"label": "A {modifier}button"} # noqa: F841
  40. gui.add_page("test", Markdown("<|button|properties=button_properties|>"))
  41. with patch("sys.argv", ["prog"]):
  42. gui.run(run_server=False)
  43. client = gui._server.test_client()
  44. jsx = client.get("/taipy-jsx/test").json["jsx"]
  45. for expected in ["<Button", 'defaultLabel="A nice button"']:
  46. assert expected in jsx
  47. helpers.test_cleanup()
  48. def test_dict_binding(helpers):
  49. """
  50. Tests the binding of a dictionary property
  51. """
  52. d = {"k": "test"} # noqa: F841
  53. gui = Gui("<|{d.k}|>")
  54. with patch("sys.argv", ["prog"]):
  55. gui.run(run_server=False)
  56. client = gui._server.test_client()
  57. jsx = client.get("/taipy-jsx/TaiPy_root_page").json["jsx"]
  58. for expected in ["<Field", 'defaultValue="test"']:
  59. assert expected in jsx
  60. helpers.test_cleanup()