"""Tests for Hugging Face utilities""" import pytest from src.hf_utils import get_gguf_files_from_repo, extract_username, extract_quantization class TestGetGgufFilesFromRepo: """Tests for get_gguf_files_from_repo function""" def test_finds_gguf_files(self, monkeypatch, test_repo_id): from huggingface_hub import HfApi def _mock_list_repo_files(repo_id: str): assert repo_id == test_repo_id return [ "README.md", "model.gguf", "nested/other.gguf", "nested/config.json", ] monkeypatch.setattr(HfApi, "list_repo_files", lambda self, rid: _mock_list_repo_files(rid)) api = HfApi() gguf_files = get_gguf_files_from_repo(test_repo_id, api) assert isinstance(gguf_files, list) assert len(gguf_files) > 0 assert all(f.endswith(".gguf") for f in gguf_files) def test_returns_empty_for_invalid_repo(self, monkeypatch): from huggingface_hub import HfApi def _raise_repo_not_found(self, rid: str): raise Exception("not found") monkeypatch.setattr(HfApi, "list_repo_files", _raise_repo_not_found) api = HfApi() gguf_files = get_gguf_files_from_repo("nonexistent/repo-12345", api) assert isinstance(gguf_files, list) assert len(gguf_files) == 0 def test_returns_empty_for_repo_without_gguf(self, monkeypatch): from huggingface_hub import HfApi monkeypatch.setattr( HfApi, "list_repo_files", lambda self, rid: ["README.md", "config.json", "pytorch_model.bin"], ) api = HfApi() gguf_files = get_gguf_files_from_repo("bert-base-uncased", api) assert isinstance(gguf_files, list) assert len(gguf_files) == 0 class TestExtractUsername: """Tests for extract_username function""" def test_extracts_from_dict_with_name(self): user_info = {"name": "testuser", "email": "test@example.com"} assert extract_username(user_info) == "testuser" def test_extracts_from_dict_with_username(self): user_info = {"username": "testuser", "email": "test@example.com"} assert extract_username(user_info) == "testuser" def test_prefers_name_over_username(self): user_info = {"name": "preferred", "username": "fallback"} assert extract_username(user_info) == "preferred" def test_extracts_from_object_with_name(self): class UserInfo: name = "testuser" assert extract_username(UserInfo()) == "testuser" def test_extracts_from_object_with_username(self): class UserInfo: username = "testuser" assert extract_username(UserInfo()) == "testuser" def test_returns_none_for_empty_dict(self): assert extract_username({}) is None def test_returns_none_for_none_input(self): assert extract_username(None) is None class TestExtractQuantization: """Tests for extract_quantization function""" def test_extracts_q8_0(self): filename = "Qwen3-0.6B-Q8_0.gguf" assert extract_quantization(filename) == "Q8_0" def test_extracts_q4_k_m(self): filename = "model-Q4_K_M.gguf" assert extract_quantization(filename) == "Q4_K_M" def test_extracts_q5_0(self): filename = "model-Q5_0.gguf" assert extract_quantization(filename) == "Q5_0" def test_extracts_q6_k(self): filename = "model-Q6_K.gguf" assert extract_quantization(filename) == "Q6_K" def test_extracts_q3_k_s(self): filename = "model-Q3_K_S.gguf" assert extract_quantization(filename) == "Q3_K_S" def test_extracts_q2_k(self): filename = "model-Q2_K.gguf" assert extract_quantization(filename) == "Q2_K" def test_returns_unknown_for_no_quantization(self): filename = "model-no-quant.gguf" assert extract_quantization(filename) == "Unknown" def test_case_insensitive(self): filename = "model-q8_0.gguf" assert extract_quantization(filename) == "q8_0" def test_extracts_f16(self): filename = "granite-4.0-350m-F16.gguf" assert extract_quantization(filename) == "F16" def test_extracts_bf16(self): filename = "granite-4.0-350m-BF16.gguf" assert extract_quantization(filename) == "BF16"