-
Notifications
You must be signed in to change notification settings - Fork 3
feat: look up compounds by name, SMILES, or InChI (#465) #817
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright 2026 Open Reaction Database Project Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Tests for the compound-resolution endpoint, focused on identifier-type routing (#465).""" | ||
|
|
||
| from fastapi import status | ||
|
|
||
| from ord_app.service_api.resources.v1 import utilities | ||
|
|
||
|
|
||
| def test_resolve_compound_smiles_canonicalizes_locally( | ||
| api_client, mock_authenticated_user, monkeypatch | ||
| ): | ||
| # A SMILES is already a structure: canonicalize locally, never hit the remote resolvers. | ||
| remote_calls = [] | ||
|
|
||
| async def fake_remote(value_type, identifier): | ||
|
Check warning on line 27 in ord_app/service_api/resources/v1/utilities_test.py
|
||
| remote_calls.append((value_type, identifier)) | ||
| return ("PubChem API", "unused") | ||
|
Check failure on line 29 in ord_app/service_api/resources/v1/utilities_test.py
|
||
|
|
||
| monkeypatch.setattr(utilities, "name_resolve_cached", fake_remote) | ||
| monkeypatch.setattr( | ||
| utilities, "canonicalize_smiles_cached", lambda smiles: f"canon:{smiles}" | ||
| ) | ||
|
|
||
| response = api_client.post( | ||
| "/api/v1/resolve-compound", | ||
|
Check failure on line 37 in ord_app/service_api/resources/v1/utilities_test.py
|
||
| json={"identifier_type": "smiles", "identifier": "C(C)O"}, | ||
| ).raise_for_status() | ||
|
|
||
| assert response.json() == {"smiles": "canon:C(C)O", "resolver": "RDKit"} | ||
| assert remote_calls == [] # no remote lookup for a SMILES | ||
|
|
||
|
|
||
| def test_resolve_compound_threads_identifier_type( | ||
| api_client, mock_authenticated_user, monkeypatch | ||
| ): | ||
| # Non-SMILES types (name/InChI) go to the remote resolvers with the type passed through. | ||
| remote_calls = [] | ||
|
|
||
| async def fake_remote(value_type, identifier): | ||
|
Check warning on line 51 in ord_app/service_api/resources/v1/utilities_test.py
|
||
| remote_calls.append((value_type, identifier)) | ||
| return ("PubChem API", "O") | ||
|
|
||
| monkeypatch.setattr(utilities, "name_resolve_cached", fake_remote) | ||
| monkeypatch.setattr(utilities, "canonicalize_smiles_cached", lambda smiles: smiles) | ||
|
|
||
| response = api_client.post( | ||
| "/api/v1/resolve-compound", | ||
| json={"identifier_type": "inchi", "identifier": "InChI=1S/H2O/h1H2"}, | ||
| ).raise_for_status() | ||
|
|
||
| assert response.json() == {"smiles": "O", "resolver": "PubChem API"} | ||
| assert remote_calls == [("inchi", "InChI=1S/H2O/h1H2")] | ||
|
|
||
|
|
||
| def test_resolve_compound_unresolvable_returns_400( | ||
| api_client, mock_authenticated_user, monkeypatch | ||
| ): | ||
| # Every resolver failing yields None; surface a clean 400 rather than a 500 from unpacking None. | ||
| async def fake_remote(value_type, identifier): | ||
|
Check warning on line 71 in ord_app/service_api/resources/v1/utilities_test.py
|
||
| return None | ||
|
|
||
| monkeypatch.setattr(utilities, "name_resolve_cached", fake_remote) | ||
|
|
||
| response = api_client.post( | ||
| "/api/v1/resolve-compound", | ||
| json={"identifier_type": "name", "identifier": "nonexistent-compound"}, | ||
| ) | ||
| assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
identifierTypefield is typed asstring, which is looser than what the backend accepts and what the UI dropdown enforces. Narrowing it to a union literal lets the TypeScript compiler catch any mismatch at the call sites.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!