test_endpoint_docs.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from typing import Set
  2. import pytest
  3. import requests
  4. from nicegui import __version__
  5. from nicegui.testing import Screen
  6. @pytest.fixture(autouse=True)
  7. def activate_fastapi_docs(screen: Screen):
  8. screen.ui_run_kwargs['fastapi_docs'] = True
  9. def get_openapi_paths() -> Set[str]:
  10. return set(requests.get(f'http://localhost:{Screen.PORT}/openapi.json', timeout=5).json()['paths'])
  11. def test_endpoint_documentation_default(screen: Screen):
  12. screen.open('/')
  13. assert get_openapi_paths() == set()
  14. def test_endpoint_documentation_page_only(screen: Screen):
  15. screen.ui_run_kwargs['endpoint_documentation'] = 'page'
  16. screen.open('/')
  17. assert get_openapi_paths() == {'/'}
  18. def test_endpoint_documentation_internal_only(screen: Screen):
  19. screen.ui_run_kwargs['endpoint_documentation'] = 'internal'
  20. screen.open('/')
  21. assert get_openapi_paths() == {
  22. f'/_nicegui/{__version__}/libraries/{{key}}',
  23. f'/_nicegui/{__version__}/components/{{key}}',
  24. f'/_nicegui/{__version__}/resources/{{key}}/{{path}}',
  25. }
  26. def test_endpoint_documentation_all(screen: Screen):
  27. screen.ui_run_kwargs['endpoint_documentation'] = 'all'
  28. screen.open('/')
  29. assert get_openapi_paths() == {
  30. '/',
  31. f'/_nicegui/{__version__}/libraries/{{key}}',
  32. f'/_nicegui/{__version__}/components/{{key}}',
  33. f'/_nicegui/{__version__}/resources/{{key}}/{{path}}',
  34. }