Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix click types to call super().convert #61

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions isic_cli/cli/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cli_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down