Mi2dRPGamEng/tests/test_export_blank_textures.py

63 lines
2.2 KiB
Python

from pathlib import Path
import pytest
from PIL import Image
from engine.defs import DefRegistry
from engine.placeholder_textures import TEXTURE_SIZE
from scripts.export_blank_textures import export_blank_textures
DEFS_DIR = Path(__file__).resolve().parent.parent / "resources" / "defs"
@pytest.fixture()
def registry():
return DefRegistry.load(DEFS_DIR)
def test_writes_a_correctly_sized_blank_file_for_a_known_item(registry, tmp_path):
export_blank_textures(registry, tmp_path)
staff_def = registry.item_defs["staff_oak"]
path = tmp_path / staff_def.texture
assert path.exists()
with Image.open(path) as img:
assert img.size == (TEXTURE_SIZE, TEXTURE_SIZE)
assert img.mode == "RGBA"
def test_written_image_is_mostly_transparent(registry, tmp_path):
export_blank_textures(registry, tmp_path)
staff_def = registry.item_defs["staff_oak"]
with Image.open(tmp_path / staff_def.texture) as img:
alphas = img.tobytes()[3::4] # every 4th byte of an RGBA buffer is alpha
# a small label is drawn, but the vast majority of a 32x32 canvas stays untouched
assert alphas.count(0) > len(alphas) * 0.5
def test_covers_items_and_body_parts_too(registry, tmp_path):
export_blank_textures(registry, tmp_path)
item_def = next(d for d in registry.item_defs.values() if d.texture)
part_def = next(d for d in registry.body_part_defs.values() if d.texture)
assert (tmp_path / item_def.texture).exists()
assert (tmp_path / part_def.texture).exists()
def test_reports_written_and_skipped_counts(registry, tmp_path):
written_first, skipped_first = export_blank_textures(registry, tmp_path)
assert written_first > 0
assert skipped_first == 0
written_second, skipped_second = export_blank_textures(registry, tmp_path)
assert written_second == 0
assert skipped_second == written_first
def test_does_not_overwrite_a_file_an_artist_already_replaced(registry, tmp_path):
export_blank_textures(registry, tmp_path)
staff_def = registry.item_defs["staff_oak"]
path = tmp_path / staff_def.texture
path.write_bytes(b"real-art-not-a-real-png")
export_blank_textures(registry, tmp_path)
assert path.read_bytes() == b"real-art-not-a-real-png"