Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add more tests
IKarbowiak9010acc6e8f2
diff --git a/.isort.cfg b/.isort.cfgindex 5e42e17d1f..8c4afd3e1f 100644--- a/.isort.cfg+++ b/.isort.cfg@@ -1,2 +1,2 @@[settings]-known_third_party = PIL,babel,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_jwt,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,opentracing,petl,phonenumber_field,phonenumbers,prices,promise,pytest,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefield+known_third_party = PIL,babel,braintree,celery,dj_database_url,dj_email_url,django,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,geolite2,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_jwt,graphql_relay,html_to_draftjs,i18naddress,jaeger_client,jwt,markdown,measurement,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,prices,promise,pytest,pytz,razorpay,requests,sentry_sdk,storages,stripe,templated_email,text_unidecode,tqdm,versatileimagefielddiff --git a/saleor/csv/utils/products_data.py b/saleor/csv/utils/products_data.pyindex 8ead8b2d41..b40bfb96c8 100644--- a/saleor/csv/utils/products_data.py+++ b/saleor/csv/utils/products_data.py@@ -3,7 +3,7 @@ from collections import ChainMap, defaultdictfrom typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple, Unionfrom django.conf import settings-from django.db.models import CharField, F, Value as V+from django.db.models import Case, CharField, F, Value as V, Whenfrom django.db.models.functions import Concatfrom ...core.utils import build_absolute_uri@@ -129,7 +129,11 @@ def prepare_products_data(products_data = queryset.annotate(product_currency=F("currency"),- product_weight=Concat("weight", V(" g"), output_field=CharField()),+ product_weight=Case(+ When(weight__isnull=False, then=Concat("weight", V(" g"))),+ default=V(""),+ output_field=CharField(),+ ),).values(*product_export_fields)product_relations_data, products_attributes_fields = get_products_relations_data(@@ -300,8 +304,12 @@ def prepare_variants_data(ProductVariant.objects.filter(product__pk=pk).annotate(variant_currency=F("currency"),- variant_weight=Concat("weight", V(" g"), output_field=CharField()),variant_image_path=F("images__image"),+ variant_weight=Case(+ When(weight__isnull=False, then=Concat("weight", V(" g"))),+ default=V(""),+ output_field=CharField(),+ ),).order_by("sku").values(*variant_fields)diff --git a/tests/test_csv_export.py b/tests/test_csv_export.pyindex 8b9950f112..d0f01380fc 100644--- a/tests/test_csv_export.py+++ b/tests/test_csv_export.py@@ -2,6 +2,7 @@ import datetimeimport shutilfrom unittest.mock import ANY, MagicMock, Mock, patch+import openpyxlimport pytestimport pytzfrom django.core.files import File@@ -202,6 +203,63 @@ def test_create_csv_file_and_save_in_export_file_csv(export_file, tmpdir):shutil.rmtree(tmpdir)+def test_create_csv_file_and_save_in_export_file_xlsx(export_file, tmpdir):+ from django.conf import settings++ settings.MEDIA_ROOT = tmpdir++ export_data = [+ {"id": "123", "name": "test1", "collections": "coll1"},+ {"id": "345", "name": "test2"},+ ]+ headers = ["id", "name", "collections"]+ csv_headers_mapping = {"id": "ID", "name": "NAME", "collections": "COLLECTIONS"}+ delimiter = ";"+ export_file = export_file+ file_name = "test.xlsx"++ export_file_csv_upload_dir = ExportFile.content_file.field.upload_to++ assert not export_file.content_file++ create_csv_file_and_save_in_export_file(+ export_data,+ headers,+ csv_headers_mapping,+ delimiter,+ export_file,+ file_name,+ FileTypes.XLSX,+ )++ xlsx_file = export_file.content_file+ assert xlsx_file+ assert xlsx_file.name == f"{export_file_csv_upload_dir}/{file_name}"++ wb_obj = openpyxl.load_workbook(xlsx_file)++ sheet_obj = wb_obj.active+ max_col = sheet_obj.max_column+ max_row = sheet_obj.max_row+ expected_headers = list(csv_headers_mapping.values())+ headers = [sheet_obj.cell(row=1, column=i).value for i in range(1, max_col + 1)]+ data = []+ for i in range(2, max_row + 1):+ row = []+ for j in range(1, max_col + 1):+ row.append(sheet_obj.cell(row=i, column=j).value)+ data.append(row)++ assert headers == expected_headers+ assert list(export_data[0].values()) in data+ row2 = list(export_data[1].values())+ # add string with space for collections column+ row2.append(" ")+ assert row2 in data++ shutil.rmtree(tmpdir)++def test_save_csv_file_in_export_file(export_file, tmpdir):from django.conf import settingsdiff --git a/tests/test_csv_products_data.py b/tests/test_csv_products_data.pyindex b26ac4ad61..4d6153136b 100644--- a/tests/test_csv_products_data.py+++ b/tests/test_csv_products_data.py@@ -1,5 +1,7 @@from unittest.mock import patch+from measurement.measures import Weight+from saleor.csv.utils.products_data import (ProductExportFields,add_attribute_info_to_data,@@ -104,6 +106,9 @@ def test_get_products_data_with_some_of_product_and_variant_fields(def test_prepare_products_data(product, product_with_image, collection, image):+ product.weight = Weight(kg=5)+ product.save()+collection.products.add(product)variant = product.variants.first()@@ -139,7 +144,9 @@ def test_prepare_products_data(product, product_with_image, collection, image):product_data["product_currency"] = product.currencyproduct_data["product_type__name"] = product.product_type.nameproduct_data["id"] = product.pk- product_data["product_weight"] = product.weight+ product_data["product_weight"] = (+ "{} g".format(int(product.weight.value * 1000)) if product.weight else ""+ )product_data["charge_taxes"] = product.charge_taxesproduct_data["product_image_path"] = (""@@ -165,7 +172,11 @@ def test_prepare_products_data(product, product_with_image, collection, image):data["variant_currency"] = variant.currencydata["price_override_amount"] = variant.price_override_amountdata["cost_price_amount"] = variant.cost_price_amount- data["variant_weight"] = variant.weight+ data["variant_weight"] = (+ "{} g".foramt(int(variant.weight.value * 1000))+ if variant.weight+ else ""+ )for stock in variant.stocks.all():slug = stock.warehouse.slug@@ -362,6 +373,8 @@ def test_prepare_products_relations_data(product_with_image, collection_list):def test_prepare_variants_data(product):variant = product.variants.first()+ variant.weight = Weight(kg=5)+ variant.save()warehouse_headers = set()attribute_headers = set()@@ -384,7 +397,9 @@ def test_prepare_variants_data(product):"cost_price_amount": variant.cost_price_amount,"price_override_amount": variant.price_override_amount,"variant_currency": variant.currency,- "variant_weight": variant.weight,+ "variant_weight": "{} g".format(int(variant.weight.value * 1000))+ if variant.weight+ else "",}assigned_attribute = variant.attributes.first()if assigned_attribute: