Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for apply_tax_to_price
Rafał Pitońc06825de65bd
diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex 6a01c97502..a717f75b8d 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -17,7 +17,7 @@ from django_prices_openexchangerates import exchange_currencyfrom django_prices_vatlayer.utils import (get_tax_rates_for_country, get_tax_for_rate)from geolite2 import geolite2-from prices import Money, MoneyRange, TaxedMoney+from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRangefrom versatileimagefield.image_warmer import VersatileImageFieldWarmerfrom ...account.models import User@@ -114,17 +114,26 @@ def get_taxes_for_country(country):return taxes-def apply_tax_to_price(taxes, rate_name, price):+def apply_tax_to_price(taxes, rate_name, base):if not taxes:# Naively convert Money to TaxedMoney for consistency with price- # handling logic across the codebase- return TaxedMoney(net=price, gross=price)+ # handling logic across the codebase, passthrough other money types+ if isinstance(base, Money):+ return TaxedMoney(net=base, gross=base)+ if isinstance(base, MoneyRange):+ return TaxedMoneyRange(+ apply_tax_to_price(taxes, rate_name, base.start),+ apply_tax_to_price(taxes, rate_name, base.stop))+ if isinstance(base, (TaxedMoney, TaxedMoneyRange)):+ return base+ raise TypeError('Unknown base for flat_tax: %r' % (base,))+if rate_name in taxes:tax_to_apply = taxes[rate_name]else:tax_to_apply = taxes['standard']- price_with_tax = tax_to_apply(price, not settings.BASE_PRICES_ARE_NET)- return price_with_tax++ return tax_to_apply(base, not settings.INCLUDE_TAXES_IN_PRICES)def to_local_currency(price, currency):diff --git a/saleor/settings.py b/saleor/settings.pyindex 091f1747ba..a79825e9bb 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -284,7 +284,7 @@ OPENEXCHANGERATES_API_KEY = os.environ.get('OPENEXCHANGERATES_API_KEY')# dashboard are treated as net amounts. If you wish to reverse that and use# them as gross amounts instead change the BASE_PRICES_ARE_NET to FalseVATLAYER_ACCESS_KEY = os.environ.get('VATLAYER_ACCESS_KEY')-BASE_PRICES_ARE_NET = True+INCLUDE_TAXES_IN_PRICES = TrueACCOUNT_ACTIVATION_DAYS = 3diff --git a/tests/conftest.py b/tests/conftest.pyindex c4d77d8eac..5e7493ff12 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -9,6 +9,7 @@ from django.core.files import Filefrom django.core.files.uploadedfile import SimpleUploadedFilefrom django.forms import ModelFormfrom django.utils.encoding import smart_text+from django_prices_vatlayer.utils import get_tax_for_ratefrom payments import FraudStatus, PaymentStatusfrom PIL import Imagefrom prices import Money@@ -684,3 +685,32 @@ def menu_with_items(menu, default_category, collection):menu.items.create(name=collection.name, collection=collection, parent=menu_item)return menu+++def tax_rates():+ return {+ 'standard_rate': 23,+ 'reduced_rates': {+ 'pharmaceuticals': 8,+ 'medical': 8,+ 'passenger transport': 8,+ 'newspapers': 8,+ 'hotels': 8,+ 'restaurants': 8,+ 'admission to cultural events': 8,+ 'admission to sporting events': 8,+ 'admission to entertainment events': 8,+ 'foodstuffs': 5+ }+ }+++def taxes(tax_rates):+ taxes = {'standard': get_tax_for_rate(tax_rates)}+ if tax_rates['reduced_rates']:+ taxes.update({+ rate: get_tax_for_rate(tax_rates, rate)+ for rate in tax_rates['reduced_rates']})+ return taxesdiff --git a/tests/test_core.py b/tests/test_core.pyindex a0aa3a74c1..ed5864a115 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -3,15 +3,14 @@ from contextlib import redirect_stdoutfrom unittest.mock import Mock, patchimport pytest-from django.conf import settingsfrom django.shortcuts import reverse-from prices import Money+from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRangefrom saleor.account.models import Address, Userfrom saleor.core.storages import S3MediaStoragefrom saleor.core.utils import (- Country, create_superuser, create_thumbnails, format_money,- get_country_by_ip, get_currency_for_country, random_data)+ Country, apply_tax_to_price, create_superuser, create_thumbnails,+ format_money, get_country_by_ip, get_currency_for_country, random_data)from saleor.core.utils.text import get_cleaner, strip_htmlfrom saleor.discount.models import Sale, Voucherfrom saleor.order.models import Order@@ -32,6 +31,78 @@ type_schema = {'is_shipping_required': True}}+def test_apply_tax_to_price_include_tax(taxes):+ money = Money(100, 'USD')+ assert apply_tax_to_price(taxes, 'standard', money) == TaxedMoney(+ net=Money(100, 'USD'), gross=Money(123, 'USD'))+ assert apply_tax_to_price(taxes, 'medical', money) == TaxedMoney(+ net=Money(100, 'USD'), gross=Money(108, 'USD'))++ taxed_money = TaxedMoney(net=Money(100, 'USD'), gross=Money(100, 'USD'))+ assert apply_tax_to_price(taxes, 'standard', taxed_money) == TaxedMoney(+ net=Money(100, 'USD'), gross=Money(123, 'USD'))+ assert apply_tax_to_price(taxes, 'medical', taxed_money) == TaxedMoney(+ net=Money(100, 'USD'), gross=Money(108, 'USD'))+++def test_apply_tax_to_price_include_tax_fallback_to_standard_rate(taxes):+ money = Money(100, 'USD')+ taxed_money = TaxedMoney(net=Money(100, 'USD'), gross=Money(123, 'USD'))+ assert apply_tax_to_price(taxes, 'space suits', money) == taxed_money+++def test_apply_tax_to_price_add_tax(settings, taxes):+ settings.INCLUDE_TAXES_IN_PRICES = False++ money = Money(100, 'USD')+ assert apply_tax_to_price(taxes, 'standard', money) == TaxedMoney(+ net=Money('81.30', 'USD'), gross=Money(100, 'USD'))+ assert apply_tax_to_price(taxes, 'medical', money) == TaxedMoney(+ net=Money('92.59', 'USD'), gross=Money(100, 'USD'))+++def test_apply_tax_to_price_add_tax_fallback_to_standard_rate(+ settings, taxes):+ settings.INCLUDE_TAXES_IN_PRICES = False+ money = Money(100, 'USD')+ assert apply_tax_to_price(taxes, 'space suits', money) == TaxedMoney(+ net=Money('81.30', 'USD'), gross=Money(100, 'USD'))++ taxed_money = TaxedMoney(net=Money(100, 'USD'), gross=Money(100, 'USD'))+ assert apply_tax_to_price(taxes, 'space suits', taxed_money) == TaxedMoney(+ net=Money('81.30', 'USD'), gross=Money(100, 'USD'))+++def test_apply_tax_to_price_raise_typeerror_for_invalid_type(taxes):+ with pytest.raises(TypeError):+ assert apply_tax_to_price(taxes, 'standard', 100)+++def test_apply_tax_to_price_no_taxes_return_taxed_money():+ money = Money(100, 'USD')+ taxed_money = TaxedMoney(net=Money(100, 'USD'), gross=Money(100, 'USD'))++ assert apply_tax_to_price(None, 'standard', money) == taxed_money+ assert apply_tax_to_price(None, 'medical', taxed_money) == taxed_money+++def test_apply_tax_to_price_no_taxes_return_taxed_money_range():+ money_range = MoneyRange(Money(100, 'USD'), Money(200, 'USD'))+ taxed_money_range = TaxedMoneyRange(+ TaxedMoney(net=Money(100, 'USD'), gross=Money(100, 'USD')),+ TaxedMoney(net=Money(200, 'USD'), gross=Money(200, 'USD')))++ assert (apply_tax_to_price(+ None, 'standard', money_range) == taxed_money_range)+ assert (apply_tax_to_price(+ None, 'standard', taxed_money_range) == taxed_money_range)+++def test_apply_tax_to_price_no_taxes_raise_typeerror_for_invalid_type():+ with pytest.raises(TypeError):+ assert apply_tax_to_price(None, 'standard', 100)++def test_format_money():money = Money('123.99', 'USD')assert format_money(money) == '$123.99'