Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix tests in api/test_checkout and test_discounts
Maciej Korycinski78685cf202be
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex 1a0022a1df..ce0c040802 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -4,6 +4,7 @@ from unittest.mock import ANY, patchimport grapheneimport pytestfrom django.core.exceptions import ValidationError+from prices import TaxedMoneyfrom saleor.checkout.models import Checkoutfrom saleor.checkout.utils import add_voucher_to_checkout, clean_checkout, is_fully_paid@@ -863,6 +864,7 @@ def test_checkout_complete(checkout_line_variant = checkout_line.varianttotal = checkout.get_total()+ total = TaxedMoney(total, total)payment = payment_dummypayment.is_active = Truepayment.order = None@@ -947,6 +949,7 @@ def test_checkout_complete_insufficient_stock(checkout.billing_address = addresscheckout.save()total = checkout.get_total()+ total = TaxedMoney(total, total)payment = payment_dummypayment.is_active = Truepayment.order = None@@ -1036,11 +1039,13 @@ def test_checkout_prices(user_api_client, checkout_with_item):data = content["data"]["checkout"]assert data["token"] == str(checkout_with_item.token)assert len(data["lines"]) == checkout_with_item.lines.count()+ total = checkout_with_item.get_total()assert data["totalPrice"]["gross"]["amount"] == (- checkout_with_item.get_total().gross.amount+ TaxedMoney(total, total).gross.amount)+ subtotal = checkout_with_item.get_subtotal()assert data["subtotalPrice"]["gross"]["amount"] == (- checkout_with_item.get_subtotal().gross.amount+ TaxedMoney(subtotal, subtotal).gross.amount)@@ -1076,11 +1081,7 @@ def test_checkout_shipping_method_update(checkout.refresh_from_db()assert checkout.shipping_method == shipping_methodmock_clean_shipping.assert_called_once_with(- checkout=checkout,- method=shipping_method,- discounts=ANY,- taxes=ANY,- remove=False,+ checkout=checkout, method=shipping_method, discounts=ANY, remove=False)@@ -1159,6 +1160,7 @@ def test_clean_checkout(checkout_with_item, payment_dummy, address, shipping_metcheckout.billing_address = addresscheckout.save()total = checkout.get_total()+ total = TaxedMoney(total, total)payment = payment_dummypayment.is_active = Truepayment.order = None@@ -1167,7 +1169,7 @@ def test_clean_checkout(checkout_with_item, payment_dummy, address, shipping_metpayment.checkout = checkoutpayment.save()# Shouldn't raise any errors- clean_checkout(checkout, None, None)+ clean_checkout(checkout, None)def test_clean_checkout_no_shipping_method(checkout_with_item, address):@@ -1176,7 +1178,7 @@ def test_clean_checkout_no_shipping_method(checkout_with_item, address):checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, None, None)+ clean_checkout(checkout, None)msg = "Shipping method is not set"assert e.value.error_list[0].message == msg@@ -1188,7 +1190,7 @@ def test_clean_checkout_no_shipping_address(checkout_with_item, shipping_method)checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, None, None)+ clean_checkout(checkout, None)msg = "Shipping address is not set"assert e.value.error_list[0].message == msg@@ -1203,7 +1205,7 @@ def test_clean_checkout_invalid_shipping_method(checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, None, None)+ clean_checkout(checkout, None)msg = "Shipping method is not valid for your shipping address"assert e.value.error_list[0].message == msg@@ -1218,7 +1220,7 @@ def test_clean_checkout_no_billing_address(checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, None, None)+ clean_checkout(checkout, None)msg = "Billing address is not set"assert e.value.error_list[0].message == msg@@ -1231,7 +1233,7 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)checkout.save()with pytest.raises(ValidationError) as e:- clean_checkout(checkout, None, None)+ clean_checkout(checkout, None)msg = "Provided payment methods can not cover the checkout's total amount"assert e.value.error_list[0].message == msg@@ -1240,6 +1242,7 @@ def test_clean_checkout_no_payment(checkout_with_item, shipping_method, address)def test_is_fully_paid(checkout_with_item, payment_dummy):checkout = checkout_with_itemtotal = checkout.get_total()+ total = TaxedMoney(total, total)payment = payment_dummypayment.is_active = Truepayment.order = None@@ -1247,13 +1250,14 @@ def test_is_fully_paid(checkout_with_item, payment_dummy):payment.currency = total.gross.currencypayment.checkout = checkoutpayment.save()- is_paid = is_fully_paid(checkout, None, None)+ is_paid = is_fully_paid(checkout, None)assert is_paiddef test_is_fully_paid_many_payments(checkout_with_item, payment_dummy):checkout = checkout_with_itemtotal = checkout.get_total()+ total = TaxedMoney(total, total)payment = payment_dummypayment.is_active = Truepayment.order = None@@ -1269,13 +1273,14 @@ def test_is_fully_paid_many_payments(checkout_with_item, payment_dummy):payment2.currency = total.gross.currencypayment2.checkout = checkoutpayment2.save()- is_paid = is_fully_paid(checkout, None, None)+ is_paid = is_fully_paid(checkout, None)assert is_paiddef test_is_fully_paid_partially_paid(checkout_with_item, payment_dummy):checkout = checkout_with_itemtotal = checkout.get_total()+ total = TaxedMoney(total, total)payment = payment_dummypayment.is_active = Truepayment.order = None@@ -1283,13 +1288,13 @@ def test_is_fully_paid_partially_paid(checkout_with_item, payment_dummy):payment.currency = total.gross.currencypayment.checkout = checkoutpayment.save()- is_paid = is_fully_paid(checkout, None, None)+ is_paid = is_fully_paid(checkout, None)assert not is_paiddef test_is_fully_paid_no_payment(checkout_with_item):checkout = checkout_with_item- is_paid = is_fully_paid(checkout, None, None)+ is_paid = is_fully_paid(checkout, None)assert not is_paid@@ -1371,7 +1376,8 @@ def test_checkout_add_voucher_not_applicable_voucher(def test_checkout_lines_delete_with_not_applicable_voucher(user_api_client, checkout_with_item, voucher):- voucher.min_amount_spent = checkout_with_item.get_subtotal().gross+ subtotal = checkout_with_item.get_subtotal()+ voucher.min_amount_spent = TaxedMoney(subtotal, subtotal).grossvoucher.save(update_fields=["min_amount_spent"])add_voucher_to_checkout(voucher, checkout_with_item)diff --git a/tests/test_discounts.py b/tests/test_discounts.pyindex 9779bf84d0..9103c860e5 100644--- a/tests/test_discounts.py+++ b/tests/test_discounts.py@@ -1,7 +1,7 @@from datetime import date, timedeltaimport pytest-from prices import Money, TaxedMoney+from prices import Moneyfrom saleor.checkout.utils import get_voucher_discount_for_checkoutfrom saleor.discount import DiscountValueType, VoucherType@@ -27,7 +27,7 @@ def get_min_amount_spent(min_amount_spent):"min_amount_spent, value",[(Money(5, "USD"), Money(10, "USD")), (Money(10, "USD"), Money(10, "USD"))],)-def test_valid_voucher_min_amount_spent(settings, min_amount_spent, value):+def test_valid_voucher_min_amount_spent(min_amount_spent, value):voucher = Voucher(code="unique",type=VoucherType.SHIPPING,@@ -35,7 +35,7 @@ def test_valid_voucher_min_amount_spent(settings, min_amount_spent, value):discount_value=Money(10, "USD"),min_amount_spent=min_amount_spent,)- voucher.validate_min_amount_spent(TaxedMoney(net=value, gross=value))+ voucher.validate_min_amount_spent(value)@pytest.mark.integration@@ -49,7 +49,7 @@ def test_variant_discounts(product):high_discount = Sale.objects.create(type=DiscountValueType.FIXED, value=50)high_discount.products.add(product)final_price = variant.get_price(discounts=Sale.objects.all())- assert final_price.gross == Money(0, "USD")+ assert final_price == Money(0, "USD")@pytest.mark.integration@@ -59,7 +59,7 @@ def test_percentage_discounts(product):discount = Sale.objects.create(type=DiscountValueType.PERCENTAGE, value=50)discount.products.add(product)final_price = variant.get_price(discounts=[discount])- assert final_price.gross == Money(5, "USD")+ assert final_price == Money(5, "USD")def test_voucher_queryset_active(voucher):@@ -82,7 +82,6 @@ def test_voucher_queryset_active(voucher):],)def test_products_voucher_checkout_discount_not(- settings,monkeypatch,prices,discount_value,@@ -93,10 +92,7 @@ def test_products_voucher_checkout_discount_not():monkeypatch.setattr("saleor.checkout.utils.get_prices_of_discounted_products",- lambda lines, discounted_products: (- TaxedMoney(net=Money(price, "USD"), gross=Money(price, "USD"))- for price in prices- ),+ lambda lines, discounted_products: (Money(price, "USD") for price in prices),)voucher = Voucher(code="unique",@@ -186,7 +182,7 @@ def test_get_value_voucher_discount(min_amount_spent=get_min_amount_spent(min_amount_spent),)voucher.save()- total_price = TaxedMoney(net=Money(total, "USD"), gross=Money(total, "USD"))+ total_price = Money(total, "USD")discount = get_value_voucher_discount(voucher, total_price)assert discount == Money(expected_value, "USD")@@ -217,10 +213,8 @@ def test_get_shipping_voucher_discount(min_amount_spent=get_min_amount_spent(min_amount_spent),)voucher.save()- total = TaxedMoney(net=Money(total, "USD"), gross=Money(total, "USD"))- shipping_price = TaxedMoney(- net=Money(shipping_price, "USD"), gross=Money(shipping_price, "USD")- )+ total = Money(total, "USD")+ shipping_price = Money(shipping_price, "USD")discount = get_shipping_voucher_discount(voucher, total, shipping_price)assert discount == Money(expected_value, "USD")@@ -236,10 +230,7 @@ def test_get_shipping_voucher_discount(def test_get_voucher_discount_all_products(prices, discount_value_type, discount_value, voucher_type, expected_value):- prices = [- TaxedMoney(net=Money(price, "USD"), gross=Money(price, "USD"))- for price in prices- ]+ prices = [Money(price, "USD") for price in prices]voucher = Voucher(code="unique",type=voucher_type,