Created a layer of abstraction for tests

feature/thumbnails
KarolG 2023-08-19 16:40:37 +02:00
rodzic dc64fd56ae
commit 4d06d1344a
2 zmienionych plików z 33 dodań i 17 usunięć

Wyświetl plik

@ -1,21 +1,22 @@
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
from abc import ABC
class CeleryTaskTest(TestCase):
@patch('artel.tasks.generate_thumbnails')
def test_generate_thumbnails(self, mock_generate_thumbnails):
template_instance = ProductTemplateFactory()
class AbstractThumbnailGenerationTest(TestCase, ABC):
model_factory = None
def test_generate_thumbnails(self):
model_instance = self.model_factory()
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,
instance_mock = self.model_class(
template=model_instance,
image=image_file,
is_main=True
)
@ -26,3 +27,8 @@ class CeleryTaskTest(TestCase):
pk=instance_pk,
field="image")
self.assertTrue(result["status"])
class ProductTemplateImageThumbnailTest(AbstractThumbnailGenerationTest):
model_factory = ProductTemplateFactory
model_class = ProductTemplateImage

Wyświetl plik

@ -3,21 +3,31 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from unittest.mock import patch
from store.tests.factories import ProductTemplateFactory
from store.models import ProductTemplateImage
from abc import ABC
class SignalTest(TestCase):
class AbstractSignalTest(TestCase, ABC):
def setUp(self):
self.template_instance = ProductTemplateFactory()
def create_instance_mock(self):
raise NotImplementedError("Subclasses must implement this method")
@patch("artel.tasks.generate_thumbnails.delay")
def test_generate_thumbnails_async_signal(self, mock_generate_thumbnails):
template_instance = ProductTemplateFactory()
self.instance_mock = self.create_instance_mock()
instance_mock = ProductTemplateImage.objects.create(
template=template_instance,
mock_generate_thumbnails.assert_called_once_with(
model=self.instance_mock.__class__,
pk=self.instance_mock.pk,
field="image",
)
class ProductTemplateImageSignalTest(AbstractSignalTest):
def create_instance_mock(self):
return ProductTemplateImage.objects.create(
template=self.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",
)