Skip to content

Commit

Permalink
Merge pull request #324 from OpenFreeEnergy/issue-297
Browse files Browse the repository at this point in the history
Closes #297
  • Loading branch information
dotsdl authored Nov 11, 2024
2 parents 2cf0ab5 + c1b9fad commit fe387b0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
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 @@ def check_existence(
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(
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 @@ def __eq__(self, other):

@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`")

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

0 comments on commit fe387b0

Please sign in to comment.