mirror of https://github.com/zulip/zulip.git
lib: Rename lib/api_test_helpers.py to openapi/python_examples.py
This will make the contained code easier to find.
This commit is contained in:
parent
c90056bdb2
commit
f280e9cf84
|
@ -16,7 +16,7 @@ Our API documentation is defined by a few sets of files:
|
||||||
docs](../documentation/user.html), with some special extensions for
|
docs](../documentation/user.html), with some special extensions for
|
||||||
rendering nice code blocks and example responses.
|
rendering nice code blocks and example responses.
|
||||||
* The text for the Python examples comes from a test suite for the
|
* The text for the Python examples comes from a test suite for the
|
||||||
Python API documentation (`zerver/lib/api_test_helpers.py`; run via
|
Python API documentation (`zerver/openapi/python_examples.py`; run via
|
||||||
`tools/test-api`). The `generate_code_example` macro will magically
|
`tools/test-api`). The `generate_code_example` macro will magically
|
||||||
read content from that test suite and render it as the code example.
|
read content from that test suite and render it as the code example.
|
||||||
This structure ensures that Zulip's API documentation is robust to a
|
This structure ensures that Zulip's API documentation is robust to a
|
||||||
|
@ -93,7 +93,7 @@ Additionally, JavaScript examples should conform to the coding style
|
||||||
and structure of [Zulip's existing JavaScript examples][javascript-examples].
|
and structure of [Zulip's existing JavaScript examples][javascript-examples].
|
||||||
|
|
||||||
For the Python examples, you'll write the example in
|
For the Python examples, you'll write the example in
|
||||||
`zerver/lib/api_test_helpers.py`, and it'll be run and verified
|
`zerver/openapi/python_examples.py`, and it'll be run and verified
|
||||||
automatically in Zulip's automated test suite. The code there will
|
automatically in Zulip's automated test suite. The code there will
|
||||||
look something like this:
|
look something like this:
|
||||||
|
|
||||||
|
@ -201,7 +201,7 @@ above.
|
||||||
[rest-api-tutorial]: ../tutorials/writing-views.html#writing-api-rest-endpoints
|
[rest-api-tutorial]: ../tutorials/writing-views.html#writing-api-rest-endpoints
|
||||||
|
|
||||||
1. Add a function for the endpoint you'd like to document to
|
1. Add a function for the endpoint you'd like to document to
|
||||||
`zerver/lib/api_test_helpers.py`. `render_message` is a good
|
`zerver/openapi/python_examples.py`. `render_message` is a good
|
||||||
example to follow. There are generally two key pieces to your
|
example to follow. There are generally two key pieces to your
|
||||||
test: (1) doing an API query and (2) verifying its result has the
|
test: (1) doing an API query and (2) verifying its result has the
|
||||||
expected format using `validate_against_openapi_schema`.
|
expected format using `validate_against_openapi_schema`.
|
||||||
|
@ -218,7 +218,7 @@ above.
|
||||||
|
|
||||||
1. Add the function to the `TEST_FUNCTIONS` dict and one of the
|
1. Add the function to the `TEST_FUNCTIONS` dict and one of the
|
||||||
`test_*` functions at the end of
|
`test_*` functions at the end of
|
||||||
`zerver/lib/api_test_helpers.py`; these will ensure your function
|
`zerver/openapi/python_examples.py`; these will ensure your function
|
||||||
will be called when running `test-api`.
|
will be called when running `test-api`.
|
||||||
|
|
||||||
1. Capture the JSON response returned by the API call (the test
|
1. Capture the JSON response returned by the API call (the test
|
||||||
|
|
|
@ -68,7 +68,7 @@ Zulip also has about a dozen smaller tests suites:
|
||||||
`/help` user documentation site, and related pages.
|
`/help` user documentation site, and related pages.
|
||||||
- `tools/test-api`: Tests that the API documentation at `/api`
|
- `tools/test-api`: Tests that the API documentation at `/api`
|
||||||
actually works; the actual code for this is defined in
|
actually works; the actual code for this is defined in
|
||||||
`zerver/lib/api_test_helpers.py`.
|
`zerver/openapi/python_examples.py`.
|
||||||
- `test-locked-requirements`: Verifies that developers didn't forget
|
- `test-locked-requirements`: Verifies that developers didn't forget
|
||||||
to run `tools/update-locked-requirements` after modifying
|
to run `tools/update-locked-requirements` after modifying
|
||||||
`requirements/*.in`. See
|
`requirements/*.in`. See
|
||||||
|
|
|
@ -26,7 +26,7 @@ omit =
|
||||||
zerver/lib/test_fixtures.py
|
zerver/lib/test_fixtures.py
|
||||||
zerver/lib/test_runner.py
|
zerver/lib/test_runner.py
|
||||||
# Has its own independent test suite
|
# Has its own independent test suite
|
||||||
zerver/lib/api_test_helpers.py
|
zerver/openapi/python_examples.py
|
||||||
zerver/lib/parallel.py
|
zerver/lib/parallel.py
|
||||||
# Debugging tools that don't lend themselves well to unit tests
|
# Debugging tools that don't lend themselves well to unit tests
|
||||||
zerver/lib/debug.py
|
zerver/lib/debug.py
|
||||||
|
|
|
@ -47,7 +47,7 @@ FILES_WITH_LEGACY_SUBJECT = {
|
||||||
'zerver/tests/test_email_mirror.py',
|
'zerver/tests/test_email_mirror.py',
|
||||||
|
|
||||||
# These are tied more to our API than our DB model.
|
# These are tied more to our API than our DB model.
|
||||||
'zerver/lib/api_test_helpers.py',
|
'zerver/openapi/python_examples.py',
|
||||||
'zerver/tests/test_openapi.py',
|
'zerver/tests/test_openapi.py',
|
||||||
|
|
||||||
# This has lots of query data embedded, so it's hard
|
# This has lots of query data embedded, so it's hard
|
||||||
|
@ -449,7 +449,7 @@ python_rules = RuleList(
|
||||||
'puppet/',
|
'puppet/',
|
||||||
# Zerver files that we should just clean.
|
# Zerver files that we should just clean.
|
||||||
'zerver/tests',
|
'zerver/tests',
|
||||||
'zerver/lib/api_test_helpers.py',
|
'zerver/openapi/python_examples.py',
|
||||||
'zerver/lib/request.py',
|
'zerver/lib/request.py',
|
||||||
'zerver/views/streams.py',
|
'zerver/views/streams.py',
|
||||||
# thumbor is (currently) python2 only
|
# thumbor is (currently) python2 only
|
||||||
|
|
|
@ -16,7 +16,7 @@ from zulip import Client
|
||||||
|
|
||||||
from tools.lib.test_script import assert_provisioning_status_ok
|
from tools.lib.test_script import assert_provisioning_status_ok
|
||||||
from tools.lib.test_server import test_server_running
|
from tools.lib.test_server import test_server_running
|
||||||
from zerver.lib.api_test_helpers import test_the_api, test_invalid_api_key
|
from zerver.openapi.python_examples import test_the_api, test_invalid_api_key
|
||||||
|
|
||||||
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings'
|
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings'
|
||||||
django.setup()
|
django.setup()
|
||||||
|
|
|
@ -109,7 +109,7 @@ not_yet_fully_covered = {path for target in [
|
||||||
'zerver/lib/generate_test_data.py',
|
'zerver/lib/generate_test_data.py',
|
||||||
'zerver/lib/test_fixtures.py',
|
'zerver/lib/test_fixtures.py',
|
||||||
'zerver/lib/test_runner.py',
|
'zerver/lib/test_runner.py',
|
||||||
'zerver/lib/api_test_helpers.py',
|
'zerver/openapi/python_examples.py',
|
||||||
# Tornado should ideally have full coverage, but we're not there.
|
# Tornado should ideally have full coverage, but we're not there.
|
||||||
'zerver/tornado/autoreload.py',
|
'zerver/tornado/autoreload.py',
|
||||||
'zerver/tornado/descriptors.py',
|
'zerver/tornado/descriptors.py',
|
||||||
|
|
|
@ -7,7 +7,7 @@ from markdown.preprocessors import Preprocessor
|
||||||
from typing import Any, Dict, Optional, List
|
from typing import Any, Dict, Optional, List
|
||||||
import markdown
|
import markdown
|
||||||
|
|
||||||
import zerver.lib.api_test_helpers
|
import zerver.openapi.python_examples
|
||||||
from zerver.lib.openapi import get_openapi_fixture
|
from zerver.lib.openapi import get_openapi_fixture
|
||||||
|
|
||||||
MACRO_REGEXP = re.compile(r'\{generate_code_example(\(\s*(.+?)\s*\))*\|\s*(.+?)\s*\|\s*(.+?)\s*(\(\s*(.+)\s*\))?\}')
|
MACRO_REGEXP = re.compile(r'\{generate_code_example(\(\s*(.+?)\s*\))*\|\s*(.+?)\s*\|\s*(.+?)\s*(\(\s*(.+)\s*\))?\}')
|
||||||
|
@ -55,7 +55,7 @@ def extract_python_code_example(source: List[str], snippet: List[str]) -> List[s
|
||||||
return extract_python_code_example(source, snippet)
|
return extract_python_code_example(source, snippet)
|
||||||
|
|
||||||
def render_python_code_example(function: str, admin_config: Optional[bool]=False) -> List[str]:
|
def render_python_code_example(function: str, admin_config: Optional[bool]=False) -> List[str]:
|
||||||
method = zerver.lib.api_test_helpers.TEST_FUNCTIONS[function]
|
method = zerver.openapi.python_examples.TEST_FUNCTIONS[function]
|
||||||
function_source_lines = inspect.getsourcelines(method)[0]
|
function_source_lines = inspect.getsourcelines(method)[0]
|
||||||
|
|
||||||
if admin_config:
|
if admin_config:
|
||||||
|
@ -144,7 +144,7 @@ class APICodeExamplesPreprocessor(Preprocessor):
|
||||||
path, method = function.rsplit(':', 1)
|
path, method = function.rsplit(':', 1)
|
||||||
fixture_dict = get_openapi_fixture(path, method, name)
|
fixture_dict = get_openapi_fixture(path, method, name)
|
||||||
else:
|
else:
|
||||||
fixture_dict = zerver.lib.api_test_helpers.FIXTURES[function]
|
fixture_dict = zerver.openapi.python_examples.FIXTURES[function]
|
||||||
|
|
||||||
fixture_json = json.dumps(fixture_dict, indent=4, sort_keys=True,
|
fixture_json = json.dumps(fixture_dict, indent=4, sort_keys=True,
|
||||||
separators=(',', ': '))
|
separators=(',', ': '))
|
||||||
|
|
Loading…
Reference in New Issue