Files
unshackle/tests/remote/unit/test_remote_flag_options.py
T

82 lines
3.1 KiB
Python

"""A service --flag/--no-flag option over --remote: the listing, the client command and the params sent to the server."""
from __future__ import annotations
import json
from typing import Any
from unittest.mock import patch
import click
import pytest
from unshackle.core.api import handlers, routes
from unshackle.core.remote_service import remote_service_params
from unshackle.core.service import Service
from unshackle.core.services import Services
class EXAMPLE(Service):
"""Example service."""
@staticmethod
@click.command(name="EXAMPLE")
@click.argument("title", type=str)
@click.option("--flag/--no-flag", default=True)
@click.option("--loud", is_flag=True, default=False)
@click.option("--quiet/--no-quiet", " /-Q", default=lambda: True)
@click.pass_context
def cli(ctx: click.Context, **kwargs: Any) -> dict[str, Any]:
return kwargs
@pytest.fixture
def listing(monkeypatch: pytest.MonkeyPatch) -> dict[str, Any]:
monkeypatch.setattr(Services, "load", staticmethod(lambda tag: EXAMPLE))
monkeypatch.setattr(routes, "SERVICE_DETAILS", {})
return dict(json.loads(json.dumps(routes.service_details("EXAMPLE"))))
def sent(info: dict[str, Any], argv: list[str]) -> dict[str, Any]:
with patch.object(Services, "fetch_remote_services", return_value=[info]):
cmd = Services.make_remote_command("EXAMPLE", None)
params = cmd.make_context("EXAMPLE", [*argv, "an-id"]).params
return remote_service_params(info, {"service_params": {k: v for k, v in params.items() if k != "title"}})
def served(params: dict[str, Any]) -> dict[str, Any]:
ctx = click.Context(click.Command("dl"))
return dict(handlers.instantiate_service(ctx, EXAMPLE, "an-id", {"service_params": params}))
def test_no_flag_reaches_the_server_service(listing: dict[str, Any]) -> None:
params = sent(listing, ["--no-flag"])
assert params == {"flag": False}
assert served(params) == {"title": "an-id", "flag": False, "loud": False, "quiet": True}
def test_a_secondary_only_alias_with_a_callable_default_reaches_the_server(listing: dict[str, Any]) -> None:
params = sent(listing, ["-Q"])
assert params == {"flag": True, "quiet": False}
assert served(params)["quiet"] is False
def test_a_default_value_is_sent_as_before(listing: dict[str, Any]) -> None:
assert sent(listing, []) == {"flag": True}
assert sent(listing, ["--loud"]) == {"flag": True, "loud": True}
def test_a_listing_without_secondary_names_keeps_the_primary_flag_only(listing: dict[str, Any]) -> None:
for p in listing["cli_params"]:
p.pop("secondary_opts", None)
assert sent(listing, ["--flag"]) == {"flag": True}
with pytest.raises(click.NoSuchOption):
sent(listing, ["--no-flag"])
def test_a_rest_relay_sends_false_for_a_default_true_flag(listing: dict[str, Any]) -> None:
assert remote_service_params(listing, {"service_params": {"flag": False, "loud": False}}) == {"flag": False}
options, _ = handlers.requested_service_options(
{"service_params": {"flag": False}}, EXAMPLE.cli.params, handlers.DL_TRANSPORT_KEYS
)
assert options == {"flag": False}