githubharald / SimpleHTR

Handwritten Text Recognition (HTR) system implemented with TensorFlow.
https://towardsdatascience.com/2326a3487cd5
MIT License
1.99k stars 894 forks source link

Assertion Error in Thumbnail Signal Testing #63

Closed djcasker closed 5 years ago

djcasker commented 5 years ago

I am getting this error when testing my signals Ran 6 tests in 0.156s

FAILED (failures=1) Destroying test database for alias 'default'... (venv) PS C:\Users\Dj CaskeR\Desktop\Django\booktime> python manage.py test Creating test database for alias 'default'... System check identified no issues (0 silenced). ..F...

FAIL: test_thumbnails_are_generated_on_save (main.tests.test_signals.TestSignal)

Traceback (most recent call last): File "C:\Users\Dj CaskeR\Desktop\Django\booktime\main\tests\test_signals.py", line 33, in test_thumbnails_are_generated_on_save assert image.thumbnail.read() == expected_content AssertionError


signals.py file looks like from io import BytesIO import logging from PIL import Image from django.contrib.auth.signals import user_logged_in from django.core.files.base import ContentFile from django.db.models.signals import pre_save, post_save from django.dispatch import receiver from django.conf import settings

from .models import ProductImage

THUMBNAIL_SIZE = (300, 300)

logger = logging.getLogger(name)

@receiver(pre_save, sender=ProductImage) def generate_thumbnail(sender, instance, **kwargs): logger.info( "Generating thumbnail for product %d", instance.product.id, ) image = Image.open(instance.image) image = image.convert("RGB") image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)

temp_thumb = BytesIO()
image.save(temp_thumb, "JPEG")
temp_thumb.seek(0)

# set save=False, otherwise it will run in an infinite loop
instance.thumbnail.save(
    instance.image.name,
    ContentFile(temp_thumb.read()),
    save=False,
)
temp_thumb.close()

test_signals.py file

from django.test import TestCase from main import models from django.core.files.images import ImageFile from decimal import Decimal

class TestSignal(TestCase): def test_thumbnails_are_generated_on_save(self): product = models.Product( name="The cathedral and the bazaar", price=Decimal("10.00"), ) product.save()

    with open(
        "main\\fixtures\\the-cathedral-the-bazaar.jpg", "rb"
    ) as f:
        image = models.ProductImage(
            product=product,
            image=ImageFile(f, name="tctb.jpg"),
        )
        with self.assertLogs("main", level="INFO") as cm:
            image.save()

    self.assertGreaterEqual(len(cm.output), 1)
    image.refresh_from_db()

    with open(
        "main\\fixtures\\the-cathedral-the-bazaar-thumb.jpg",
        "rb",
    ) as f:
        expected_content = f.read()
        assert image.thumbnail.read() == expected_content

    print(expected_content)

    image.thumbnail.delete(save=False)
    image.image.delete(save=False)