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

Closes #297 #324

Merged
merged 5 commits into from
Nov 11, 2024
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
9 changes: 8 additions & 1 deletion alchemiscale/interface/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@
n4js: Neo4jStore = Depends(get_n4js_depends),
token: TokenData = Depends(get_token_data_depends),
):
sk = ScopedKey.from_str(scoped_key)
try:
sk = ScopedKey.from_str(scoped_key)
except ValueError as e:
raise HTTPException(

Check warning on line 102 in alchemiscale/interface/api.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/interface/api.py#L99-L102

Added lines #L99 - L102 were not covered by tests
status_code=http_status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=e.args[0],
)

validate_scopes(sk.scope, token)

return n4js.check_existence(scoped_key=sk)
Expand Down
2 changes: 1 addition & 1 deletion alchemiscale/interface/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_scoped_key(self, obj: GufeTokenizable, scope: Scope) -> ScopedKey:
"Scope for a ScopedKey must be specific; it cannot contain wildcards."
)

def check_exists(self, scoped_key: ScopedKey) -> bool:
def check_exists(self, scoped_key: ScopedKey | str) -> bool:
"""Returns ``True`` if the given ScopedKey represents an object in the database."""
return self._get_resource(f"/exists/{scoped_key}")

Expand Down
6 changes: 5 additions & 1 deletion alchemiscale/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@

@classmethod
def from_str(cls, string):
prefix, token, org, campaign, project = string.split("-")
try:
prefix, token, org, campaign, project = string.split("-")
except ValueError:
raise ValueError("input does not appear to be a `ScopedKey`")

Check warning on line 157 in alchemiscale/models.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/models.py#L156-L157

Added lines #L156 - L157 were not covered by tests

gufe_key = GufeKey(f"{prefix}-{token}")

return cls(gufe_key=gufe_key, org=org, campaign=campaign, project=project)
Expand Down
28 changes: 28 additions & 0 deletions alchemiscale/tests/integration/interface/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,38 @@ def test_create_network(
network_sks = user_client.query_networks()
assert an_sk in network_sks

assert user_client.check_exists(an_sk)

# TODO: make a network in a scope that doesn't have any components in
# common with an existing network
# user_client.create_network(

def test_check_exists(
self,
scope_test,
n4js_preloaded,
user_client: client.AlchemiscaleClient,
network_tyk2,
):
an_sks = user_client.query_networks()

# check that a known existing AlchemicalNetwork exists
assert user_client.check_exists(an_sks[0])

# check that an AlchemicalNetwork that doesn't exist shows as not existing
an_sk = an_sks[0]
an_sk_nonexistent = ScopedKey(
gufe_key=GufeKey("AlchemicalNetwork-lol"), **scope_test.dict()
)
assert not user_client.check_exists(an_sk_nonexistent)

# check that we get an exception when we try a malformed key
with pytest.raises(
AlchemiscaleClientError,
match="Status Code 422 : Unprocessable Entity : input does not appear to be a `ScopedKey`",
):
user_client.check_exists("lol")

@pytest.mark.parametrize(("state",), [[state.value] for state in NetworkStateEnum])
def test_set_network_state(
self,
Expand Down
Loading