Files
ComfyUI-Lora-Manager/tests/utils/test_utils.py
2025-10-05 14:43:21 +08:00

71 lines
2.2 KiB
Python

import pytest
from py.services.settings_manager import settings
from py.utils.utils import (
calculate_recipe_fingerprint,
calculate_relative_path_for_model,
)
@pytest.fixture
def isolated_settings(monkeypatch):
default_settings = settings._get_default_settings()
default_settings.update(
{
"download_path_templates": {
"lora": "{base_model}/{first_tag}",
"checkpoint": "{base_model}/{first_tag}",
"embedding": "{base_model}/{first_tag}",
},
"base_model_path_mappings": {},
}
)
monkeypatch.setattr(settings, "settings", default_settings)
monkeypatch.setattr(type(settings), "_save_settings", lambda self: None)
return default_settings
def test_calculate_relative_path_for_embedding_replaces_spaces(isolated_settings):
model_data = {
"base_model": "Base Model",
"tags": ["tag with space"],
"civitai": {"id": 1, "creator": {"username": "Author Name"}},
}
relative_path = calculate_relative_path_for_model(model_data, "embedding")
assert relative_path == "Base_Model/tag_with_space"
def test_calculate_relative_path_for_model_uses_mappings_and_defaults(isolated_settings):
isolated_settings["download_path_templates"]["lora"] = "{base_model}/{first_tag}/{author}"
isolated_settings["base_model_path_mappings"] = {"SDXL": "SDXL-mapped"}
model_data = {
"base_model": "SDXL",
"tags": [],
"civitai": {"id": 12, "creator": {"username": "Creator"}},
}
relative_path = calculate_relative_path_for_model(model_data, "lora")
assert relative_path == "SDXL-mapped/no tags/Creator"
def test_calculate_recipe_fingerprint_filters_and_sorts():
loras = [
{"hash": "ABC", "strength": 0.1234},
{"hash": "", "isDeleted": True, "modelVersionId": 42, "strength": 0.5},
{"hash": "def", "weight": 0.345},
{"hash": "skip", "exclude": True, "strength": 0.9},
{"hash": "", "strength": 0.1},
]
fingerprint = calculate_recipe_fingerprint(loras)
assert fingerprint == "42:0.5|abc:0.12|def:0.34"
def test_calculate_recipe_fingerprint_empty_input():
assert calculate_recipe_fingerprint([]) == ""