Skip to content

Commit

Permalink
Improve api_url detail usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvanrun committed Nov 12, 2024
1 parent b1690af commit 5546156
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 4 additions & 3 deletions gcapi/apibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ def iterate_all(self, params=None) -> Iterator[T]:
def detail(
self, pk=None, api_url=None, **params
) -> Generator[T, dict[Any, Any], T]:
if all((pk, params)):
raise ValueError("Only one of pk or params must be specified")

if sum(bool(arg) for arg in [pk, api_url, params]) != 1:
raise ValueError(
"Exactly one of pk, api_url, or params must be specified"
)
if pk is not None or api_url is not None:
if pk is not None:
request_kwargs = dict(path=urljoin(self.base_path, pk + "/"))
Expand Down
20 changes: 19 additions & 1 deletion tests/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def test_get_reader_study_by_slug(local_grand_challenge):
assert by_pk == by_slug == by_api_url


@pytest.mark.parametrize("key", ["slug", "pk"])
@pytest.mark.parametrize("key", ["slug", "pk", "api_url"])
def test_detail_no_objects(local_grand_challenge, key):
c = Client(
base_url=local_grand_challenge, verify=False, token=READERSTUDY_TOKEN
Expand All @@ -378,6 +378,24 @@ def test_detail_no_objects(local_grand_challenge, key):
c.reader_studies.detail(**{key: "foo"})


@pytest.mark.parametrize(
"keys",
(
("api_url", "pk", "slug"),
("api_url", "pk"),
("api_url", "slug"),
("pk", "slug"),
),
)
def test_detail_multiple_args(local_grand_challenge, keys):
c = Client(
base_url=local_grand_challenge, verify=False, token=READERSTUDY_TOKEN
)

with pytest.raises(ValueError):
c.reader_studies.detail(**{k: "foo" for k in keys})


def test_detail_multiple_objects(local_grand_challenge):
c = Client(token=ADMIN_TOKEN, base_url=local_grand_challenge, verify=False)

Expand Down

0 comments on commit 5546156

Please sign in to comment.