diff --git a/isic_cli/cli/types.py b/isic_cli/cli/types.py index 927283a..b15befa 100644 --- a/isic_cli/cli/types.py +++ b/isic_cli/cli/types.py @@ -13,6 +13,8 @@ class SearchString(click.ParamType): name = "search_string" def convert(self, value, param, ctx): + value = super().convert(value, param, ctx) + r = ctx.obj.session.get("images/search/", params={"query": value, "limit": 1}) if r.status_code == 400 and "detail" in r.json() and "query" in r.json()["detail"]: self.fail('Invalid search query string "%s"' % value, param, ctx) @@ -23,6 +25,8 @@ class CommaSeparatedIdentifiers(click.ParamType): name = "comma_separated_identifiers" def convert(self, value, param, ctx): + value = super().convert(value, param, ctx) + if value != "" and not re.match(r"^(\d+)(,\d+)*$", value): self.fail('Improperly formatted value "%s".' % value, param, ctx) return value @@ -36,6 +40,8 @@ def __init__(self, locked_okay: Optional[bool] = False) -> None: self.locked_okay = locked_okay def convert(self, value: str, param, ctx) -> str: + value = super().convert(value, param, ctx) + try: collection = get_collection(ctx.obj.session, value) except HTTPError as e: @@ -57,6 +63,8 @@ class CohortId(IntParamType): name = "cohort_id" def convert(self, value: str, param, ctx) -> str: + value = super().convert(value, param, ctx) + try: get_cohort(ctx.obj.session, value) except HTTPError as e: diff --git a/tests/test_cli_collection.py b/tests/test_cli_collection.py index 66be944..389d41e 100644 --- a/tests/test_cli_collection.py +++ b/tests/test_cli_collection.py @@ -43,6 +43,18 @@ def cmd(foo): assert "locked for modification" in result.output, result.output +def test_collection_id_type_must_be_integer(mocker): + @click.command() + @click.argument("foo", type=CollectionId()) + def cmd(foo): + pass + + # magicmock is used to mock out ctx.obj + result = CliRunner().invoke(cmd, ["some-non-numeric-string"], obj=mocker.MagicMock()) + assert result.exit_code == 2, result.exit_code + assert "is not a valid" in result.output, result.output + + def test_collection_id_type_access(mocker): @click.command() @click.argument("foo", type=CollectionId())