Skip to content

Commit 3731db9

Browse files
feat: add test for _collect_file_metadata function (#186)
* 🧪 Test _collect_file_metadata function Co-authored-by: n24q02m <135627235+n24q02m@users.noreply.github.com> * 🧪 Test _collect_file_metadata function Co-authored-by: n24q02m <135627235+n24q02m@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent d8422d1 commit 3731db9

1 file changed

Lines changed: 58 additions & 3 deletions

File tree

tests/test_model_management.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,65 @@ def test_verify_files_oserror_returns_false(self, mock_snap, mock_info, mock_tre
615615
)
616616
assert result == str(snapshot_dir)
617617

618+
# ---------------------------------------------------------------------------
619+
# TestRetrieveModelGcs
620+
# ---------------------------------------------------------------------------
618621

619-
# ---------------------------------------------------------------------------
620-
# TestRetrieveModelGcs
621-
# ---------------------------------------------------------------------------
622+
@patch("qwen3_embed.common.model_management.list_repo_tree")
623+
@patch("qwen3_embed.common.model_management.model_info")
624+
@patch("qwen3_embed.common.model_management.snapshot_download")
625+
def test_collect_file_metadata_logic(self, mock_snap, mock_info, mock_tree, tmp_path):
626+
"""Tests the internal _collect_file_metadata logic by verifying the saved metadata.json."""
627+
snapshot_dir = tmp_path / "models--org--repo"
628+
snapshot_dir.mkdir(parents=True)
629+
630+
# Create some files:
631+
# 1. A normal file in the root
632+
(snapshot_dir / "model.onnx").write_bytes(b"x" * 500)
633+
634+
# 2. A file in a subdirectory
635+
sub_dir = snapshot_dir / "sub"
636+
sub_dir.mkdir()
637+
(sub_dir / "config.json").write_bytes(b"x" * 100)
638+
639+
# 3. A file that is not in repo_files
640+
(snapshot_dir / "extra.txt").write_bytes(b"x" * 50)
641+
642+
# 4. The metadata file itself (should be ignored)
643+
# removed to force re-collection
644+
645+
repo_files = [
646+
make_repo_file("model.onnx", size=500, oid="aaa"),
647+
make_repo_file("config.json", size=100, oid="bbb"),
648+
]
649+
650+
mock_info.return_value = Mock(sha="rev123")
651+
mock_tree.return_value = repo_files
652+
mock_snap.return_value = str(snapshot_dir)
653+
654+
ModelManagement.download_files_from_huggingface(
655+
hf_source_repo="org/repo",
656+
cache_dir=str(tmp_path),
657+
extra_patterns=["model.onnx"],
658+
)
659+
660+
meta_file = snapshot_dir / ModelManagement.METADATA_FILE
661+
assert meta_file.exists()
662+
663+
metadata = json.loads(meta_file.read_text())
664+
665+
# Verify metadata dictionary contents
666+
assert "model.onnx" in metadata
667+
assert metadata["model.onnx"] == {"size": 500, "blob_id": "aaa"}
668+
669+
assert "sub/config.json" in metadata
670+
assert metadata["sub/config.json"] == {"size": 100, "blob_id": "bbb"}
671+
672+
assert "extra.txt" not in metadata
673+
assert ModelManagement.METADATA_FILE not in metadata
674+
675+
676+
# # ---------------------------------------------------------------------------
622677

623678

624679
class TestRetrieveModelGcs:

0 commit comments

Comments
 (0)