Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extracting all vatlayer fixtures from main conftest to tax module
Maciej Korycinski3f153e4dcd1b
diff --git a/tests/api/test_shop.py b/tests/api/test_shop.pyindex 64246a6f4b..390e089991 100644--- a/tests/api/test_shop.py+++ b/tests/api/test_shop.py@@ -1,8 +1,5 @@-from unittest.mock import patch-import graphenefrom django_countries import countries-from django_prices_vatlayer.models import VATfrom saleor.core.permissions import MODELS_PERMISSIONSfrom saleor.graphql.core.utils import str_to_enum@@ -50,58 +47,6 @@ def test_query_countries(user_api_client):assert len(data["countries"]) == len(countries)-def test_query_countries_with_tax(user_api_client, vatlayer, tax_rates):- query = """- query {- shop {- countries {- code- vat {- standardRate- reducedRates {- rate- rateType- }- }- }- }- }- """- response = user_api_client.post_graphql(query)- content = get_graphql_content(response)- data = content["data"]["shop"]["countries"]- vat = VAT.objects.first()- country = next(country for country in data if country["code"] == vat.country_code)- assert country["vat"]["standardRate"] == tax_rates["standard_rate"]- rates = {rate["rateType"]: rate["rate"] for rate in country["vat"]["reducedRates"]}- reduced_rates = {- str_to_enum(tax_rate): tax_rates["reduced_rates"][tax_rate]- for tax_rate in tax_rates["reduced_rates"]- }- assert rates == reduced_rates---def test_query_default_country_with_tax(user_api_client, settings, vatlayer, tax_rates):- settings.DEFAULT_COUNTRY = "PL"- query = """- query {- shop {- defaultCountry {- code- vat {- standardRate- }- }- }- }- """- response = user_api_client.post_graphql(query)- content = get_graphql_content(response)- data = content["data"]["shop"]["defaultCountry"]- assert data["code"] == settings.DEFAULT_COUNTRY- assert data["vat"]["standardRate"] == tax_rates["standard_rate"]--def test_query_currencies(user_api_client, settings):query = """query {@@ -566,43 +511,6 @@ def test_mutation_authorization_key_delete(assert content["data"]["authorizationKeyDelete"]["authorizationKey"]-MUTATION_SHOP_FETCH_TAX_RATES = """- mutation FetchTaxRates {- shopFetchTaxRates {- errors {- field- message- }- }- }- """---def test_shop_fetch_tax_rates_no_api_access_key(- staff_api_client, permission_manage_settings-):- staff_api_client.user.user_permissions.add(permission_manage_settings)- response = staff_api_client.post_graphql(MUTATION_SHOP_FETCH_TAX_RATES)- content = get_graphql_content(response)- data = content["data"]["shopFetchTaxRates"]- error_message = (- "Could not fetch tax rates. "- "Make sure you have supplied a valid API Access Key."- )- assert data["errors"][0]["message"] == error_message---@patch("saleor.graphql.shop.mutations.call_command")-def test_shop_fetch_tax_rates(- mock_call_command, staff_api_client, permission_manage_settings, settings-):- settings.VATLAYER_ACCESS_KEY = "KEY"- staff_api_client.user.user_permissions.add(permission_manage_settings)- response = staff_api_client.post_graphql(MUTATION_SHOP_FETCH_TAX_RATES)- get_graphql_content(response)- mock_call_command.assert_called_once_with("get_vat_rates")--def test_mutation_update_company_address(staff_api_client,authorization_key,diff --git a/tests/api/test_shop_taxes.py b/tests/api/test_shop_taxes.pynew file mode 100644index 0000000000..d6f2a10799--- /dev/null+++ b/tests/api/test_shop_taxes.py@@ -0,0 +1,163 @@+from unittest.mock import patch++import pytest+from django_prices_vatlayer.models import VAT+from django_prices_vatlayer.utils import get_tax_for_rate++from saleor.graphql.core.utils import str_to_enum+from tests.api.utils import get_graphql_content++# FIXME we are going to rewrite tax section. Currently, below tests are connected only+# with vatlayer. After we introduce approach for taxes and API, we should rebuild this+# tests.+++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": {+ "value": tax_rates["standard_rate"],+ "tax": get_tax_for_rate(tax_rates),+ }+ }+ if tax_rates["reduced_rates"]:+ taxes.update(+ {+ rate: {+ "value": tax_rates["reduced_rates"][rate],+ "tax": get_tax_for_rate(tax_rates, rate),+ }+ for rate in tax_rates["reduced_rates"]+ }+ )+ return taxes+++def vatlayer(db, settings, tax_rates, taxes):+ settings.VATLAYER_ACCESS_KEY = "enablevatlayer"+ VAT.objects.create(country_code="PL", data=tax_rates)++ tax_rates_2 = {+ "standard_rate": 19,+ "reduced_rates": {+ "admission to cultural events": 7,+ "admission to entertainment events": 7,+ "books": 7,+ "foodstuffs": 7,+ "hotels": 7,+ "medical": 7,+ "newspapers": 7,+ "passenger transport": 7,+ },+ }+ VAT.objects.create(country_code="DE", data=tax_rates_2)+ return taxes+++def test_query_countries_with_tax(user_api_client, vatlayer, tax_rates):+ query = """+ query {+ shop {+ countries {+ code+ vat {+ standardRate+ reducedRates {+ rate+ rateType+ }+ }+ }+ }+ }+ """+ response = user_api_client.post_graphql(query)+ content = get_graphql_content(response)+ data = content["data"]["shop"]["countries"]+ vat = VAT.objects.first()+ country = next(country for country in data if country["code"] == vat.country_code)+ assert country["vat"]["standardRate"] == tax_rates["standard_rate"]+ rates = {rate["rateType"]: rate["rate"] for rate in country["vat"]["reducedRates"]}+ reduced_rates = {+ str_to_enum(tax_rate): tax_rates["reduced_rates"][tax_rate]+ for tax_rate in tax_rates["reduced_rates"]+ }+ assert rates == reduced_rates+++def test_query_default_country_with_tax(user_api_client, settings, vatlayer, tax_rates):+ settings.DEFAULT_COUNTRY = "PL"+ query = """+ query {+ shop {+ defaultCountry {+ code+ vat {+ standardRate+ }+ }+ }+ }+ """+ response = user_api_client.post_graphql(query)+ content = get_graphql_content(response)+ data = content["data"]["shop"]["defaultCountry"]+ assert data["code"] == settings.DEFAULT_COUNTRY+ assert data["vat"]["standardRate"] == tax_rates["standard_rate"]+++MUTATION_SHOP_FETCH_TAX_RATES = """+ mutation FetchTaxRates {+ shopFetchTaxRates {+ errors {+ field+ message+ }+ }+ }+ """+++def test_shop_fetch_tax_rates_no_api_access_key(+ staff_api_client, permission_manage_settings+):+ staff_api_client.user.user_permissions.add(permission_manage_settings)+ response = staff_api_client.post_graphql(MUTATION_SHOP_FETCH_TAX_RATES)+ content = get_graphql_content(response)+ data = content["data"]["shopFetchTaxRates"]+ error_message = (+ "Could not fetch tax rates. "+ "Make sure you have supplied a valid API Access Key."+ )+ assert data["errors"][0]["message"] == error_message+++@patch("saleor.graphql.shop.mutations.call_command")+def test_shop_fetch_tax_rates(+ mock_call_command, staff_api_client, permission_manage_settings, settings+):+ settings.VATLAYER_ACCESS_KEY = "KEY"+ staff_api_client.user.user_permissions.add(permission_manage_settings)+ response = staff_api_client.post_graphql(MUTATION_SHOP_FETCH_TAX_RATES)+ get_graphql_content(response)+ mock_call_command.assert_called_once_with("get_vat_rates")diff --git a/tests/api/test_variant_pricing.py b/tests/api/test_variant_pricing.pyindex 236add566a..45be7360a7 100644--- a/tests/api/test_variant_pricing.py+++ b/tests/api/test_variant_pricing.py@@ -99,7 +99,7 @@ def test_get_variant_pricing_not_on_sale(api_client, product):assert pricing["price"]["net"]["amount"] == product.price.amount-def test_variant_pricing(variant: ProductVariant, monkeypatch, settings, taxes):+def test_variant_pricing(variant: ProductVariant, monkeypatch, settings):taxed_price = TaxedMoney(Money("10.0", "USD"), Money("12.30", "USD"))monkeypatch.setattr("saleor.product.utils.availability.apply_taxes_to_product",diff --git a/tests/conftest.py b/tests/conftest.pyindex b1f1ef53eb..15790ba453 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -11,8 +11,6 @@ from django.forms import ModelFormfrom django.test.client import Clientfrom django.utils.encoding import smart_textfrom django_countries import countries-from django_prices_vatlayer.models import VAT-from django_prices_vatlayer.utils import get_tax_for_ratefrom PIL import Imagefrom prices import Money, TaxedMoney@@ -627,8 +625,7 @@ def voucher_shipping_type():@pytest.fixture()-def order_line(order, variant, vatlayer):- taxes = vatlayer+def order_line(order, variant):net = variant.get_price()gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)return order.lines.create(@@ -638,7 +635,7 @@ def order_line(order, variant, vatlayer):quantity=3,variant=variant,unit_price=TaxedMoney(net=net, gross=gross),- tax_rate=taxes["standard"]["value"],+ tax_rate=23,)@@ -1046,68 +1043,6 @@ def menu_with_items(menu, category, collection):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": {- "value": tax_rates["standard_rate"],- "tax": get_tax_for_rate(tax_rates),- }- }- if tax_rates["reduced_rates"]:- taxes.update(- {- rate: {- "value": tax_rates["reduced_rates"][rate],- "tax": get_tax_for_rate(tax_rates, rate),- }- for rate in tax_rates["reduced_rates"]- }- )- return taxes---def vatlayer(db, settings, tax_rates, taxes):- settings.VATLAYER_ACCESS_KEY = "enablevatlayer"- VAT.objects.create(country_code="PL", data=tax_rates)-- tax_rates_2 = {- "standard_rate": 19,- "reduced_rates": {- "admission to cultural events": 7,- "admission to entertainment events": 7,- "books": 7,- "foodstuffs": 7,- "hotels": 7,- "medical": 7,- "newspapers": 7,- "passenger transport": 7,- },- }- VAT.objects.create(country_code="DE", data=tax_rates_2)- return taxes--@pytest.fixturedef translated_variant_fr(product):attribute = product.product_type.variant_attributes.first()diff --git a/tests/taxes/conftest.py b/tests/taxes/conftest.pyindex e69de29bb2..9382290ce6 100644--- a/tests/taxes/conftest.py+++ b/tests/taxes/conftest.py@@ -0,0 +1,65 @@+import pytest+from django_prices_vatlayer.models import VAT+from django_prices_vatlayer.utils import get_tax_for_rate+++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": {+ "value": tax_rates["standard_rate"],+ "tax": get_tax_for_rate(tax_rates),+ }+ }+ if tax_rates["reduced_rates"]:+ taxes.update(+ {+ rate: {+ "value": tax_rates["reduced_rates"][rate],+ "tax": get_tax_for_rate(tax_rates, rate),+ }+ for rate in tax_rates["reduced_rates"]+ }+ )+ return taxes+++def vatlayer(db, settings, tax_rates, taxes):+ settings.VATLAYER_ACCESS_KEY = "enablevatlayer"+ VAT.objects.create(country_code="PL", data=tax_rates)++ tax_rates_2 = {+ "standard_rate": 19,+ "reduced_rates": {+ "admission to cultural events": 7,+ "admission to entertainment events": 7,+ "books": 7,+ "foodstuffs": 7,+ "hotels": 7,+ "medical": 7,+ "newspapers": 7,+ "passenger transport": 7,+ },+ }+ VAT.objects.create(country_code="DE", data=tax_rates_2)+ return taxesdiff --git a/tests/test_product.py b/tests/test_product.pyindex 7d34f61ece..a9a71e55f6 100644--- a/tests/test_product.py+++ b/tests/test_product.py@@ -161,7 +161,7 @@ def test_render_home_page_with_sale(client, product, sale):assert response.status_code == 200-def test_render_home_page_with_taxes(client, product, vatlayer):+def test_render_home_page_with_taxes(client, product):response = client.get(reverse("home"))assert response.status_code == 200@@ -176,7 +176,7 @@ def test_render_category_with_sale(client, category, product, sale):assert response.status_code == 200-def test_render_category_with_taxes(client, category, product, vatlayer):+def test_render_category_with_taxes(client, category, product):response = client.get(category.get_absolute_url())assert response.status_code == 200@@ -191,7 +191,7 @@ def test_render_product_detail_with_sale(client, product, sale):assert response.status_code == 200-def test_render_product_detail_with_taxes(client, product, vatlayer):+def test_render_product_detail_with_taxes(client, product):response = client.get(product.get_absolute_url())assert response.status_code == 200@@ -823,7 +823,7 @@ def test_digital_product_view_url_expired(client, digital_content):assert response.status_code == 404-def test_variant_picker_data_price_range(product_type, taxes, category):+def test_variant_picker_data_price_range(product_type, category):product = models.Product.objects.create(product_type=product_type, category=category, price=Money("15.00", "USD"))diff --git a/tests/test_product_availability.py b/tests/test_product_availability.pyindex 14b709ed99..b963c92a01 100644--- a/tests/test_product_availability.py+++ b/tests/test_product_availability.py@@ -71,7 +71,7 @@ def test_variant_availability_status(unavailable_product):get_variant_availability_status(variant)-def test_availability(product, monkeypatch, settings, taxes):+def test_availability(product, monkeypatch, settings):taxed_price = TaxedMoney(Money("10.0", "USD"), Money("12.30", "USD"))monkeypatch.setattr("saleor.product.utils.availability.apply_taxes_to_product",