Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests
Pacu2bcdfc75737aa
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 69b7a787f7..5fef05d991 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -326,14 +326,12 @@ def create_variant(product, **kwargs):def create_product_image(product, placeholder_dir):placeholder_root = os.path.join(settings.PROJECT_ROOT, placeholder_dir)- img_path = '%s/%s' % (placeholder_dir,- random.choice(os.listdir(placeholder_root)))- image = ProductImage(- product=product,- image=File(open(img_path, 'rb')))- image.save()- create_product_thumbnails.delay(image.pk)- return image+ image_name = random.choice(os.listdir(placeholder_root))+ image = get_image(placeholder_dir, image_name)+ product_image = ProductImage(product=product, image=image)+ product_image.save()+ create_product_thumbnails.delay(product_image.pk)+ return product_imagedef create_attribute(**kwargs):@@ -451,7 +449,6 @@ def create_fulfillments(order):def create_fake_order():-user = random.choice([None, User.objects.filter(is_superuser=False).order_by('?').first()])if user:diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex 208acd3b23..9657c99157 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -330,10 +330,12 @@ class ProductImageForm(forms.ModelForm):if self.instance.image:self.fields['image'].widget = ImagePreviewWidget()- def save(self, commit):+ def save(self, commit=True):+ image = super().save(commit=commit)create_product_thumbnails.delay(image.pk)return image+class VariantImagesSelectForm(forms.Form):images = forms.ModelMultipleChoiceField(queryset=VariantImage.objects.none(),diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.pyindex 2cb3036a81..e457bdea40 100644--- a/tests/dashboard/test_product.py+++ b/tests/dashboard/test_product.py@@ -1,6 +1,6 @@import jsonfrom io import BytesIO-from unittest.mock import MagicMock, Mock+from unittest.mock import MagicMock, Mock, patchimport pytestfrom django.conf import settings@@ -468,7 +468,9 @@ def test_view_invalid_reorder_product_images(assert 'ordered_images' in resp_decoded['error']-def test_view_product_image_add(admin_client, product_with_image):+@patch('saleor.dashboard.product.forms.create_product_thumbnails.delay')+def test_view_product_image_add(+ mock_create_thumbnails, admin_client, product_with_image):assert len(ProductImage.objects.all()) == 1assert len(product_with_image.images.all()) == 1url = reverse(@@ -486,10 +488,12 @@ def test_view_product_image_add(admin_client, product_with_image):assert len(images) == 2assert image_name in images[1].image.nameassert images[1].alt == 'description'+ mock_create_thumbnails.assert_called_once_with(images[1].pk)+@patch('saleor.dashboard.product.forms.create_product_thumbnails.delay')def test_view_product_image_edit_same_image_add_description(- admin_client, product_with_image):+ mock_create_thumbnails, admin_client, product_with_image):assert len(product_with_image.images.all()) == 1product_image = product_with_image.images.all()[0]url = reverse(@@ -505,9 +509,12 @@ def test_view_product_image_edit_same_image_add_description(assert len(product_with_image.images.all()) == 1product_image.refresh_from_db()assert product_image.alt == 'description'+ mock_create_thumbnails.assert_called_once_with(product_image.pk)-def test_view_product_image_edit_new_image(admin_client, product_with_image):+@patch('saleor.dashboard.product.forms.create_product_thumbnails.delay')+def test_view_product_image_edit_new_image(+ mock_create_thumbnails, admin_client, product_with_image):assert len(product_with_image.images.all()) == 1product_image = product_with_image.images.all()[0]url = reverse(@@ -525,6 +532,7 @@ def test_view_product_image_edit_new_image(admin_client, product_with_image):product_image.refresh_from_db()assert image_name in product_image.image.nameassert product_image.alt == 'description'+ mock_create_thumbnails.assert_called_once_with(product_image.pk)def perform_bulk_action(product_list, action):diff --git a/tests/test_core.py b/tests/test_core.pyindex 3b4a9f6bfe..16cc8123ed 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -1,17 +1,23 @@-from unittest.mock import Mock+import io+from contextlib import redirect_stdout+from unittest.mock import Mock, patchimport pytestfrom django.shortcuts import reversefrom prices import Money-from saleor.account.models import Address, Userfrom saleor.core.utils import (+<<<<<<< HEADCountry, create_superuser, format_money, get_country_by_ip,get_currency_for_country, random_data)from saleor.core.utils.text import get_cleaner, strip_html+=======+ Country, create_superuser, create_thumbnails, format_money,+ get_country_by_ip, get_currency_for_country, random_data)+>>>>>>> Add testsfrom saleor.discount.models import Sale, Voucherfrom saleor.order.models import Order-from saleor.product.models import Product+from saleor.product.models import Product, ProductImagefrom saleor.shipping.models import ShippingMethodtype_schema = {@@ -119,19 +125,27 @@ def test_create_product_types_by_schema(db):assert product_type.is_shipping_required-def test_create_products_by_type(db, monkeypatch, product_image):+@patch('saleor.core.utils.random_data.create_product_thumbnails.delay')+def test_create_products_by_type(+ mock_create_thumbnails, db, monkeypatch, product_image):# Tests shouldn't depend on images present in placeholder foldermonkeypatch.setattr('saleor.core.utils.random_data.get_image',Mock(return_value=product_image))+ dummy_file_names = ['example.jpg', 'example2.jpg']+ monkeypatch.setattr(+ 'saleor.core.utils.random_data.os.listdir',+ Mock(return_value=dummy_file_names))+assert Product.objects.all().count() == 0how_many = 5product_type = random_data.create_product_types_by_schema(type_schema)[0][0]random_data.create_products_by_type(product_type, type_schema['Vegetable'], '/',- how_many=how_many, create_images=False)+ how_many=how_many, create_images=True)assert Product.objects.all().count() == how_many+ assert mock_create_thumbnails.calleddef test_create_fake_order(db, monkeypatch, product_image):@@ -184,3 +198,27 @@ def test_utils_strip_html():'\t<b>World</b>')text = strip_html(base_text, strip_whitespace=True)assert text == 'Hello World'+++def test_create_thumbnails(product_with_image, settings):+ settings.VERSATILEIMAGEFIELD_SETTINGS['create_images_on_demand'] = False+ sizeset = settings.VERSATILEIMAGEFIELD_RENDITION_KEY_SETS['products']+ product_image = product_with_image.images.first()++ # There's no way to list images created by versatile prewarmer+ # So we delete all created thumbnails/crops and count them+ log_deleted_images = io.StringIO()+ with redirect_stdout(log_deleted_images):+ product_image.image.delete_all_created_images()+ log_deleted_images = log_deleted_images.getvalue()+ # Image didn't have any thumbnails/crops created, so there's no log+ assert not log_deleted_images++ create_thumbnails(product_image.pk, ProductImage, 'products')++ log_deleted_images = io.StringIO()+ with redirect_stdout(log_deleted_images):+ product_image.image.delete_all_created_images()+ log_deleted_images = log_deleted_images.getvalue()+ # There should be oneline (one img) per one size in the sizeset+ assert log_deleted_images.count('\n') == len(sizeset)diff --git a/tests/test_product.py b/tests/test_product.pyindex ddabfc8d3d..bdfc33f713 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -1,6 +1,6 @@import datetimeimport json-from unittest.mock import Mock+from unittest.mock import Mock, patchimport pytestfrom django.urls import reverse@@ -11,12 +11,13 @@ from saleor.cart import CartStatus, utilsfrom saleor.cart.models import Cartfrom saleor.product import (ProductAvailabilityStatus, VariantAvailabilityStatus, models)-from saleor.product.models import Category+from saleor.product.models import Category, ProductImagefrom saleor.product.utils import (allocate_stock, deallocate_stock, decrease_stock,get_attributes_display_map, get_availability,get_product_availability_status, get_variant_availability_status,get_variant_picker_data, increase_stock)+from saleor.product.thumbnails import create_product_thumbnails@pytest.fixture()@@ -532,3 +533,12 @@ def test_include_products_from_subcategories_in_main_view('path': path, 'category_id': default_category.pk})response = authorized_client.get(url)assert product in response.context_data['products'][0]+++@patch('saleor.product.thumbnails.create_thumbnails')+def test_create_product_thumbnails(+ mock_create_thumbnails, product_with_image):+ product_image = product_with_image.images.first()+ create_product_thumbnails(product_image.pk)+ assert mock_create_thumbnails.called_once_with(+ product_image.pk, ProductImage, 'products')