|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | from model2vec.model import StaticModel |
| 9 | +from model2vec.persistence.hf import maybe_get_cached_model_path |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def test_local_loading(mock_static_model: StaticModel) -> None: |
@@ -49,3 +50,30 @@ def test_garbage(mock_static_model: StaticModel) -> None: |
49 | 50 | shutil.move(dir_name_path / "model.safetensors", dir_name_path / "model.safetenso") |
50 | 51 | with pytest.raises(ValueError): |
51 | 52 | StaticModel.from_pretrained(dir_name) |
| 53 | + |
| 54 | + |
| 55 | +def test_maybe_get_cached_model_path() -> None: |
| 56 | + """Test cached model path.""" |
| 57 | + model_id = "t/t" |
| 58 | + with TemporaryDirectory() as temp_dir: |
| 59 | + with patch("model2vec.persistence.hf.HF_HUB_CACHE", temp_dir): |
| 60 | + # No slash |
| 61 | + assert maybe_get_cached_model_path("t") is None |
| 62 | + # More than 1 slash |
| 63 | + assert maybe_get_cached_model_path("t/t/t") is None |
| 64 | + # Not created yet |
| 65 | + assert maybe_get_cached_model_path(model_id) is None |
| 66 | + normalized = model_id.replace("/", "--") |
| 67 | + repo_dir = Path(temp_dir) / f"models--{normalized}" |
| 68 | + repo_dir.mkdir(parents=True) |
| 69 | + # Snapshot not created |
| 70 | + assert maybe_get_cached_model_path(model_id) is None |
| 71 | + with_snapshot = repo_dir / "snapshots" |
| 72 | + with_snapshot.mkdir(parents=True, exist_ok=True) |
| 73 | + # No snapshots yet |
| 74 | + assert maybe_get_cached_model_path(model_id) == None |
| 75 | + repo_dir_a = with_snapshot / "a" |
| 76 | + repo_dir_a.mkdir(parents=True, exist_ok=True) |
| 77 | + repo_dir_b = with_snapshot / "b" |
| 78 | + repo_dir_b.mkdir(parents=True, exist_ok=True) |
| 79 | + assert maybe_get_cached_model_path(model_id) == repo_dir_b |
0 commit comments