-
Notifications
You must be signed in to change notification settings - Fork 41
/
test_hfh_util.py
274 lines (220 loc) · 8.73 KB
/
test_hfh_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""Unit test module for Hugging Face Hub utilities."""
import io
import logging
import os.path
import zipfile
from datetime import datetime, timezone
from unittest import mock
import huggingface_hub
import pytest
from huggingface_hub.utils import EntryNotFoundError
import annif.hfh_util
from annif.config import AnnifConfigCFG
from annif.exception import OperationFailedException
@mock.patch("annif.hfh_util._is_repo_in_cache", return_value=False)
def test_download_allowed_trust_repo(mock_is_repo_in_cache, caplog):
trust_repo = True
repo_id = "dummy-repo"
with caplog.at_level(logging.WARNING, logger="annif"):
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id)
assert (
'Download allowed from "dummy-repo" because "--trust-repo" flag is used.'
in caplog.text
)
@mock.patch("annif.hfh_util._is_repo_in_cache", return_value=True)
def test_download_allowed_repo_in_cache(mock_is_repo_in_cache, caplog):
trust_repo = False
repo_id = "dummy-repo"
with caplog.at_level(logging.DEBUG, logger="annif"):
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id)
assert (
'Download allowed from "dummy-repo" because repo is already in cache.'
in caplog.text
)
@mock.patch("huggingface_hub.utils._cache_manager.HFCacheInfo")
@mock.patch("huggingface_hub.scan_cache_dir") # Bypass CacheNotFound on CI/CD
def test_download_not_allowed(mock_scan_cache_dir, mock_HFCacheInfo):
trust_repo = False
repo_id = "dummy-repo"
mock_HFCacheInfo.return_value.repos = frozenset()
with pytest.raises(OperationFailedException) as excinfo:
annif.hfh_util.check_is_download_allowed(trust_repo, repo_id)
assert (
str(excinfo.value)
== 'Cannot download projects from untrusted repo "dummy-repo"'
)
def test_archive_dir(testdatadir):
dirpath = os.path.join(str(testdatadir), "projects", "dummy-fi")
os.makedirs(dirpath, exist_ok=True)
open(os.path.join(str(dirpath), "foo.txt"), "a").close()
open(os.path.join(str(dirpath), "-train.txt"), "a").close()
fobj = annif.hfh_util._archive_dir(dirpath)
assert isinstance(fobj, io.BufferedRandom)
with zipfile.ZipFile(fobj, mode="r") as zfile:
archived_files = zfile.namelist()
assert len(archived_files) == 1
assert os.path.split(archived_files[0])[1] == "foo.txt"
def test_get_project_config(app_project):
result = annif.hfh_util._get_project_config(app_project)
assert isinstance(result, io.BytesIO)
string_result = result.read().decode("UTF-8")
assert "[dummy-en]" in string_result
def test_unzip_archive_initial(testdatadir):
dirpath = os.path.join(str(testdatadir), "projects", "dummy-fi")
fpath = os.path.join(str(dirpath), "file.txt")
annif.hfh_util.unzip_archive(
os.path.join("tests", "huggingface-cache", "projects", "dummy-fi.zip"),
force=False,
)
assert os.path.exists(fpath)
assert os.path.getsize(fpath) == 0 # Zero content from zip
ts = os.path.getmtime(fpath)
assert datetime.fromtimestamp(ts).astimezone(tz=timezone.utc) == datetime(
1980, 1, 1, 0, 0
).astimezone(tz=timezone.utc)
def test_unzip_archive_no_overwrite(testdatadir):
dirpath = os.path.join(str(testdatadir), "projects", "dummy-fi")
fpath = os.path.join(str(dirpath), "file.txt")
os.makedirs(dirpath, exist_ok=True)
with open(fpath, "wt") as pf:
print("Existing content", file=pf)
annif.hfh_util.unzip_archive(
os.path.join("tests", "huggingface-cache", "projects", "dummy-fi.zip"),
force=False,
)
assert os.path.exists(fpath)
assert os.path.getsize(fpath) == 17 # Existing content
assert datetime.now().timestamp() - os.path.getmtime(fpath) < 1
def test_unzip_archive_overwrite(testdatadir):
dirpath = os.path.join(str(testdatadir), "projects", "dummy-fi")
fpath = os.path.join(str(dirpath), "file.txt")
os.makedirs(dirpath, exist_ok=True)
with open(fpath, "wt") as pf:
print("Existing content", file=pf)
annif.hfh_util.unzip_archive(
os.path.join("tests", "huggingface-cache", "projects", "dummy-fi.zip"),
force=True,
)
assert os.path.exists(fpath)
assert os.path.getsize(fpath) == 0 # Zero content from zip
ts = os.path.getmtime(fpath)
assert datetime.fromtimestamp(ts).astimezone(tz=timezone.utc) == datetime(
1980, 1, 1, 0, 0
).astimezone(tz=timezone.utc)
@mock.patch("os.path.exists", return_value=True)
@mock.patch("annif.hfh_util._compute_crc32", return_value=0)
@mock.patch("shutil.copy")
def test_copy_project_config_no_overwrite(copy, _compute_crc32, exists):
annif.hfh_util.copy_project_config(
os.path.join("tests", "huggingface-cache", "dummy-fi.cfg"), force=False
)
assert not copy.called
@mock.patch("os.path.exists", return_value=True)
@mock.patch("shutil.copy")
def test_copy_project_config_overwrite(copy, exists):
annif.hfh_util.copy_project_config(
os.path.join("tests", "huggingface-cache", "dummy-fi.cfg"), force=True
)
assert copy.called
assert copy.call_args == mock.call(
"tests/huggingface-cache/dummy-fi.cfg", "projects.d/dummy-fi.cfg"
)
@mock.patch(
"huggingface_hub.ModelCard.load",
side_effect=EntryNotFoundError("mymessage"),
)
@mock.patch("huggingface_hub.HfFileSystem.glob", return_value=[])
@mock.patch("huggingface_hub.ModelCard")
def test_upsert_modelcard_insert_new(ModelCard, glob, load, project):
repo_id = "annif-user/annif-repo"
token = "mytoken"
revision = "mybranch"
annif.hfh_util.upsert_modelcard(repo_id, [project], token, revision)
ModelCard.assert_called_once()
assert "# annif-repo" in ModelCard.call_args[0][0] # README heading
card = ModelCard.return_value
assert card.data.language == ["fi"]
assert card.data.pipeline_tag == "text-classification"
assert card.data.tags == ["annif"]
card.push_to_hub.assert_called_once_with(
repo_id=repo_id,
token=token,
revision=revision,
commit_message="Create README.md with Annif",
)
@mock.patch("huggingface_hub.ModelCard.push_to_hub")
@mock.patch(
"huggingface_hub.ModelCard.load", # Mock language in existing card
return_value=huggingface_hub.ModelCard("---\nlanguage:\n- en\n---"),
)
@mock.patch("huggingface_hub.HfFileSystem.glob", return_value=["dummy-en.cfg"])
@mock.patch(
"huggingface_hub.HfFileSystem.read_text",
return_value="""
[dummy-en]
name=Dummy English
language=en
vocab=dummy
""",
)
def test_upsert_modelcard_update_existing(read_text, glob, load, push_to_hub, project):
repo_id = "annif-user/annif-repo"
token = "mytoken"
revision = "mybranch"
annif.hfh_util.upsert_modelcard(repo_id, [project], token, revision)
load.assert_called_once_with(repo_id)
card = load.return_value
retained_project_list_content = "dummy-en Dummy English dummy en "
assert retained_project_list_content in card.text
assert sorted(card.data.language) == ["en", "fi"]
card.push_to_hub.assert_called_once_with(
repo_id=repo_id,
token=token,
revision=revision,
commit_message="Update README.md with Annif",
)
def test_update_modelcard_projects_section_append_new():
empty_cfg = AnnifConfigCFG(projstr="")
text = """This is some existing text in the card."""
updated_text = annif.hfh_util._update_projects_section(text, empty_cfg)
expected_tail = """\
<!--- start-of-autoupdating-part --->
## Projects
```
Project ID Project Name Vocabulary ID Language
-------------------------------------------------
```
<!--- end-of-autoupdating-part --->"""
assert updated_text == text + expected_tail
def test_update_modelcard_projects_section_update_existing():
cfg = AnnifConfigCFG(
projstr="""\
[dummy-fi]
name=Dummy Finnish
language=fi
vocab=dummy"""
)
text_head = """This is some existing text in the card.\n"""
text_initial_projects = """\
<!--- start-of-autoupdating-part --->
## Projects
```
Project ID Project Name Vocabulary ID Language
-------------------------------------------------
```
<!--- end-of-autoupdating-part --->\n"""
text_tail = (
"This is text after the Projects section; it should remain after updates."
)
text = text_head + text_initial_projects + text_tail
updated_text = annif.hfh_util._update_projects_section(text, cfg)
expected_updated_projects = """\
<!--- start-of-autoupdating-part --->
## Projects
```
Project ID Project Name Vocabulary ID Language
--------------------------------------------------
dummy-fi Dummy Finnish dummy fi \n```
<!--- end-of-autoupdating-part --->
"""
assert updated_text == text_head + expected_updated_projects + text_tail