Skip to content

Commit abe6416

Browse files
committed
Add tests for huggingface
Signed-off-by: Tushar Goel <[email protected]>
1 parent 2add3e3 commit abe6416

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/fetchcode/huggingface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_download_url(cls, purl: str):
3636
return None
3737

3838
revision = p.version or "main"
39-
model_id = p.name
39+
model_id = f"{p.namespace}/{p.name}" if p.namespace else p.name
4040
q = p.qualifiers or {}
4141

4242
api_url = f"https://huggingface.co/api/models/{model_id}?revision={revision}"

tests/test_huggingface.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# fetchcode is a free software tool from nexB Inc. and others.
2+
# Visit https://github.com/aboutcode-org/fetchcode for support and download.
3+
#
4+
# Copyright (c) nexB Inc. and others. All rights reserved.
5+
# http://nexb.com and http://aboutcode.org
6+
#
7+
# This software is licensed under the Apache License version 2.0.
8+
#
9+
# You may not use this software except in compliance with the License.
10+
# You may obtain a copy of the License at:
11+
# http://apache.org/licenses/LICENSE-2.0
12+
# Unless required by applicable law or agreed to in writing, software distributed
13+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations under the License.
16+
17+
from unittest.mock import patch
18+
19+
from fetchcode.huggingface import Huggingface
20+
21+
22+
def test_returns_bin_file_url():
23+
purl = "pkg:huggingface/facebook/opt-350m"
24+
revision = "main"
25+
expected_url = "https://huggingface.co/facebook/opt-350m/resolve/main/pytorch_model.bin"
26+
27+
mock_data = {
28+
"siblings": [
29+
{"rfilename": "config.json"},
30+
{"rfilename": "pytorch_model.bin"},
31+
]
32+
}
33+
34+
with patch("fetchcode.huggingface.fetch_json_response", return_value=mock_data):
35+
result = Huggingface.get_download_url(purl)
36+
assert result == expected_url
37+
38+
39+
def test_no_executable_files_returns_none():
40+
purl = "pkg:huggingface/facebook/opt-350m"
41+
mock_data = {
42+
"siblings": [
43+
{"rfilename": "config.json"},
44+
{"rfilename": "tokenizer.json"},
45+
]
46+
}
47+
48+
with patch("fetchcode.huggingface.fetch_json_response", return_value=mock_data):
49+
result = Huggingface.get_download_url(purl)
50+
assert result is None
51+
52+
53+
def test_custom_revision_in_purl():
54+
purl = "pkg:huggingface/facebook/[email protected]"
55+
expected_url = "https://huggingface.co/facebook/opt-350m/resolve/v1.0/pytorch_model.bin"
56+
57+
mock_data = {
58+
"siblings": [
59+
{"rfilename": "pytorch_model.bin"},
60+
]
61+
}
62+
63+
with patch("fetchcode.huggingface.fetch_json_response", return_value=mock_data):
64+
result = Huggingface.get_download_url(purl)
65+
assert result == expected_url

0 commit comments

Comments
 (0)