<s> from decimal import Decimal <EOL> from mock import Mock <EOL> from prices import FixedDiscount , FractionalDiscount , Price <EOL> import pytest <EOL> from . . product . models import ProductVariant , Product <EOL> from . models import Sale , Voucher , NotApplicable <EOL> @ pytest . fixture <EOL> def product ( ) : <EOL> return Product . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> price = <NUM_LIT:10> , <EOL> weight = <NUM_LIT:1> ) <EOL> @ pytest . fixture <EOL> def product_variant ( product ) : <EOL> return ProductVariant . objects . create ( <EOL> product = product , <EOL> sku = '<STR_LIT>' , <EOL> name = '<STR_LIT>' ) <EOL> @ pytest . mark . integration <EOL> @ pytest . mark . django_db ( transaction = True ) <EOL> def test_variant_discounts ( product_variant ) : <EOL> product = product_variant . product <EOL> low_discount = Sale . objects . create ( <EOL> type = Sale . FIXED , <EOL> value = <NUM_LIT:5> ) <EOL> low_discount . products . add ( product ) <EOL> discount = Sale . objects . create ( <EOL> type = Sale . FIXED , <EOL> value = <NUM_LIT:8> ) <EOL> discount . products . add ( product ) <EOL> high_discount = Sale . objects . create ( <EOL> type = Sale . FIXED , <EOL> value = <NUM_LIT:50> ) <EOL> high_discount . products . add ( product ) <EOL> final_price = product_variant . get_price_per_item ( <EOL> discounts = Sale . objects . all ( ) ) <EOL> assert final_price . gross == <NUM_LIT:0> <EOL> applied_discount = final_price . history . right <EOL> assert isinstance ( applied_discount , FixedDiscount ) <EOL> assert applied_discount . amount . gross == <NUM_LIT:50> <EOL> @ pytest . mark . integration <EOL> @ pytest . mark . django_db ( transaction = True ) <EOL> def test_percentage_discounts ( product_variant ) : <EOL> discount = Sale . objects . create ( <EOL> type = Sale . PERCENTAGE , <EOL> value = <NUM_LIT:50> ) <EOL> discount . products . add ( product_variant . product ) <EOL> final_price = product_variant . get_price_per_item ( discounts = [ discount ] ) <EOL> assert final_price . gross == <NUM_LIT:5> <EOL> applied_discount = final_price . history . right <EOL> assert isinstance ( applied_discount , FractionalDiscount ) <EOL> assert applied_discount . factor == Decimal ( '<STR_LIT>' ) <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( '<STR_LIT:100>' , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , None , <NUM_LIT:10> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_PERCENTAGE , <NUM_LIT:100> , <NUM_LIT:10> ) ] ) <EOL> def test_value_voucher_checkout_discount ( settings , total , discount_value , <EOL> discount_type , limit , expected_value ) : <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . VALUE_TYPE , <EOL> discount_value_type = discount_type , <EOL> discount_value = discount_value , <EOL> limit = Price ( limit , currency = '<STR_LIT>' ) if limit is not None else None ) <EOL> checkout = Mock ( cart = Mock ( get_total = Mock ( <EOL> return_value = Price ( total , currency = '<STR_LIT>' ) ) ) ) <EOL> discount = voucher . get_discount_for_checkout ( checkout ) <EOL> assert discount . amount == Price ( expected_value , currency = '<STR_LIT>' ) <EOL> def test_value_voucher_checkout_discount_not_applicable ( settings ) : <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . VALUE_TYPE , <EOL> discount_value_type = Voucher . DISCOUNT_VALUE_FIXED , <EOL> discount_value = <NUM_LIT:10> , <EOL> limit = <NUM_LIT:100> ) <EOL> checkout = Mock ( cart = Mock ( get_total = Mock ( <EOL> return_value = Price ( <NUM_LIT:10> , currency = '<STR_LIT>' ) ) ) ) <EOL> with pytest . raises ( NotApplicable ) as e : <EOL> voucher . get_discount_for_checkout ( checkout ) <EOL> assert str ( e . value ) == '<STR_LIT>' <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( <NUM_LIT:10> , None , <NUM_LIT:50> , Voucher . DISCOUNT_VALUE_PERCENTAGE , None , <NUM_LIT:5> ) , <EOL> ( <NUM_LIT:10> , None , <NUM_LIT:20> , Voucher . DISCOUNT_VALUE_FIXED , None , <NUM_LIT:10> ) , <EOL> ( <NUM_LIT:10> , '<STR_LIT>' , <NUM_LIT:20> , Voucher . DISCOUNT_VALUE_FIXED , '<STR_LIT>' , <NUM_LIT:10> ) , <EOL> ( <NUM_LIT:5> , '<STR_LIT>' , <NUM_LIT:5> , Voucher . DISCOUNT_VALUE_FIXED , '<STR_LIT>' , <NUM_LIT:5> ) ] ) <EOL> def test_shipping_voucher_checkout_discount ( <EOL> settings , shipping_cost , shipping_country_code , discount_value , <EOL> discount_type , apply_to , expected_value ) : <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> checkout = Mock ( <EOL> is_shipping_required = True , shipping_method = Mock ( <EOL> price = Price ( shipping_cost , currency = '<STR_LIT>' ) , <EOL> country_code = shipping_country_code ) ) <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . SHIPPING_TYPE , <EOL> discount_value_type = discount_type , <EOL> discount_value = discount_value , <EOL> apply_to = apply_to ) <EOL> discount = voucher . get_discount_for_checkout ( checkout ) <EOL> assert discount . amount == Price ( expected_value , currency = '<STR_LIT>' ) <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( True , Mock ( country_code = '<STR_LIT>' ) , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , '<STR_LIT>' , <EOL> None , '<STR_LIT>' ) , <EOL> ( True , None , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , None , None , <EOL> '<STR_LIT>' ) , <EOL> ( False , None , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , None , None , <EOL> '<STR_LIT>' ) , <EOL> ( True , Mock ( price = Price ( <NUM_LIT:10> , currency = '<STR_LIT>' ) ) , <NUM_LIT:10> , <EOL> Voucher . DISCOUNT_VALUE_FIXED , None , <NUM_LIT:5> , <EOL> '<STR_LIT>' ) ] ) <EOL> def test_shipping_voucher_checkout_discountnot_applicable ( <EOL> settings , is_shipping_required , shipping_method , discount_value , <EOL> discount_type , apply_to , limit , error_msg ) : <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> checkout = Mock ( is_shipping_required = is_shipping_required , <EOL> shipping_method = shipping_method ) <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . SHIPPING_TYPE , <EOL> discount_value_type = discount_type , <EOL> discount_value = discount_value , <EOL> limit = Price ( limit , currency = '<STR_LIT>' ) if limit is not None else None , <EOL> apply_to = apply_to ) <EOL> with pytest . raises ( NotApplicable ) as e : <EOL> voucher . get_discount_for_checkout ( checkout ) <EOL> assert str ( e . value ) == error_msg <EOL> def test_product_voucher_checkout_discount_not_applicable ( settings , <EOL> monkeypatch ) : <EOL> monkeypatch . setattr ( <EOL> '<STR_LIT>' , <EOL> lambda cart , product : [ ] ) <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . PRODUCT_TYPE , <EOL> discount_value_type = Voucher . DISCOUNT_VALUE_FIXED , <EOL> discount_value = <NUM_LIT:10> ) <EOL> checkout = Mock ( cart = Mock ( ) ) <EOL> with pytest . raises ( NotApplicable ) as e : <EOL> voucher . get_discount_for_checkout ( checkout ) <EOL> assert str ( e . value ) == '<STR_LIT>' <EOL> def test_category_voucher_checkout_discount_not_applicable ( settings , <EOL> monkeypatch ) : <EOL> monkeypatch . setattr ( <EOL> '<STR_LIT>' , <EOL> lambda cart , product : [ ] ) <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . CATEGORY_TYPE , <EOL> discount_value_type = Voucher . DISCOUNT_VALUE_FIXED , <EOL> discount_value = <NUM_LIT:10> ) <EOL> checkout = Mock ( cart = Mock ( ) ) <EOL> with pytest . raises ( NotApplicable ) as e : <EOL> voucher . get_discount_for_checkout ( checkout ) <EOL> assert str ( e . value ) == '<STR_LIT>' <EOL> @ pytest . mark . parametrize ( <EOL> '<STR_LIT>' , [ <EOL> ( [ <NUM_LIT:10> ] , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ONE_PRODUCT , <NUM_LIT:10> ) , <EOL> ( [ <NUM_LIT:5> ] , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ONE_PRODUCT , <NUM_LIT:5> ) , <EOL> ( [ <NUM_LIT:5> , <NUM_LIT:5> ] , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ONE_PRODUCT , <NUM_LIT:10> ) , <EOL> ( [ <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ONE_PRODUCT , <NUM_LIT:5> ) , <EOL> ( [ <NUM_LIT:10> , <NUM_LIT:10> ] , <NUM_LIT:5> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ALL_PRODUCTS , <NUM_LIT:10> ) , <EOL> ( [ <NUM_LIT:5> , <NUM_LIT:2> ] , <NUM_LIT:5> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ALL_PRODUCTS , <NUM_LIT:7> ) , <EOL> ( [ <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ] , <NUM_LIT:5> , Voucher . DISCOUNT_VALUE_FIXED , Voucher . APPLY_TO_ALL_PRODUCTS , <NUM_LIT:15> ) , <EOL> ( [ <NUM_LIT:10> ] , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_PERCENTAGE , None , <NUM_LIT:1> ) , <EOL> ( [ <NUM_LIT:10> , <NUM_LIT:10> ] , <NUM_LIT:10> , Voucher . DISCOUNT_VALUE_PERCENTAGE , None , <NUM_LIT:2> ) ] ) <EOL> def test_products_voucher_checkout_discount_not ( settings , monkeypatch , prices , <EOL> discount_value , discount_type , <EOL> apply_to , expected_value ) : <EOL> monkeypatch . setattr ( <EOL> '<STR_LIT>' , <EOL> lambda cart , product : ( <EOL> ( None , Price ( p , currency = '<STR_LIT>' ) ) for p in prices ) ) <EOL> settings . DEFAULT_CURRENCY = '<STR_LIT>' <EOL> voucher = Voucher ( <EOL> code = '<STR_LIT>' , type = Voucher . PRODUCT_TYPE , <EOL> discount_value_type = discount_type , <EOL> discount_value = discount_value , <EOL> apply_to = apply_to ) <EOL> checkout = Mock ( cart = Mock ( ) ) <EOL> discount = voucher . get_discount_for_checkout ( checkout ) <EOL> assert discount . amount == Price ( expected_value , currency = '<STR_LIT>' ) </s>
