test_partial.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2023 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 json
  12. import warnings
  13. from types import SimpleNamespace
  14. from unittest.mock import patch
  15. from taipy.gui import Gui, Markdown
  16. def test_partial(gui: Gui):
  17. with warnings.catch_warnings(record=True):
  18. gui.add_partial(Markdown("#This is a partial"))
  19. with patch("sys.argv", ["prog"]):
  20. gui.run(run_server=False)
  21. client = gui._server.test_client()
  22. response = client.get(f"/taipy-jsx/{gui._config.partial_routes[0]}")
  23. response_data = json.loads(response.get_data().decode("utf-8", "ignore"))
  24. assert response.status_code == 200
  25. assert "jsx" in response_data and "This is a partial" in response_data["jsx"]
  26. def test_partial_update(gui: Gui):
  27. with warnings.catch_warnings(record=True):
  28. partial = gui.add_partial(Markdown("#This is a partial"))
  29. with patch("sys.argv", ["prog"]):
  30. gui.run(run_server=False, single_client=True)
  31. client = gui._server.test_client()
  32. response = client.get(f"/taipy-jsx/{gui._config.partial_routes[0]}")
  33. response_data = json.loads(response.get_data().decode("utf-8", "ignore"))
  34. assert response.status_code == 200
  35. assert "jsx" in response_data and "This is a partial" in response_data["jsx"]
  36. # update partial
  37. fake_state = SimpleNamespace()
  38. fake_state._gui = gui
  39. partial.update_content(fake_state, "#partial updated") # type: ignore
  40. response = client.get(f"/taipy-jsx/{gui._config.partial_routes[0]}")
  41. response_data = json.loads(response.get_data().decode("utf-8", "ignore"))
  42. assert response.status_code == 200
  43. assert "jsx" in response_data and "partial updated" in response_data["jsx"]