66
77import duckdb
88import pytest
9- import requests
109
1110from osmsg .exceptions import OsmsgError
1211from osmsg .history import Manifest
1514UTC = dt .UTC
1615
1716
18- class _FakeResponse :
19- def __init__ (self , status_code , chunks , break_after = None ):
20- self .status_code = status_code
21- self ._chunks = chunks
22- self ._break_after = break_after
23-
24- def __enter__ (self ):
25- return self
26-
27- def __exit__ (self , * args ):
28- return False
29-
30- def raise_for_status (self ):
31- pass
32-
33- def iter_content (self , chunk_size ):
34- for index , chunk in enumerate (self ._chunks ):
35- if self ._break_after is not None and index == self ._break_after :
36- raise requests .exceptions .ChunkedEncodingError ("connection broken" )
37- yield chunk
38-
39-
40- def test_download_resumes_after_interruption (tmp_path , monkeypatch ):
41- dest = tmp_path / "rollup.parquet"
42- calls = []
43-
44- def fake_get (url , headers = None , stream = True , timeout = 60 ):
45- calls .append (headers or {})
46- if not headers :
47- return _FakeResponse (200 , [b"aa" , b"bb" , b"cc" ], break_after = 2 )
48- return _FakeResponse (206 , [b"cc" , b"dd" ])
49-
50- monkeypatch .setattr (refresh .requests , "get" , fake_get )
51- refresh ._download_file ("test/repo" , "rollup/x/data.parquet" , dest )
52- assert dest .read_bytes () == b"aabbccdd"
53- assert calls [1 ]["Range" ] == "bytes=4-"
54-
55-
56- def test_download_raises_after_retry_budget (tmp_path , monkeypatch ):
57- dest = tmp_path / "rollup.parquet"
58-
59- def always_break (url , headers = None , stream = True , timeout = 60 ):
60- return _FakeResponse (200 , [b"aa" , b"bb" ], break_after = 0 )
61-
62- monkeypatch .setattr (refresh .requests , "get" , always_break )
63- with pytest .raises (OsmsgError , match = "stalled after" ):
64- refresh ._download_file ("test/repo" , "rollup/x/data.parquet" , dest )
65-
66-
6717def _month_start (year , month ):
6818 return dt .datetime (year , month , 1 , tzinfo = UTC )
6919
@@ -77,9 +27,11 @@ def _write_rollup(path, latest, rows):
7727
7828
7929def _fake_download (latest , manifest_dict ):
80- """Stand in for the HTTP download : materialize each requested file at its scratch destination ."""
30+ """Stand in for huggingface_hub.hf_hub_download : materialize each requested file under into_dir ."""
8131
82- def _download (repo , remote , dest ):
32+ def _download (repo , remote , into_dir ):
33+ dest = into_dir / remote
34+ dest .parent .mkdir (parents = True , exist_ok = True )
8335 if remote .endswith ("manifest.json" ):
8436 dest .write_text (json .dumps (manifest_dict ))
8537 elif "hashtag_changeset" in remote :
@@ -108,7 +60,7 @@ def test_noop_when_already_current(tmp_path, monkeypatch):
10860 def _boom (* args , ** kwargs ):
10961 raise AssertionError ("must not download when already current" )
11062
111- monkeypatch .setattr (refresh , "_download_file " , _boom )
63+ monkeypatch .setattr (refresh , "_download " , _boom )
11264 assert refresh .refresh_artifact ("test/repo" , artifact ) is False
11365
11466
@@ -122,7 +74,7 @@ def test_happy_path_advances_and_swaps(tmp_path, monkeypatch):
12274 new_manifest = {"schema_version" : 1 , "min_month" : "2005-04" , "max_month" : "2026-07" }
12375 _patch_manifests (monkeypatch , artifact , remote_frontier = _month_start (2026 , 8 ), local_frontier = _month_start (2026 , 7 ))
12476 monkeypatch .setattr (
125- refresh , "_download_file " , _fake_download (dt .datetime (2026 , 7 , 31 , 23 , 59 , tzinfo = UTC ), new_manifest )
77+ refresh , "_download " , _fake_download (dt .datetime (2026 , 7 , 31 , 23 , 59 , tzinfo = UTC ), new_manifest )
12678 )
12779
12880 assert refresh .refresh_artifact ("test/repo" , artifact ) is True
@@ -140,7 +92,7 @@ def test_short_rollup_is_rejected_and_live_files_untouched(tmp_path, monkeypatch
14092 _write_rollup (artifact / "hashtag_changeset.parquet" , _month_start (2026 , 6 ), rows = 5 )
14193 _patch_manifests (monkeypatch , artifact , remote_frontier = _month_start (2026 , 8 ), local_frontier = _month_start (2026 , 7 ))
14294 monkeypatch .setattr (
143- refresh , "_download_file " , _fake_download (dt .datetime (2026 , 6 , 15 , tzinfo = UTC ), {"max_month" : "2026-07" })
95+ refresh , "_download " , _fake_download (dt .datetime (2026 , 6 , 15 , tzinfo = UTC ), {"max_month" : "2026-07" })
14496 )
14597
14698 with pytest .raises (OsmsgError , match = "short of frontier" ):
@@ -154,7 +106,7 @@ def test_shrunk_rollup_is_rejected(tmp_path, monkeypatch):
154106 _write_rollup (artifact / "hashtag_changeset.parquet" , _month_start (2026 , 6 ), rows = 50 )
155107 _patch_manifests (monkeypatch , artifact , remote_frontier = _month_start (2026 , 8 ), local_frontier = _month_start (2026 , 7 ))
156108 monkeypatch .setattr (
157- refresh , "_download_file " , _fake_download (dt .datetime (2026 , 7 , 31 , tzinfo = UTC ), {"max_month" : "2026-07" })
109+ refresh , "_download " , _fake_download (dt .datetime (2026 , 7 , 31 , tzinfo = UTC ), {"max_month" : "2026-07" })
158110 )
159111
160112 with pytest .raises (OsmsgError , match = "fewer rows" ):
0 commit comments