Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix duplicated checkout tests (#17266)
Iga Karbowiakb1fa9cb8f20b
diff --git a/saleor/checkout/tests/fixtures/checkout_with_discount.py b/saleor/checkout/tests/fixtures/checkout_with_discount.pyindex 8467b9abb2..12f5ee1e24 100644--- a/saleor/checkout/tests/fixtures/checkout_with_discount.py+++ b/saleor/checkout/tests/fixtures/checkout_with_discount.py@@ -13,38 +13,6 @@ from ...models import CheckoutLinefrom ...utils import add_variant_to_checkout, add_voucher_to_checkout-def checkout_with_item_on_sale(checkout_with_item, promotion_converted_from_sale):- line = checkout_with_item.lines.first()- channel = checkout_with_item.channel- discount_amount = Decimal("5.0")- variant = line.variant- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)- predicate = {"variantPredicate": {"ids": [variant_id]}}- rule = promotion_converted_from_sale.rules.first()- rule.catalogue_predicate = predicate- rule.reward_value = discount_amount- rule.save(update_fields=["catalogue_predicate", "reward_value"])- rule.channels.add(channel)- channel_listing = variant.channel_listings.get(channel=channel)- channel_listing.discounted_price_amount = (- channel_listing.price_amount - discount_amount- )- channel_listing.save(update_fields=["discounted_price_amount"])-- CheckoutLineDiscount.objects.create(- line=line,- promotion_rule=rule,- type=DiscountType.SALE,- value_type=rule.reward_value_type,- value=discount_amount,- amount_value=discount_amount * line.quantity,- currency=channel.currency_code,- )-- return checkout_with_item--@pytest.fixturedef checkout_with_item_on_promotion(checkout_with_item):line = checkout_with_item.lines.first()diff --git a/saleor/checkout/tests/test_base_calculations.py b/saleor/checkout/tests/test_base_calculations.pyindex 57f21357d7..c9fc45da95 100644--- a/saleor/checkout/tests/test_base_calculations.py+++ b/saleor/checkout/tests/test_base_calculations.py@@ -52,45 +52,6 @@ def test_calculate_base_line_unit_price_with_custom_price(checkout_with_single_iassert unit_price == expected_price-def test_calculate_base_line_unit_price_with_variant_on_sale(- checkout_with_item_on_sale,-):- # given- checkout_lines_info, _ = fetch_checkout_lines(checkout_with_item_on_sale)-- checkout_line_info = checkout_lines_info[0]- assert not checkout_line_info.voucher- variant = checkout_line_info.variant- checkout_line_info.product = variant.product-- # when- unit_price = calculate_base_line_unit_price(checkout_line_info)-- # then- assert unit_price == checkout_line_info.channel_listing.discounted_price---def test_calculate_base_line_unit_price_with_variant_on_sale_custom_price(- checkout_with_item_on_sale,-):- # given- line = checkout_with_item_on_sale.lines.first()- price_override = Decimal("20.00")- line.price_override = price_override- line.save(update_fields=["price_override"])-- checkout_lines_info, _ = fetch_checkout_lines(checkout_with_item_on_sale)- checkout_line_info = checkout_lines_info[0]-- # when- unit_price = calculate_base_line_unit_price(checkout_line_info)-- # then- discount = line.discounts.first()- expected_price = price_override - discount.value- assert unit_price.amount == expected_price--def test_calculate_base_line_unit_price_with_variant_on_promotion(checkout_with_item_on_promotion, category):@@ -410,27 +371,6 @@ def test_calculate_base_line_total_price(checkout_with_single_item):assert total_price == expected_price * quantity-def test_calculate_base_line_total_price_with_variant_on_sale(- checkout_with_item_on_sale,-):- # given- quantity = 3- checkout = checkout_with_item_on_sale- checkout_line = checkout.lines.first()- checkout_line.quantity = quantity- checkout_line.save()-- checkout_lines_info, _ = fetch_checkout_lines(checkout)- checkout_line_info = checkout_lines_info[0]-- # when- total_price = calculate_base_line_total_price(checkout_line_info)-- # then- expected_unit_price = checkout_line_info.channel_listing.discounted_price- assert total_price == expected_unit_price * quantity--def test_calculate_base_line_total_price_with_variant_on_promotion(checkout_with_item_on_promotion,):@@ -609,74 +549,6 @@ def test_calculate_base_line_total_price_with_discounts_apply_once_per_order(assert total_price == (expected_unit_price * quantity) - expected_voucher_amount-def test_calculate_base_line_total_price_with_variant_on_sale_and_voucher(- checkout_with_item_on_sale, category, voucher, channel_USD-):- # given- quantity = 3- checkout = checkout_with_item_on_sale- checkout_line = checkout.lines.first()- checkout_line.quantity = quantity- checkout_line.save()-- checkout_line = checkout.lines.first()-- voucher.products.add(checkout_line.variant.product)- voucher.type = VoucherType.SPECIFIC_PRODUCT- voucher.save()-- voucher_amount = Money(Decimal(3), checkout.currency)- voucher_channel_listing = voucher.channel_listings.get(channel=channel_USD)- voucher_channel_listing.discount = voucher_amount- voucher_channel_listing.save()-- checkout.voucher_code = voucher.code-- checkout_lines_info, _ = fetch_checkout_lines(checkout)- checkout_line_info = checkout_lines_info[0]-- # when- total_price = calculate_base_line_total_price(checkout_line_info)-- # then- expected_unit_price = checkout_line_info.channel_listing.discounted_price- assert total_price == (expected_unit_price - voucher_amount) * quantity---def test_calculate_base_line_total_price_with_variant_on_sale_and_voucher_applied_once(- checkout_with_item_on_sale, category, voucher, channel_USD-):- # given- quantity = 3- checkout = checkout_with_item_on_sale- checkout_line = checkout.lines.first()- checkout_line.quantity = quantity- checkout_line.save()-- checkout.voucher_code = voucher.code- checkout.save(update_fields=["voucher_code"])-- voucher.products.add(checkout_line.variant.product)- voucher.type = VoucherType.SPECIFIC_PRODUCT- voucher.apply_once_per_order = True- voucher.save()-- voucher_amount = Money(Decimal(3), checkout.currency)- voucher_channel_listing = voucher.channel_listings.get(channel=channel_USD)- voucher_channel_listing.discount = voucher_amount- voucher_channel_listing.save()-- checkout_lines_info, _ = fetch_checkout_lines(checkout)- checkout_line_info = checkout_lines_info[0]-- # when- total_price = calculate_base_line_total_price(checkout_line_info)-- # then- expected_unit_price = checkout_line_info.channel_listing.discounted_price- assert total_price == (expected_unit_price * quantity) - voucher_amount--def test_calculate_base_line_total_price_with_variant_on_promotion_and_voucher(checkout_with_item_on_promotion, voucher, channel_USD):diff --git a/saleor/checkout/tests/test_checkout.py b/saleor/checkout/tests/test_checkout.pyindex 616cb8741e..fc8fabfc78 100644--- a/saleor/checkout/tests/test_checkout.py+++ b/saleor/checkout/tests/test_checkout.py@@ -1414,46 +1414,6 @@ def test_recalculate_checkout_discount_percentage()-def test_recalculate_checkout_discount_with_sale(- checkout_with_item_on_sale,- voucher_percentage,-):- # given- checkout = checkout_with_item_on_sale- checkout.voucher_code = voucher_percentage.code- voucher_discount = (- voucher_percentage.channel_listings.filter(channel=checkout.channel)- .first()- .discount_value- )-- manager = get_plugins_manager(allow_replica=False)- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)-- # when- recalculate_checkout_discount(manager, checkout_info, lines)-- # then- discounted_subtotal = (- lines[0].channel_listing.discounted_price_amount * lines[0].line.quantity- )- voucher_discount = voucher_discount / 100 * discounted_subtotal- assert checkout.discount_amount == voucher_discount-- checkout.price_expiration = timezone.now()- checkout.save()- assert (- calculations.checkout_total(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- ).gross.amount- == discounted_subtotal - voucher_discount- )--def test_recalculate_checkout_discount_with_promotion(checkout_with_voucher_percentage,voucher_percentage,diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.pyindex 4afb41bf56..48e45899e7 100644--- a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py@@ -303,106 +303,11 @@ def test_checkout_add_voucher_code_line_without_listing(assert errors[0]["field"] == "lines"-def test_checkout_add_voucher_code_checkout_with_sale(- api_client, checkout_with_item_on_sale, voucher_percentage, channel_USD+def test_checkout_add_products_voucher_code_checkout_with_promotion(+ api_client, checkout_with_item_on_promotion, voucher_percentage):# given- checkout = checkout_with_item_on_sale- manager = get_plugins_manager(allow_replica=False)- address = checkout.shipping_address-- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)- checkout_info.checkout.price_expiration = timezone.now()-- subtotal_discounted = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=address,- )-- voucher_discount_value = (- voucher_percentage.channel_listings.filter(channel=checkout.channel)- .first()- .discount_value- )-- variables = {- "id": to_global_id_or_none(checkout),- "promoCode": voucher_percentage.code,- }-- # when- data = _mutate_checkout_add_promo_code(api_client, variables)-- # then- discounted_subtotal = (- lines[0].channel_listing.discounted_price_amount * lines[0].line.quantity- )- voucher_discount = voucher_discount_value / 100 * discounted_subtotal-- previous_checkout_last_change = checkout.last_change-- checkout.refresh_from_db()- assert not data["errors"]- assert checkout.voucher_code == voucher_percentage.code- assert checkout.discount_amount == voucher_discount- assert checkout.last_change != previous_checkout_last_change- assert checkout.subtotal < subtotal_discounted---def test_checkout_add_specific_product_voucher_code_checkout_with_sale(- api_client, checkout_with_item_on_sale, voucher_specific_product_type-):- # given- voucher = voucher_specific_product_type- checkout = checkout_with_item_on_sale- expected_discount = Decimal(1.5)- manager = get_plugins_manager(allow_replica=False)-- checkout.price_expiration = timezone.now()-- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)-- subtotal_discounted = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )-- variables = {- "id": to_global_id_or_none(checkout),- "promoCode": voucher.code,- }-- # when- data = _mutate_checkout_add_promo_code(api_client, variables)-- # then- checkout.refresh_from_db()- lines, _ = fetch_checkout_lines(checkout)- subtotal_with_voucher = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )- assert not data["errors"]- assert subtotal_discounted == subtotal_with_voucher + Money(- expected_discount, "USD"- )- assert checkout.voucher_code == voucher.code- assert checkout.discount_amount == expected_discount---def test_checkout_add_products_voucher_code_checkout_with_sale(- api_client, checkout_with_item_on_sale, voucher_percentage, channel_USD-):- # given- checkout = checkout_with_item_on_sale+ checkout = checkout_with_item_on_promotionproduct = checkout.lines.first().variant.productvoucher = voucher_percentagevoucher.type = VoucherType.SPECIFIC_PRODUCT@@ -459,72 +364,11 @@ def test_checkout_add_products_voucher_code_checkout_with_sale(assert checkout.subtotal < subtotal_discounted-def test_checkout_add_collection_voucher_code_checkout_with_sale(- api_client, checkout_with_item_on_sale, voucher_percentage, collection+def test_checkout_add_collection_voucher_code_checkout_with_promotion_collection_deleted(+ api_client, checkout_with_item_on_promotion, voucher_percentage, collection):# given- checkout = checkout_with_item_on_sale-- voucher = voucher_percentage- product = checkout.lines.first().variant.product- product.collections.add(collection)- voucher.type = VoucherType.SPECIFIC_PRODUCT- voucher.save()- voucher.collections.add(collection)-- voucher_discount_value = (- voucher_percentage.channel_listings.filter(channel=checkout.channel)- .first()- .discount_value- )-- manager = get_plugins_manager(allow_replica=False)-- checkout.price_expiration = timezone.now()- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)- subtotal_discounted = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )- variables = {- "id": to_global_id_or_none(checkout),- "promoCode": voucher.code,- }-- # when- data = _mutate_checkout_add_promo_code(api_client, variables)-- # then- discounted_subtotal = (- lines[0].channel_listing.discounted_price_amount * lines[0].line.quantity- )- voucher_discount = voucher_discount_value / 100 * discounted_subtotal-- checkout.refresh_from_db()- lines, _ = fetch_checkout_lines(checkout)- subtotal_with_voucher = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )-- assert not data["errors"]- assert subtotal_discounted == subtotal_with_voucher + Money(- voucher_discount, checkout.currency- )- assert checkout.voucher_code == voucher.code- assert checkout.discount_amount == voucher_discount---def test_checkout_add_collection_voucher_code_checkout_with_sale_collection_deleted(- api_client, checkout_with_item_on_sale, voucher_percentage, collection-):- # given- checkout = checkout_with_item_on_sale+ checkout = checkout_with_item_on_promotionvoucher = voucher_percentageproduct = checkout.lines.first().variant.product@@ -556,66 +400,6 @@ def test_checkout_add_collection_voucher_code_checkout_with_sale_collection_deleassert Collection.objects.count() == 0-def test_checkout_add_category_code_checkout_with_sale(- api_client, checkout_with_item_on_sale, voucher_percentage, channel_USD-):- # given- checkout = checkout_with_item_on_sale- product = checkout.lines.first().variant.product- category = product.category- voucher = voucher_percentage- voucher.type = VoucherType.SPECIFIC_PRODUCT- voucher.save()- voucher.categories.add(category)-- voucher_discount_value = (- voucher_percentage.channel_listings.filter(channel=checkout.channel)- .first()- .discount_value- )-- checkout.price_expiration = timezone.now()- manager = get_plugins_manager(allow_replica=False)- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)- subtotal_discounted = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )-- variables = {- "id": to_global_id_or_none(checkout),- "promoCode": voucher.code,- }-- # when- data = _mutate_checkout_add_promo_code(api_client, variables)-- # then- discounted_subtotal = (- lines[0].channel_listing.discounted_price_amount * lines[0].line.quantity- )- voucher_discount = voucher_discount_value / 100 * discounted_subtotal-- checkout.refresh_from_db()- lines, _ = fetch_checkout_lines(checkout)- subtotal_with_voucher = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )- assert not data["errors"]- assert subtotal_discounted == subtotal_with_voucher + Money(- voucher_discount, checkout.currency- )- assert checkout.voucher_code == voucher.code- assert checkout.discount_amount == voucher_discount- assert checkout.subtotal < subtotal_discounted--def test_checkout_add_voucher_code_checkout_on_promotion(api_client, checkout_with_item_on_promotion, voucher_percentage, channel_USD):diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.pyindex d701a57105..fb3d320ae2 100644--- a/saleor/graphql/checkout/tests/test_checkout.py+++ b/saleor/graphql/checkout/tests/test_checkout.py@@ -22,8 +22,7 @@ from ....checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ....checkout.utils import add_variant_to_checkout, add_voucher_to_checkoutfrom ....core.db.connection import allow_writerfrom ....core.prices import quantize_price-from ....discount import DiscountType, DiscountValueType, VoucherType-from ....discount.models import CheckoutLineDiscount+from ....discount import DiscountValueType, VoucherTypefrom ....payment import TransactionActionfrom ....payment.interface import (ListStoredPaymentMethodsRequestData,@@ -2107,208 +2106,6 @@ def test_checkout_prices_checkout_with_custom_prices_when_line_without_listing()-def test_checkout_prices_with_sales(user_api_client, checkout_with_item_on_sale):- # given- query = QUERY_CHECKOUT_PRICES- checkout = checkout_with_item_on_sale- variables = {"id": to_global_id_or_none(checkout)}-- manager = get_plugins_manager(allow_replica=False)- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)-- # when- response = user_api_client.post_graphql(query, variables)- content = get_graphql_content(response)- data = content["data"]["checkout"]-- # then- assert data["token"] == str(checkout.token)- assert len(data["lines"]) == checkout.lines.count()-- checkout.refresh_from_db()- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)-- total = calculations.checkout_total(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )- assert data["totalPrice"]["gross"]["amount"] == (total.gross.amount)- subtotal = calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=checkout.shipping_address,- )- assert data["subtotalPrice"]["gross"]["amount"] == (subtotal.gross.amount)- line_info = lines[0]- assert line_info.line.quantity > 0- line_total_price = calculations.checkout_line_total(- manager=manager,- checkout_info=checkout_info,- lines=lines,- checkout_line_info=line_info,- )- assert data["lines"][0]["unitPrice"]["gross"]["amount"] == float(- round(line_total_price.gross.amount / line_info.line.quantity, 2)- )- assert (- data["lines"][0]["totalPrice"]["gross"]["amount"]- == line_total_price.gross.amount- )- undiscounted_unit_price = line_info.variant.get_base_price(- line_info.channel_listing,- line_info.line.price_override,- )- undiscounted_total_price = undiscounted_unit_price.amount * line_info.line.quantity- assert (- data["lines"][0]["undiscountedUnitPrice"]["amount"]- == undiscounted_unit_price.amount- )- assert (- data["lines"][0]["undiscountedTotalPrice"]["amount"] == undiscounted_total_price- )- assert line_total_price.gross.amount < undiscounted_total_price--- ("channel_listing_model", "listing_filter_field"),- [- (ProductVariantChannelListing, "variant_id"),- (ProductChannelListing, "product__variants__id"),- ],-)-def test_checkout_prices_with_sales_when_line_without_listing(- channel_listing_model,- listing_filter_field,- user_api_client,- checkout_with_item,- promotion_converted_from_sale,-):- # given- checkout = checkout_with_item-- line_without_listing = checkout_with_item.lines.first()-- channel = checkout_with_item.channel- discount_amount = Decimal("5.0")- variant = line_without_listing.variant- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)- predicate = {"variantPredicate": {"ids": [variant_id]}}-- rule = promotion_converted_from_sale.rules.first()- rule.catalogue_predicate = predicate- rule.reward_value = discount_amount- rule.save(update_fields=["catalogue_predicate", "reward_value"])- rule.channels.add(channel)-- channel_listing = variant.channel_listings.get(channel=channel)- channel_listing.discounted_price_amount = (- channel_listing.price_amount - discount_amount- )- channel_listing.save(update_fields=["discounted_price_amount"])-- channel_listing.variantlistingpromotionrule.create(- promotion_rule=rule,- discount_amount=discount_amount,- currency=channel.currency_code,- )-- CheckoutLineDiscount.objects.create(- line=line_without_listing,- promotion_rule=rule,- type=DiscountType.PROMOTION,- value_type=rule.reward_value_type,- value=discount_amount,- amount_value=discount_amount * line_without_listing.quantity,- currency=channel.currency_code,- )-- checkout.price_expiration = timezone.now()- checkout.save()-- checkout = checkout_with_item-- channel_listing_model.objects.filter(- channel_id=checkout.channel_id, **{listing_filter_field: variant.id}- ).delete()-- query = QUERY_CHECKOUT_PRICES- variables = {"id": to_global_id_or_none(checkout_with_item)}-- # when- response = user_api_client.post_graphql(query, variables)- content = get_graphql_content(response)- data = content["data"]["checkout"]-- # then- manager = get_plugins_manager(allow_replica=False)- lines, _ = fetch_checkout_lines(checkout)- checkout_info = fetch_checkout_info(checkout, lines, manager)-- line_info = [line for line in lines if line.line.pk == line_without_listing.pk][0]- line_total_price = calculations.checkout_line_total(- manager=manager,- checkout_info=checkout_info,- lines=lines,- checkout_line_info=line_info,- )- line_unit_price = calculations.checkout_line_unit_price(- manager=manager,- checkout_info=checkout_info,- lines=lines,- checkout_line_info=line_info,- )-- assert len(data["lines"]) == checkout.lines.count()- response_api_line_without_listing = [- line_data- for line_data in data["lines"]- if line_data["id"] == to_global_id_or_none(line_without_listing)- ][0]-- assert response_api_line_without_listing["variant"]["pricing"] is None- assert (- response_api_line_without_listing["unitPrice"]["gross"]["amount"]- == line_unit_price.gross.amount- )- assert (- response_api_line_without_listing["undiscountedUnitPrice"]["amount"]- == line_without_listing.undiscounted_unit_price_amount- )- assert (- response_api_line_without_listing["totalPrice"]["gross"]["amount"]- == line_total_price.gross.amount- )- assert (- response_api_line_without_listing["undiscountedTotalPrice"]["amount"]- == line_without_listing.undiscounted_unit_price_amount- * line_without_listing.quantity- )- assert line_unit_price.gross < line_without_listing.undiscounted_unit_price- assert (- line_total_price.gross- < line_without_listing.undiscounted_unit_price * line_without_listing.quantity- )-- checkout_problems = data["problems"]- assert len(checkout_problems) == 1- assert (- checkout_problems[0]["__typename"] == "CheckoutLineProblemVariantNotAvailable"- )- assert checkout_problems[0]["line"]["id"] == to_global_id_or_none(- line_without_listing- )- assert len(response_api_line_without_listing["problems"]) == 1- assert (- response_api_line_without_listing["problems"][0]["__typename"]- == "CheckoutLineProblemVariantNotAvailable"- )--def test_checkout_prices_with_promotion(user_api_client, checkout_with_item_on_promotion):