wrote tests for signal and celery

feature/thumbnails
KarolG 2023-08-18 04:59:57 +02:00
rodzic 6a8d5b735b
commit dc64fd56ae
3 zmienionych plików z 52 dodań i 2 usunięć

Wyświetl plik

@ -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)
field=fieldfile.field.name)

Wyświetl plik

@ -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"])

Wyświetl plik

@ -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",
)