-
Notifications
You must be signed in to change notification settings - Fork 1
Implement fetching by doi and custom hashes #61
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
Changes from 7 commits
710500e
a1d0800
68524b6
ff6fd70
e064711
f9856db
57f222e
9522d57
f560c1f
c6eb311
1d67899
4c00163
4389a51
d5eb021
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import json | ||
| import os | ||
| import pathlib | ||
| import shutil | ||
| import urllib.request | ||
|
|
@@ -9,7 +10,11 @@ | |
|
|
||
| import openff.nagl_models._dynamic_fetch | ||
| from openff.nagl_models import __file__ as root | ||
| from openff.nagl_models._dynamic_fetch import get_model | ||
| from openff.nagl_models._dynamic_fetch import ( | ||
| get_model, | ||
| HashComparisonFailedException, | ||
| UnableToParseDOIException, | ||
| ) | ||
|
|
||
|
|
||
| def mocked_urlretrieve(url, filename): | ||
|
|
@@ -59,11 +64,27 @@ def test_get_known_models(monkeypatch, known_model): | |
| assert "OPENFF_NAGL_MODELS" in get_model(known_model) | ||
|
|
||
|
|
||
| def test_access_internet_with_empty_cache(): | ||
| cache_path = platformdirs.user_cache_path() / "OPENFF_NAGL_MODELS" | ||
| @pytest.fixture | ||
| def hide_cache(): | ||
| cache_dir = platformdirs.user_cache_path() / "OPENFF_NAGL_MODELS" | ||
| alt_dir = str(cache_dir) + "_temp" | ||
|
|
||
| if os.path.exists(alt_dir): | ||
| raise FileExistsError(f"Temporary directory already exists: {alt_dir}") | ||
|
|
||
| if os.path.exists(cache_dir): | ||
| shutil.move(cache_dir, alt_dir) | ||
|
|
||
| yield | ||
|
|
||
| if cache_path.exists(): | ||
| shutil.rmtree(cache_path) | ||
| if os.path.exists(alt_dir): | ||
| if os.path.exists(cache_dir): | ||
| shutil.rmtree(cache_dir) | ||
| shutil.move(alt_dir, cache_dir) | ||
|
|
||
|
|
||
| def test_access_internet_with_empty_cache(hide_cache): | ||
| cache_path = platformdirs.user_cache_path() / "OPENFF_NAGL_MODELS" | ||
|
|
||
| disable_socket() | ||
|
|
||
|
|
@@ -147,3 +168,55 @@ def test_all_models_loadable(model, monkeypatch): | |
| ) | ||
|
|
||
| GNNModel.load(get_model(model), eval_mode=True) | ||
|
|
||
|
|
||
| def test_get_model_by_doi_and_hash(hide_cache): | ||
| get_model( | ||
| "my_favorite_model.pt", | ||
| doi="10.5072/zenodo.278300", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This record must be sand-box only? This is my first Google result, which seems unlikely to be what you actually want to point to: https://zenodo.org/records/14335473 A comment or note about where this lives and how the hash was generated would be useful for future developers, I don't think anything else would be necessary here |
||
| file_hash="127eb0b9512f22546f8b455582bcd85b2521866d32b86d231fee26d4771b1d81", | ||
| _sandbox=True, | ||
| ) | ||
|
|
||
|
|
||
| def test_get_model_by_doi_no_hash(hide_cache): | ||
| get_model("my_favorite_model.pt", doi="10.5072/zenodo.278300", _sandbox=True) | ||
|
|
||
|
|
||
| def test_get_model_hash_comparison_fails(): | ||
| with pytest.raises(HashComparisonFailedException): | ||
| get_model( | ||
| "my_favorite_model.pt", | ||
| doi="10.5072/zenodo.278300", | ||
| file_hash="wrong_hash", | ||
| _sandbox=True, | ||
| ) | ||
|
|
||
|
|
||
| def test_user_provided_hash_conflicts_with_known_hash(): | ||
| with pytest.raises(HashComparisonFailedException): | ||
| get_model("openff-gnn-am1bcc-0.1.0-rc.3.pt", file_hash="wrong_hash") | ||
|
|
||
|
|
||
| def test_malformed_doi(monkeypatch, hide_cache): | ||
| with monkeypatch.context() as m: | ||
| m.setattr( | ||
| urllib.request, | ||
| "urlretrieve", | ||
| mocked_urlretrieve, | ||
| ) | ||
| m.setattr( | ||
| openff.nagl_models._dynamic_fetch, | ||
| "get_release_metadata", | ||
| mocked_get_release_metadata, | ||
| ) | ||
|
|
||
| with pytest.raises(UnableToParseDOIException): | ||
| get_model("my_favorite_model.pt", doi="zenodo.278300", _sandbox=True) | ||
|
|
||
|
|
||
| def test_no_matching_file_at_doi(): | ||
| with pytest.raises(FileNotFoundError, match="sandbox.zenodo"): | ||
| get_model( | ||
| "file_that_doesnt_exist.pt", doi="10.5072/zenodo.278300", _sandbox=True | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.