ford_qc/tests/test_image_format_check.py
2025-09-03 07:03:21 -05:00

94 lines
No EOL
3.8 KiB
Python

import os
import sys
import json
import tempfile
import shutil
from PIL import Image
from pathlib import Path
# Add the parent directory to the path so we can import the checks
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from checks.image_format_check import run_check
def test_image_format_check():
"""Test the image format check for lifestyle and inventory image types"""
with tempfile.TemporaryDirectory() as temp_dir:
# Create test images
# 1. PNG lifestyle image (should fail - expecting JPEG)
lifestyle_png_path = os.path.join(temp_dir, "lifestyle.png")
Image.new('RGB', (100, 100), color='red').save(lifestyle_png_path, 'PNG')
# 2. JPEG lifestyle image (should pass)
lifestyle_jpg_path = os.path.join(temp_dir, "lifestyle.jpg")
Image.new('RGB', (100, 100), color='blue').save(lifestyle_jpg_path, 'JPEG')
# 3. PNG inventory image (should fail - expecting JPEG)
inventory_png_path = os.path.join(temp_dir, "inventory.png")
Image.new('RGB', (100, 100), color='green').save(inventory_png_path, 'PNG')
# 4. JPEG inventory image (should pass)
inventory_jpg_path = os.path.join(temp_dir, "inventory.jpg")
Image.new('RGB', (100, 100), color='yellow').save(inventory_jpg_path, 'JPEG')
# Create a test linkingrecord.json with lifestyle and inventory entries
linkingrecord = {
"items": [
{
"conditions": {
"viewtype": "lifestyle"
},
"records": [
{
"assets": [
{"filename": "lifestyle.png"},
{"filename": "lifestyle.jpg"}
]
}
]
},
{
"conditions": {
"viewtype": "inventory"
},
"records": [
{
"assets": [
{"filename": "inventory.png"},
{"filename": "inventory.jpg"}
]
}
]
}
]
}
# Write the linkingrecord.json to the temporary directory
linkingrecord_path = os.path.join(temp_dir, "linkingrecord.json")
with open(linkingrecord_path, 'w') as f:
json.dump(linkingrecord, f)
# Run the check
config = {
"working_dir": temp_dir,
"linkingrecord_filename": "linkingrecord.json"
}
result = run_check(config)
# Check that we got a failure result with exactly 2 failed images
assert result["status"] == "failed", f"Expected status 'failed', got '{result['status']}'"
assert len(result["details"]["failed_images"]) == 2, f"Expected 2 failed images, got {len(result['details']['failed_images'])}"
# Check that the failed images are the PNG ones
failed_filenames = [img["filename"] for img in result["details"]["failed_images"]]
assert "lifestyle.png" in failed_filenames, "lifestyle.png should fail format check"
assert "inventory.png" in failed_filenames, "inventory.png should fail format check"
# Check that the expected format is JPEG
for img in result["details"]["failed_images"]:
assert img["expected_format"] == "JPEG", f"Expected format for {img['filename']} should be JPEG"
assert img["actual_format"] == "PNG", f"Actual format for {img['filename']} should be PNG"
print("✅ All tests passed! Lifestyle and inventory image format checks are working correctly.")
if __name__ == "__main__":
test_image_format_check()