58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
"""montage_pngs 单元测试。"""
|
|
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
|
def _make_test_png(path, w=200, h=100, color="red"):
|
|
"""创建测试用 PNG。"""
|
|
img = Image.new("RGB", (w, h), color)
|
|
img.save(path)
|
|
return str(path)
|
|
|
|
|
|
def test_montage_basic():
|
|
"""4 张图拼成 2x2 网格。"""
|
|
from ts_anomaly_td.visualize import montage_pngs
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
paths = []
|
|
for i, color in enumerate(["red", "green", "blue", "yellow"]):
|
|
p = Path(tmpdir) / f"img_{i}.png"
|
|
_make_test_png(p, color=color)
|
|
paths.append(str(p))
|
|
|
|
out = Path(tmpdir) / "montage.png"
|
|
result = montage_pngs(paths, str(out), cols=2, title="Test")
|
|
assert Path(result).exists()
|
|
assert Path(result).stat().st_size > 1000
|
|
|
|
|
|
def test_montage_uneven():
|
|
"""3 张图拼成 2 列(第二行只 1 张)。"""
|
|
from ts_anomaly_td.visualize import montage_pngs
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
paths = []
|
|
for i in range(3):
|
|
p = Path(tmpdir) / f"img_{i}.png"
|
|
_make_test_png(p)
|
|
paths.append(str(p))
|
|
|
|
out = Path(tmpdir) / "montage_uneven.png"
|
|
result = montage_pngs(paths, str(out), cols=2)
|
|
assert Path(result).exists()
|
|
|
|
|
|
def test_montage_empty():
|
|
"""空列表应抛 ValueError。"""
|
|
from ts_anomaly_td.visualize import montage_pngs
|
|
|
|
try:
|
|
montage_pngs([], "/dev/null")
|
|
assert False, "应抛 ValueError"
|
|
except ValueError:
|
|
pass
|