From dc64fd56ae11f6e0ab520c357bd51110813f4afd Mon Sep 17 00:00:00 2001 From: KarolG Date: Fri, 18 Aug 2023 04:59:57 +0200 Subject: [PATCH] wrote tests for signal and celery --- artel/store/models.py | 3 +-- artel/store/tests/test_celery.py | 28 ++++++++++++++++++++++++++++ artel/store/tests/test_signals.py | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 artel/store/tests/test_celery.py create mode 100644 artel/store/tests/test_signals.py diff --git a/artel/store/models.py b/artel/store/models.py index 72c2b9a..d2a0e34 100644 --- a/artel/store/models.py +++ b/artel/store/models.py @@ -541,9 +541,8 @@ class OrderDocument(models.Model): return pdfkit.from_string(content, False) - @receiver(saved_file) def generate_thumbnails_async(sender, fieldfile, **kwargs): tasks.generate_thumbnails.delay( model=sender, pk=fieldfile.instance.pk, - field=fieldfile.field.name) \ No newline at end of file + field=fieldfile.field.name) diff --git a/artel/store/tests/test_celery.py b/artel/store/tests/test_celery.py new file mode 100644 index 0000000..8c39665 --- /dev/null +++ b/artel/store/tests/test_celery.py @@ -0,0 +1,28 @@ +from django.test import TestCase +from unittest.mock import patch +from store.tests.factories import ProductTemplateFactory +from store.models import ProductTemplateImage +from django.core.files.uploadedfile import SimpleUploadedFile +from artel.tasks import generate_thumbnails + + +class CeleryTaskTest(TestCase): + @patch('artel.tasks.generate_thumbnails') + def test_generate_thumbnails(self, mock_generate_thumbnails): + template_instance = ProductTemplateFactory() + image_path = "media/dow.jpg" + image_file = SimpleUploadedFile("test_image.jpg", + open(image_path, "rb").read(), + content_type="image/jpeg") + instance_mock = ProductTemplateImage( + template=template_instance, + image=image_file, + is_main=True + ) + instance_mock.save() + instance_pk = instance_mock.pk + + result = generate_thumbnails(model=instance_mock.__class__, + pk=instance_pk, + field="image") + self.assertTrue(result["status"]) diff --git a/artel/store/tests/test_signals.py b/artel/store/tests/test_signals.py new file mode 100644 index 0000000..7ed0daa --- /dev/null +++ b/artel/store/tests/test_signals.py @@ -0,0 +1,23 @@ +from django.test import TestCase +from django.core.files.uploadedfile import SimpleUploadedFile +from unittest.mock import patch +from store.tests.factories import ProductTemplateFactory +from store.models import ProductTemplateImage + + +class SignalTest(TestCase): + @patch("artel.tasks.generate_thumbnails.delay") + def test_generate_thumbnails_async_signal(self, mock_generate_thumbnails): + template_instance = ProductTemplateFactory() + + instance_mock = ProductTemplateImage.objects.create( + template=template_instance, + image=SimpleUploadedFile("test_image.jpg", b"fake_content"), + is_main=True + ) + + mock_generate_thumbnails.assert_called_once_with( + model=instance_mock.__class__, + pk=instance_mock.pk, + field="image", + )