mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-21 21:22:11 -03:00
test(example-images): expand coverage for routes and utilities
This commit is contained in:
113
tests/utils/test_example_images_file_manager.py
Normal file
113
tests/utils/test_example_images_file_manager.py
Normal file
@@ -0,0 +1,113 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
from typing import Any, Dict
|
||||
|
||||
import pytest
|
||||
|
||||
from py.services.settings_manager import settings
|
||||
from py.utils.example_images_file_manager import ExampleImagesFileManager
|
||||
|
||||
|
||||
class JsonRequest:
|
||||
def __init__(self, payload: Dict[str, Any], query: Dict[str, str] | None = None) -> None:
|
||||
self._payload = payload
|
||||
self.query = query or {}
|
||||
|
||||
async def json(self) -> Dict[str, Any]:
|
||||
return self._payload
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def restore_settings() -> None:
|
||||
original = settings.settings.copy()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
settings.settings.clear()
|
||||
settings.settings.update(original)
|
||||
|
||||
|
||||
async def test_open_folder_requires_existing_model_directory(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None:
|
||||
settings.settings["example_images_path"] = str(tmp_path)
|
||||
model_hash = "a" * 64
|
||||
model_folder = tmp_path / model_hash
|
||||
model_folder.mkdir()
|
||||
(model_folder / "image.png").write_text("data", encoding="utf-8")
|
||||
|
||||
popen_calls: list[list[str]] = []
|
||||
|
||||
class DummyPopen:
|
||||
def __init__(self, cmd, *_args, **_kwargs):
|
||||
popen_calls.append(cmd)
|
||||
|
||||
monkeypatch.setattr("subprocess.Popen", DummyPopen)
|
||||
|
||||
request = JsonRequest({"model_hash": model_hash})
|
||||
response = await ExampleImagesFileManager.open_folder(request)
|
||||
body = json.loads(response.text)
|
||||
|
||||
assert body["success"] is True
|
||||
assert popen_calls
|
||||
assert model_hash in popen_calls[0][-1]
|
||||
|
||||
|
||||
async def test_open_folder_rejects_invalid_paths(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None:
|
||||
settings.settings["example_images_path"] = str(tmp_path)
|
||||
|
||||
def fake_get_model_folder(_hash):
|
||||
return str(tmp_path.parent / "outside")
|
||||
|
||||
monkeypatch.setattr("py.utils.example_images_file_manager.get_model_folder", fake_get_model_folder)
|
||||
|
||||
request = JsonRequest({"model_hash": "a" * 64})
|
||||
response = await ExampleImagesFileManager.open_folder(request)
|
||||
body = json.loads(response.text)
|
||||
|
||||
assert response.status == 400
|
||||
assert body["success"] is False
|
||||
|
||||
|
||||
async def test_get_files_lists_supported_media(tmp_path) -> None:
|
||||
settings.settings["example_images_path"] = str(tmp_path)
|
||||
model_hash = "b" * 64
|
||||
model_folder = tmp_path / model_hash
|
||||
model_folder.mkdir()
|
||||
(model_folder / "image.png").write_text("data", encoding="utf-8")
|
||||
(model_folder / "video.webm").write_text("data", encoding="utf-8")
|
||||
(model_folder / "notes.txt").write_text("skip", encoding="utf-8")
|
||||
|
||||
request = JsonRequest({}, {"model_hash": model_hash})
|
||||
response = await ExampleImagesFileManager.get_files(request)
|
||||
body = json.loads(response.text)
|
||||
|
||||
assert response.status == 200
|
||||
names = {entry["name"] for entry in body["files"]}
|
||||
assert names == {"image.png", "video.webm"}
|
||||
|
||||
|
||||
async def test_has_images_reports_presence(tmp_path) -> None:
|
||||
settings.settings["example_images_path"] = str(tmp_path)
|
||||
model_hash = "c" * 64
|
||||
model_folder = tmp_path / model_hash
|
||||
model_folder.mkdir()
|
||||
(model_folder / "image.png").write_text("data", encoding="utf-8")
|
||||
|
||||
request = JsonRequest({}, {"model_hash": model_hash})
|
||||
response = await ExampleImagesFileManager.has_images(request)
|
||||
body = json.loads(response.text)
|
||||
|
||||
assert body["has_images"] is True
|
||||
|
||||
empty_request = JsonRequest({}, {"model_hash": "missing"})
|
||||
empty_response = await ExampleImagesFileManager.has_images(empty_request)
|
||||
empty_body = json.loads(empty_response.text)
|
||||
assert empty_body["has_images"] is False
|
||||
|
||||
|
||||
async def test_has_images_requires_model_hash() -> None:
|
||||
response = await ExampleImagesFileManager.has_images(JsonRequest({}, {}))
|
||||
body = json.loads(response.text)
|
||||
assert response.status == 400
|
||||
assert body["success"] is False
|
||||
Reference in New Issue
Block a user