<s> from decimal import Decimal as D <EOL> from django . core import exceptions <EOL> from django . test import TestCase <EOL> import mock <EOL> from oscar . apps . offer import models <EOL> from oscar . apps . offer . utils import Applicator <EOL> from oscar . test . basket import add_product , add_products <EOL> from oscar . test import factories <EOL> class TestAnAbsoluteDiscountAppliedWithCountConditionOnDifferentRange ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . condition_product = factories . ProductFactory ( ) <EOL> condition_range = factories . RangeFactory ( ) <EOL> condition_range . add_product ( self . condition_product ) <EOL> self . condition = models . CountCondition . objects . create ( <EOL> range = condition_range , <EOL> type = models . Condition . COUNT , <EOL> value = <NUM_LIT:2> ) <EOL> self . benefit_product = factories . ProductFactory ( ) <EOL> benefit_range = factories . RangeFactory ( ) <EOL> benefit_range . add_product ( self . benefit_product ) <EOL> self . benefit = models . AbsoluteDiscountBenefit . objects . create ( <EOL> range = benefit_range , <EOL> type = models . Benefit . FIXED , <EOL> value = D ( '<STR_LIT>' ) ) <EOL> self . offer = models . ConditionalOffer ( <EOL> id = <NUM_LIT:1> , condition = self . condition , benefit = self . benefit ) <EOL> self . basket = factories . create_basket ( empty = True ) <EOL> self . applicator = Applicator ( ) <EOL> def test_succcessful_application_consumes_correctly ( self ) : <EOL> add_product ( self . basket , product = self . condition_product , quantity = <NUM_LIT:2> ) <EOL> add_product ( self . basket , product = self . benefit_product , quantity = <NUM_LIT:1> ) <EOL> self . applicator . apply_offers ( self . basket , [ self . offer ] ) <EOL> discounts = self . basket . offer_applications . offer_discounts <EOL> self . assertEqual ( len ( discounts ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( discounts [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> def test_condition_is_consumed_correctly ( self ) : <EOL> add_product ( self . basket , product = self . condition_product , quantity = <NUM_LIT:3> ) <EOL> add_product ( self . basket , product = self . benefit_product , quantity = <NUM_LIT:2> ) <EOL> self . applicator . apply_offers ( self . basket , [ self . offer ] ) <EOL> discounts = self . basket . offer_applications . offer_discounts <EOL> self . assertEqual ( len ( discounts ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( discounts [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> class TestAnAbsoluteDiscountAppliedWithCountCondition ( TestCase ) : <EOL> def setUp ( self ) : <EOL> range = models . Range . objects . create ( <EOL> name = "<STR_LIT>" , includes_all_products = True ) <EOL> self . condition = models . CountCondition . objects . create ( <EOL> range = range , <EOL> type = models . Condition . COUNT , <EOL> value = <NUM_LIT:2> ) <EOL> self . offer = mock . Mock ( ) <EOL> self . benefit = models . AbsoluteDiscountBenefit . objects . create ( <EOL> range = range , <EOL> type = models . Benefit . FIXED , <EOL> value = D ( '<STR_LIT>' ) ) <EOL> self . basket = factories . create_basket ( empty = True ) <EOL> def test_applies_correctly_to_empty_basket ( self ) : <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_matches_condition_with_one_line ( self ) : <EOL> add_product ( self . basket , price = D ( '<STR_LIT>' ) , quantity = <NUM_LIT:2> ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> line = self . basket . all_lines ( ) [ <NUM_LIT:0> ] <EOL> prices = line . get_price_breakdown ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( prices ) ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , prices [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> def test_applies_correctly_to_basket_which_matches_condition_with_multiple_lines ( self ) : <EOL> add_products ( self . basket , [ <EOL> ( D ( '<STR_LIT>' ) , <NUM_LIT:1> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:1> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertTrue ( result . is_successful ) <EOL> self . assertFalse ( result . is_final ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> for line in self . basket . all_lines ( ) : <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , line . discount_value ) <EOL> def test_applies_correctly_to_basket_which_matches_condition_with_multiple_lines_and_lower_total_value ( self ) : <EOL> add_products ( self . basket , [ <EOL> ( D ( '<STR_LIT>' ) , <NUM_LIT:1> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:1> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertTrue ( result . is_successful ) <EOL> self . assertFalse ( result . is_final ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_exceeds_condition ( self ) : <EOL> add_products ( self . basket , [ <EOL> ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:4> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_exceeds_condition_with_smaller_prices_than_discount ( self ) : <EOL> add_products ( self . basket , [ <EOL> ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:4> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_exceeds_condition_with_smaller_prices_than_discount_and_higher_prices_first ( self ) : <EOL> add_products ( self . basket , [ <EOL> ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:4> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> class TestAnAbsoluteDiscount ( TestCase ) : <EOL> def setUp ( self ) : <EOL> range = models . Range . objects . create ( <EOL> name = "<STR_LIT>" , includes_all_products = True ) <EOL> self . condition = models . CountCondition . objects . create ( <EOL> range = range , <EOL> type = models . Condition . COUNT , <EOL> value = <NUM_LIT:2> ) <EOL> self . benefit = models . AbsoluteDiscountBenefit . objects . create ( <EOL> range = range , <EOL> type = models . Benefit . FIXED , <EOL> value = D ( '<STR_LIT>' ) ) <EOL> self . offer = mock . Mock ( ) <EOL> self . basket = factories . create_basket ( empty = True ) <EOL> def test_applies_correctly_when_discounts_need_rounding ( self ) : <EOL> for price in [ D ( '<STR_LIT>' ) , D ( '<STR_LIT>' ) , D ( '<STR_LIT>' ) ] : <EOL> add_product ( self . basket , price ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> line_discounts = [ line . discount_value for line in self . basket . all_lines ( ) ] <EOL> self . assertEqual ( len ( line_discounts ) , <NUM_LIT:3> ) <EOL> for i , v in enumerate ( [ D ( '<STR_LIT>' ) , D ( '<STR_LIT>' ) , D ( '<STR_LIT>' ) ] ) : <EOL> self . assertEqual ( line_discounts [ i ] , v ) <EOL> class TestAnAbsoluteDiscountWithMaxItemsSetAppliedWithCountCondition ( TestCase ) : <EOL> def setUp ( self ) : <EOL> range = models . Range . objects . create ( <EOL> name = "<STR_LIT>" , includes_all_products = True ) <EOL> self . condition = models . CountCondition . objects . create ( <EOL> range = range , <EOL> type = models . Condition . COUNT , <EOL> value = <NUM_LIT:2> ) <EOL> self . benefit = models . AbsoluteDiscountBenefit . objects . create ( <EOL> range = range , <EOL> type = models . Benefit . FIXED , <EOL> value = D ( '<STR_LIT>' ) , <EOL> max_affected_items = <NUM_LIT:1> ) <EOL> self . offer = mock . Mock ( ) <EOL> self . basket = factories . create_basket ( empty = True ) <EOL> def test_applies_correctly_to_empty_basket ( self ) : <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_matches_condition ( self ) : <EOL> add_product ( self . basket , D ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_exceeds_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_basket_which_exceeds_condition_but_with_smaller_prices_than_discount ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) , ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_without_discount ) <EOL> class TestAnAbsoluteDiscountAppliedWithValueCondition ( TestCase ) : <EOL> def setUp ( self ) : <EOL> range = models . Range . objects . create ( <EOL> name = "<STR_LIT>" , includes_all_products = True ) <EOL> self . condition = models . ValueCondition . objects . create ( <EOL> range = range , <EOL> type = models . Condition . VALUE , <EOL> value = D ( '<STR_LIT>' ) ) <EOL> self . benefit = models . AbsoluteDiscountBenefit . objects . create ( <EOL> range = range , <EOL> type = models . Benefit . FIXED , <EOL> value = D ( '<STR_LIT>' ) ) <EOL> self . offer = mock . Mock ( ) <EOL> self . basket = factories . create_basket ( empty = True ) <EOL> def test_applies_correctly_to_empty_basket ( self ) : <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_single_item_basket_which_matches_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:1> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_matches_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_exceeds_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:3> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:3> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_exceeds_condition_but_matches_boundary ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:3> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:3> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> class TestAnAbsoluteDiscountWithMaxItemsSetAppliedWithValueCondition ( TestCase ) : <EOL> def setUp ( self ) : <EOL> range = models . Range . objects . create ( <EOL> name = "<STR_LIT>" , includes_all_products = True ) <EOL> self . condition = models . ValueCondition . objects . create ( <EOL> range = range , <EOL> type = models . Condition . VALUE , <EOL> value = D ( '<STR_LIT>' ) ) <EOL> self . benefit = models . AbsoluteDiscountBenefit . objects . create ( <EOL> range = range , <EOL> type = models . Benefit . FIXED , <EOL> value = D ( '<STR_LIT>' ) , <EOL> max_affected_items = <NUM_LIT:1> ) <EOL> self . offer = mock . Mock ( ) <EOL> self . basket = factories . create_basket ( empty = True ) <EOL> def test_applies_correctly_to_empty_basket ( self ) : <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_single_item_basket_which_matches_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:1> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_matches_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:2> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_exceeds_condition ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:3> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:3> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:0> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_exceeds_condition_but_matches_boundary ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:3> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:2> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . basket . num_items_without_discount ) <EOL> def test_applies_correctly_to_multi_item_basket_which_matches_condition_but_with_lower_prices_than_discount ( self ) : <EOL> add_products ( self . basket , [ ( D ( '<STR_LIT>' ) , <NUM_LIT:6> ) ] ) <EOL> result = self . benefit . apply ( self . basket , self . condition , self . offer ) <EOL> self . assertEqual ( D ( '<STR_LIT>' ) , result . discount ) <EOL> self . assertEqual ( <NUM_LIT:5> , self . basket . num_items_with_discount ) <EOL> self . assertEqual ( <NUM_LIT:1> , self . basket . num_items_without_discount ) <EOL> class TestAnAbsoluteDiscountBenefit ( TestCase ) : <EOL> def test_requires_a_benefit_value ( self ) : <EOL> rng = models . Range . objects . create ( <EOL> name = "<STR_LIT>" , includes_all_products = True ) <EOL> benefit = models . Benefit . objects . create ( <EOL> type = models . Benefit . FIXED , range = rng <EOL> ) <EOL> with self . assertRaises ( exceptions . ValidationError ) : <EOL> benefit . clean ( ) </s>
