1
0

test_run.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import asyncio
  2. import time
  3. from typing import Awaitable, Generator
  4. import pytest
  5. from nicegui import app, run, ui
  6. from nicegui.testing import User
  7. @pytest.fixture(scope='module', autouse=True)
  8. def check_blocking_ui() -> Generator[None, None, None]:
  9. """This fixture ensures that we see a warning if the UI is blocked for too long.
  10. The warning would then automatically fail the test.
  11. """
  12. def configure() -> None:
  13. loop = asyncio.get_running_loop()
  14. loop.set_debug(True)
  15. loop.slow_callback_duration = 0.02
  16. app.on_startup(configure)
  17. yield
  18. def delayed_hello() -> str:
  19. """Test function that blocks for 1 second."""
  20. time.sleep(1)
  21. return 'hello'
  22. @pytest.mark.parametrize('func', [run.cpu_bound, run.io_bound])
  23. async def test_delayed_hello(user: User, func: Awaitable):
  24. @ui.page('/')
  25. async def index():
  26. ui.label(await func(delayed_hello))
  27. await user.open('/')
  28. await user.should_see('hello')
  29. async def test_run_unpickable_exception_in_cpu_bound_callback(user: User):
  30. class UnpicklableException(Exception):
  31. def __reduce__(self):
  32. raise NotImplementedError('This local object cannot be pickled')
  33. def raise_unpicklable_exception():
  34. raise UnpicklableException('test')
  35. @ui.page('/')
  36. async def index():
  37. with pytest.raises(AttributeError, match="Can't pickle local object|Can't get local object"):
  38. ui.label(await run.cpu_bound(raise_unpicklable_exception))
  39. await user.open('/')
  40. class ExceptionWithSuperParameter(Exception):
  41. def __init__(self) -> None:
  42. super().__init__('some parameter which does not appear in the custom exceptions init')
  43. def raise_exception_with_super_parameter():
  44. raise ExceptionWithSuperParameter()
  45. async def test_run_cpu_bound_function_which_raises_problematic_exception(user: User):
  46. @ui.page('/')
  47. async def index():
  48. with pytest.raises(run.SubprocessException, match='some parameter which does not appear in the custom exceptions init'):
  49. ui.label(await run.cpu_bound(raise_exception_with_super_parameter))
  50. await user.open('/')