54 lines
2.2 KiB
Python
54 lines
2.2 KiB
Python
import hashlib
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
from src.image_pipeline import render
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
class Images(unittest.TestCase):
|
|
def test_public_fixture_and_repeat(self):
|
|
fixture = ROOT / 'tests' / 'fixtures' / 'sample.png'
|
|
original = hashlib.sha256(fixture.read_bytes()).hexdigest()
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
target = Path(directory)
|
|
expected = {
|
|
'thumbnail': ('JPEG', (256, 160)),
|
|
'crop': ('JPEG', (128, 128)),
|
|
'optimized': ('WEBP', (320, 200)),
|
|
}
|
|
first = render(fixture, target)
|
|
self.assertEqual(len(first), len(expected))
|
|
digests = {name: hashlib.sha256((target / name).read_bytes()).hexdigest() for name in expected}
|
|
self.assertEqual(first, render(fixture, target))
|
|
for name, properties in expected.items():
|
|
with Image.open(target / name) as image:
|
|
self.assertEqual((image.format, image.size), properties)
|
|
self.assertEqual(hashlib.sha256((target / name).read_bytes()).hexdigest(), digests[name])
|
|
self.assertEqual(hashlib.sha256(fixture.read_bytes()).hexdigest(), original)
|
|
|
|
def test_orientation_alpha_and_small_image(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
target = Path(directory)
|
|
source = target / 'source.png'
|
|
image = Image.new('RGBA', (35, 15), (255, 0, 0, 0))
|
|
image.save(source, exif=self.orientation())
|
|
render(source, target)
|
|
with Image.open(target / 'thumbnail') as thumbnail:
|
|
self.assertEqual(thumbnail.size, (15, 35))
|
|
self.assertEqual(thumbnail.getpixel((4, 4)), (255, 255, 255))
|
|
self.assertFalse(thumbnail.getexif())
|
|
with Image.open(target / 'optimized') as optimized:
|
|
self.assertEqual(optimized.size, (15, 35))
|
|
self.assertFalse(optimized.getexif())
|
|
|
|
@staticmethod
|
|
def orientation():
|
|
exif = Image.Exif()
|
|
exif[274] = 6
|
|
return exif
|