wrote tests for signal and celery
rodzic
6a8d5b735b
commit
dc64fd56ae
|
@ -541,7 +541,6 @@ class OrderDocument(models.Model):
|
||||||
return pdfkit.from_string(content, False)
|
return pdfkit.from_string(content, False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(saved_file)
|
@receiver(saved_file)
|
||||||
def generate_thumbnails_async(sender, fieldfile, **kwargs):
|
def generate_thumbnails_async(sender, fieldfile, **kwargs):
|
||||||
tasks.generate_thumbnails.delay(
|
tasks.generate_thumbnails.delay(
|
||||||
|
|
|
@ -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"])
|
|
@ -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",
|
||||||
|
)
|
Ładowanie…
Reference in New Issue