Robohash/tests/test_image_consistency.py
e1ven 3b9066850a Update Robohash for PyPI republishing
Some users have asked for an updated version in PyPI, so I'm doing some minor cleanup.
No real functionality changes, just modernizing things that would be considered more
standard 10+ years after this code was written.

- Added note about maintenance mode in README
- Cleaned up Python 3 compatibility (removed Python 2 support)
- Added minimal test for image consistency
- Added Docker image + GitHub workflow to build/publish it
- Fixed some minor bugs and improved error handling
- Added better CLI help text

The project is now officially in maintenance mode as noted in the README.
2025-03-15 16:30:01 -04:00

41 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""
Test to ensure consistency of Robohash image generation.
This test checks that the output of the Robohash generator matches
expected reference images for specific inputs.
"""
import os
import unittest
from PIL import Image, ImageChops
import io
from robohash import Robohash
class TestRobohashConsistency(unittest.TestCase):
def setUp(self):
self.reference_dir = os.path.join(os.path.dirname(__file__), 'reference')
def test_pi_image_consistency(self):
"""Test that '3.14159' generates the expected image."""
# Load the reference image
reference_path = os.path.join(self.reference_dir, 'pi.png')
reference_img = Image.open(reference_path)
# Generate a new image using the same input
rh = Robohash("3.14159")
rh.assemble(sizex=300, sizey=300)
# Convert the generated image to bytes for comparison
img_buffer = io.BytesIO()
rh.img.save(img_buffer, format="PNG")
img_buffer.seek(0)
generated_img = Image.open(img_buffer)
# Compare the images
diff = ImageChops.difference(reference_img.convert('RGBA'), generated_img.convert('RGBA'))
# If the images are the same, the difference will be all black (0)
self.assertFalse(diff.getbbox(), "Generated image doesn't match the reference image")
if __name__ == '__main__':
unittest.main()