mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
docs(rest-api): build the download request table from the OpenAPI yaml
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import inspect
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from tests.admission import refusal
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core.api import routes
|
||||
from unshackle.core.dl_options import (
|
||||
DEFAULT_DOWNLOAD_PARAMS,
|
||||
DL_OPTION_ALIASES,
|
||||
@@ -31,20 +29,28 @@ def test_the_table_decides_every_dl_option():
|
||||
assert set(cast_dl_params(dl.cli, {})) == cli_names - CLI_ONLY
|
||||
|
||||
|
||||
def test_the_openapi_yaml_names_each_api_dl_option_and_no_other():
|
||||
"""The download request body documents every dl option the API takes, beside its own request keys."""
|
||||
schema = yaml.safe_load((routes.download.__doc__ or "").split("---", 1)[1])
|
||||
properties = schema["requestBody"]["content"]["application/json"]["schema"]["properties"]
|
||||
ROOT = Path(__file__).parents[3]
|
||||
HOOK_SPEC = importlib.util.spec_from_file_location("download_fields", ROOT / "docs/hooks/download_fields.py")
|
||||
assert HOOK_SPEC and HOOK_SPEC.loader
|
||||
download_fields = importlib.util.module_from_spec(HOOK_SPEC)
|
||||
HOOK_SPEC.loader.exec_module(download_fields)
|
||||
|
||||
|
||||
def test_the_openapi_yaml_documents_each_api_dl_option_with_its_default():
|
||||
"""The download request body documents every dl option the API takes, with its API default, beside its own
|
||||
request keys. A missing default reads as null."""
|
||||
properties = download_fields.request_schema()["properties"]
|
||||
assert DEFAULT_DOWNLOAD_PARAMS.keys() <= properties.keys()
|
||||
assert properties.keys() - DEFAULT_DOWNLOAD_PARAMS.keys() == {"service", "title_id", "remote", "client_name", "cdm"}
|
||||
assert {key: properties[key].get("default") for key in DEFAULT_DOWNLOAD_PARAMS} == DEFAULT_DOWNLOAD_PARAMS
|
||||
|
||||
|
||||
def test_the_endpoint_docs_name_each_api_dl_option():
|
||||
"""The POST /api/download table of the endpoint reference has a row for every dl option the API takes."""
|
||||
page = (Path(__file__).parents[3] / "docs/dev/rest-api/endpoints.md").read_text(encoding="utf-8")
|
||||
section = page.split("### `POST /api/download`", 1)[1].split("\n### ", 1)[0]
|
||||
rows = [line.split("|")[1] for line in section.splitlines() if line.startswith("| `")]
|
||||
assert DEFAULT_DOWNLOAD_PARAMS.keys() <= {name for row in rows for name in re.findall(r"`(\w+)`", row)}
|
||||
def test_the_docs_hook_renders_a_row_for_each_request_field():
|
||||
"""mkdocs.yml loads the hook, which renders one row for each request body field."""
|
||||
assert "docs/hooks/download_fields.py" in (ROOT / "mkdocs.yml").read_text(encoding="utf-8")
|
||||
schema = download_fields.request_schema()
|
||||
rows = download_fields.render(schema).splitlines()[2:]
|
||||
assert [row.split("`")[1] for row in rows] == list(schema["properties"])
|
||||
|
||||
|
||||
def test_api_defaults_cast_to_the_dl_defaults():
|
||||
|
||||
Reference in New Issue
Block a user