示例#1
0
import pytest
from mock import Mock

from saleor.core.utils import (Country, get_country_by_ip,
                               get_currency_for_country)


@pytest.mark.parametrize('ip_data, expected_country', [({
    'country': {
        'iso_code': 'PL'
    }
}, Country('PL')), ({
    'country': {
        'iso_code': 'UNKNOWN'
    }
}, None), (None, None), ({}, None), ({
    'country': {}
}, None)])
def test_get_country_by_ip(ip_data, expected_country, monkeypatch):
    monkeypatch.setattr('saleor.core.utils.georeader.get',
                        Mock(return_value=ip_data))
    country = get_country_by_ip('127.0.0.1')
    assert country == expected_country


@pytest.mark.parametrize('country, expected_currency',
                         [(Country('PL'), 'PLN'), (Country('US'), 'USD'),
                          (Country('GB'), 'GBP')])
def test_get_currency_for_country(country, expected_currency, monkeypatch):
    currency = get_currency_for_country(country)
    assert currency == expected_currency
示例#2
0
        "variant_attributes": {"GMO": ["Yes", "No"]},
        "images_dir": "candy/",
        "is_shipping_required": True,
    }
}


def test_format_money():
    money = Money("123.99", "USD")
    assert format_money(money) == "$123.99"


@pytest.mark.parametrize(
    "ip_data, expected_country",
    [
        ({"country": {"iso_code": "PL"}}, Country("PL")),
        ({"country": {"iso_code": "UNKNOWN"}}, None),
        (None, None),
        ({}, None),
        ({"country": {}}, None),
    ],
)
def test_get_country_by_ip(ip_data, expected_country, monkeypatch):
    monkeypatch.setattr("saleor.core.utils.georeader.get", Mock(return_value=ip_data))
    country = get_country_by_ip("127.0.0.1")
    assert country == expected_country


@pytest.mark.parametrize(
    "country, expected_currency",
    [(Country("PL"), "PLN"), (Country("US"), "USD"), (Country("GB"), "GBP")],
示例#3
0
            "GMO": ["Yes", "No"]
        },
        "images_dir": "candy/",
        "is_shipping_required": True,
    }
}


@pytest.mark.parametrize(
    "ip_data, expected_country",
    [
        ({
            "country": {
                "iso_code": "PL"
            }
        }, Country("PL")),
        ({
            "country": {
                "iso_code": "UNKNOWN"
            }
        }, None),
        (None, None),
        ({}, None),
        ({
            "country": {}
        }, None),
    ],
)
def test_get_country_by_ip(ip_data, expected_country, monkeypatch):
    monkeypatch.setattr("saleor.core.utils.georeader.get",
                        Mock(return_value=ip_data))
示例#4
0
type_schema = {'Vegetable': {
                   'category': 'Food',
                   'product_attributes': {
                       'Sweetness': ['Sweet', 'Sour'],
                       'Healthiness': ['Healthy', 'Not really']
                   },
                   'variant_attributes': {
                       'GMO': ['Yes', 'No']
                   },
                   'images_dir': 'candy/',
                   'is_shipping_required': True}}


@pytest.mark.parametrize('ip_data, expected_country', [
    ({'country': {'iso_code': 'PL'}}, Country('PL')),
    ({'country': {'iso_code': 'UNKNOWN'}}, None),
    (None, None),
    ({}, None),
    ({'country': {}}, None)])
def test_get_country_by_ip(ip_data, expected_country, monkeypatch):
    monkeypatch.setattr(
        'saleor.core.utils.georeader.get',
        Mock(return_value=ip_data))
    country = get_country_by_ip('127.0.0.1')
    assert country == expected_country


@pytest.mark.parametrize('country, expected_currency', [
    (Country('PL'), 'PLN'),
    (Country('US'), 'USD'),
示例#5
0
def test_get_tax_price(order_with_lines: Order, billing_address):
    order = order_with_lines
    order.billing_address = billing_address
    order.shipping_address = shipping_address = Address.objects.create(
        first_name='John',
        last_name='Doe',
        company_name='Mirumee Software',
        street_address_1='Tęczowa 7',
        city='Wrocław',
        postal_code='53-601',
        country='PL',
        phone='+48713988102')

    request_factory = RequestFactory()
    order_total = order.get_total()

    croatia_tax_rate = 0.25
    poland_tax_rate = 0.23
    default_tax_rate = 0.20

    croatia_taxed_price = order_total.gross * Decimal(1 + croatia_tax_rate)
    poland_taxed_price = order_total.gross * Decimal(1 + poland_tax_rate)

    croatia_taxed_price_10usd = Decimal(10.0) * Decimal(1 + croatia_tax_rate)
    default_taxed_price_10usd = Decimal(10.0) * Decimal(1 + default_tax_rate)

    requests = ((request_factory.post('/test_HR',
                                      {'country': 'HR'}), croatia_taxed_price,
                 croatia_tax_rate, croatia_taxed_price_10usd),
                (request_factory.post('/test_default_missing',
                                      {}), poland_taxed_price, poland_tax_rate,
                 default_taxed_price_10usd),
                (request_factory.post('/test_default_None'),
                 poland_taxed_price, poland_tax_rate,
                 default_taxed_price_10usd))

    for rq, expected_price, expected_rate, expected_price_10usd in requests:
        price, rate = get_tax_price(rq, checkout=order)
        assert rate == expected_rate
        assert_decimal(price.gross, expected_price)

        price, rate = get_tax_price(rq, total=10)
        expected_rate = (float(expected_price_10usd) - 10) / 10.0
        assert_decimal(rate, expected_rate)
        assert_decimal(price.gross, expected_price_10usd)

    shipping_address.country = Country('HR')

    price, rate = get_tax_price(None,
                                checkout=order,
                                get_first_shipping_addr=True)
    assert rate == 0.25
    assert_decimal(price.gross, croatia_taxed_price)

    price, rate = get_tax_price(None,
                                checkout=order,
                                get_first_shipping_addr=False)
    assert rate == 0.23
    assert_decimal(price.gross, poland_taxed_price)

    order.shipping_address = None
    price, rate = get_tax_price(None,
                                checkout=order,
                                get_first_shipping_addr=True)
    assert rate == 0.23
    assert_decimal(price.gross, poland_taxed_price)