Ejemplo n.º 1
0
class TestCheckout(unittest.TestCase):
    def setUp(self):
        super().setUp()
        self.checkout = Checkout(pricing_rules=[BogofRule, BulkDiscount])

    def test_without_discount(self):
        self.checkout.scan('FR1')
        self.assertEqual(self.checkout.total(), 3.11)

    def test_buy_one_get_one_free_fruit_tea(self):
        self.checkout.scan('FR1')
        self.checkout.scan('FR1')
        self.assertEqual(self.checkout.total(), 3.11)

    def test_buy_one_get_one_free_discount(self):
        self.checkout.scan('FR1')
        self.checkout.scan('SR1')
        self.checkout.scan('FR1')
        self.checkout.scan('FR1')
        self.checkout.scan('CF1')

        self.assertEqual(self.checkout.total(), 22.45)

    def test_bulk_discount(self):
        self.checkout.scan('SR1')
        self.checkout.scan('SR1')
        self.checkout.scan('FR1')
        self.checkout.scan('SR1')

        self.assertEqual(self.checkout.total(), 16.61)
Ejemplo n.º 2
0
 def test_threshold_save_above_threshold(self):
     threshold_discount = ThresholdDiscount(10, 10)
     checkout = Checkout((STUB_ITEM_NO_DISCOUNT, ), threshold_discount)
     eq_(
         18,
         checkout.total(
             (STUB_ITEM_NO_DISCOUNT.id, STUB_ITEM_NO_DISCOUNT.id)))
Ejemplo n.º 3
0
 def test_percentage_discount_multiple_exact(self):
     checkout = Checkout((STUB_ITEM_PERCENT_DISCOUNT, ))
     discounted_item_id = STUB_ITEM_PERCENT_DISCOUNT.id
     eq_(
         72,
         checkout.total((discounted_item_id, discounted_item_id,
                         discounted_item_id, discounted_item_id)))
Ejemplo n.º 4
0
 def test_percentage_discount_additional(self):
     checkout = Checkout((STUB_ITEM_PERCENT_DISCOUNT, ))
     discounted_item_id = STUB_ITEM_PERCENT_DISCOUNT.id
     eq_(
         56,
         checkout.total(
             (discounted_item_id, discounted_item_id, discounted_item_id)))
Ejemplo n.º 5
0
 def testCheckOutEx2(self):
     ps1 = PricingStrategies()
     co = Checkout(ps1)
     exbucket = ["atv", "ipd", "ipd", "atv", "ipd", "ipd", "ipd"]
     for item in exbucket:
         co.scan(item)
     self.assertEqual(co.total(), 2718.95)
Ejemplo n.º 6
0
 def test_simple(self):
     with open('prices.txt','r') as fin:
         prices = [row[:-1] for row in fin]
     c = Checkout(Prices(prices[1:]))
     for i in 'ABBA':
         c.scan(i)
     self.assertEqual(c.total(),145)
Ejemplo n.º 7
0
 def price(self,goods):
   with open('prices.txt','r') as fin:
       prices = [row[:-1] for row in fin]
   co = Checkout(Prices(prices[1:]))
   for item in goods:
       co.scan(item)
   return co.total()
Ejemplo n.º 8
0
 def testCheckOutEx4(self):
     ps1 = PricingStrategies()
     co = Checkout(ps1)
     exbucket = ["mbp", "mbp", "vga", "vga"]
     for item in exbucket:
         co.scan(item)
     self.assertEqual(co.total(), 2799.98)
Ejemplo n.º 9
0
 def test_incremental(self):
     co = Checkout(items)
     self.assertEqual(0, co.total())
     co.scan("A")
     self.assertEqual(50, co.total())
     co.scan("B")
     self.assertEqual(80, co.total())
     co.scan("A")
     self.assertEqual(130, co.total())
     co.scan("A")
     self.assertEqual(160, co.total())
     co.scan("B")
     self.assertEqual(175, co.total())
Ejemplo n.º 10
0
 def test_incremental(self):
   with open('prices.txt','r') as fin:
       prices = [row[:-1] for row in fin]
   co = Checkout(Prices(prices[1:]))
   self.assertEqual(  0, co.total())
   co.scan("A")
   self.assertEqual( 50, co.total())
   co.scan("B")
   self.assertEqual( 80, co.total())
   co.scan("A")
   self.assertEqual(130, co.total())
   co.scan("A")
   self.assertEqual(160, co.total())
   co.scan("B")
   self.assertEqual(175, co.total())
Ejemplo n.º 11
0
 def test_other_discount_disqualifies(self):
     threshold_discount = ThresholdDiscount(40, 10)
     checkout = Checkout((STUB_ITEM_PERCENT_DISCOUNT, ), threshold_discount)
     discounted_item_id = STUB_ITEM_PERCENT_DISCOUNT.id
     eq_(36, checkout.total((discounted_item_id, discounted_item_id)))
Ejemplo n.º 12
0
class TestPricingRules(unittest.TestCase):
    '''
    Unit tests for checkout module
    '''
    def init_pricingrules(self):
        '''
        This function initialises PricingRules, Checkout objects from real files
        TODO: turn it into decorator.
        '''

        self.pricing_rules = PricingRules(
            '/code/tests/config/catalog.yaml',
            '/code/tests/config/pricingrules.yaml')
        self.checkout = Checkout(self.pricing_rules)

    def teardown_pricingrules_and_catalog(self):
        '''
        Tear down Checkout object.
        Execute this after each unit test.
        TODO: turn it into decorator.
        '''
        self.checkout = None

    def test_discount_every_third_apple_tv_bought_3(self):
        '''
        Testing pricing rule discount_every_third when buying 3 Apple TVs and one VGA.
        SKUs Scanned: atv, atv, atv, vga
        Total expected: $249.00
        '''

        self.init_pricingrules()

        for sku in ['atv', 'atv', 'atv', 'vga']:
            self.checkout.scan(sku)

        #
        self.assertEqual(249.00, self.checkout.total())

        self.teardown_pricingrules_and_catalog()

    def test_discount_every_third_apple_tv_bought_4(self):
        '''
        Testing pricing rule discount_every_third when buying 4 Apple TVs and one VGA.
        Make sure that discount_every_third pricing rule works fine on Apple TV.
        SKUs Scanned: atv, atv, atv, atv, vga
        Total expected: 109.50 * 4 - 109.50 + 30.00 = $358.5
        '''

        self.init_pricingrules()

        for sku in ['atv', 'atv', 'atv', 'atv', 'vga']:
            self.checkout.scan(sku)

        #
        self.assertEqual(109.50 * 4 - 109.50 + 30.00, self.checkout.total())

        self.teardown_pricingrules_and_catalog()

    def test_discount_every_third_apple_tv_bought_5(self):
        '''
        Testing pricing rule discount_every_third when buying 5 Apple TVs and one VGA.
        Make sure that discount_every_third pricing rule works fine on Apple TV.
        SKUs Scanned: atv, atv, atv, atv, atv, vga
        Total expected: 109.50 * 5 - 109.50 + 30.00 = $468.0
        '''

        self.init_pricingrules()

        for sku in ['atv', 'atv', 'atv', 'atv', 'atv', 'vga']:
            self.checkout.scan(sku)

        #
        self.assertEqual(109.50 * 5 - 109.50 + 30.00, self.checkout.total())

        self.teardown_pricingrules_and_catalog()

    def test_discount_every_third_apple_tv_bought_7(self):
        '''
        Testing pricing rule discount_every_third when buying 5 Apple TVs and one VGA.
        Make sure that discount_every_third pricing rule works fine on Apple TV.
        SKUs Scanned: atv, atv, atv, atv, atv, atv, atv, vga
        Total expected: 109.50 * 7 - 109.50 * 2 + 30.00 = $687.0
        '''

        self.init_pricingrules()

        for sku in ['atv', 'atv', 'atv', 'atv', 'atv', 'atv', 'atv', 'vga']:
            self.checkout.scan(sku)

        self.assertEqual(109.50 * 7 - 109.50 * 2 + 30.00,
                         self.checkout.total())

        self.teardown_pricingrules_and_catalog()
Ejemplo n.º 13
0
 def test_works_on_empty(self):
     """**Scenario** Works for an empty cart"""
     checkout = Checkout()
     total = checkout.total()
     self.assertEqual(total, 0)
Ejemplo n.º 14
0
from checkout import Checkout
import json

# DISCOUNT_TYPES:
#                1. Buy N, get an item free ("buyN")
#                2. Group Discount ("groupD")

# * We're going to have a 3 for 2 deal on Apple TVs. For example, if you buy 3 Apple TVs, you will pay the price of 2 only
# * The brand new Super iPad will have a bulk discount applied, where the price will drop to $499.99 each, if someone buys more than 4
# * We will bundle in a free VGA adapter free of charge with every MacBook Pro sold

if __name__ == '__main__':
    pricingRules = json.load(open("pricingRules.json"))
    co = Checkout(pricingRules)
    co.scan("mbp")
    co.scan("vga")
    co.scan("ipd")
    co.total()
Ejemplo n.º 15
0
class TestCheckout(unittest.TestCase):
    '''
    Unit tests for checkout module
    '''
    def init_pricingrules(self):
        '''
        This function initialises PricingRules, Checkout objects from real files
        TODO: turn it into decorator.
        '''

        self.pricing_rules = PricingRules(
            '/code/tests/config/catalog.yaml',
            '/code/tests/config/pricingrules.yaml')
        self.checkout = Checkout(self.pricing_rules)

    def teardown_pricingrules_and_catalog(self):
        '''
        Tear down Checkout object.

        Execute this after each unit test.

        TODO: turn it into decorator.
        '''
        self.checkout = None

    def test_constructor(self):
        '''
        Testing constructor
        '''

        self.init_pricingrules()

        # Make sure selected_items is an empty dict
        self.assertEqual(self.checkout.selected_items, {})
        # Make sure we have set zero total_sum
        self.assertEqual(self.checkout.total_sum, 0)
        # Make sure that pricing_rules is set to the mocked PricingRules object
        self.assertEqual(True,
                         isinstance(self.checkout.pricing_rules, PricingRules))

        self.teardown_pricingrules_and_catalog()

    def test_scan_throws_exception(self):
        '''
        Test that scan() throws exception if someone scans SKU which is not in catalog
        '''

        self.init_pricingrules()

        with self.assertRaises(Exception):
            self.checkout.scan('no-such-SKU')

        self.teardown_pricingrules_and_catalog()

    def test_scan_quantity_increments(self):
        '''
        Make sure that quantity is set to 1 if Apple TV SKU is scanned first time.
        Make sure that quantity is incremented if Apple TV SKU is scanned second time.
        '''
        self.init_pricingrules()

        # First scan
        self.checkout.scan('atv')
        self.assertEqual(1, self.checkout.selected_items['atv']['quantity'])

        # Second scan
        self.checkout.scan('atv')
        self.assertEqual(2, self.checkout.selected_items['atv']['quantity'])

        self.teardown_pricingrules_and_catalog()

    def test_total_no_discount(self):
        '''
        SKUs Scanned: atv, atv, atv, vga Total expected: 358.5 (no discounts)
        '''

        self.init_pricingrules()

        for sku in ['atv', 'atv', 'atv', 'vga']:
            self.checkout.scan(sku)

        actual = self.checkout.total()
        expected = float(249.00)
        # expected = float(358.5)

        self.assertEqual(actual, expected)

        self.teardown_pricingrules_and_catalog()
Ejemplo n.º 16
0
def checkout_total(goods):
    checkout = Checkout(RULES)
    for item in goods:
        checkout.scan(item)
    return checkout.total()
Ejemplo n.º 17
0
 def test_simple_total(self):
     checkout = Checkout((STUB_ITEM_NO_DISCOUNT, ))
     eq_(
         20,
         checkout.total(
             (STUB_ITEM_NO_DISCOUNT.id, STUB_ITEM_NO_DISCOUNT.id)))
Ejemplo n.º 18
0
 def test_empty(self):
     checkout = Checkout((STUB_ITEM_NO_DISCOUNT, ))
     eq_(0, checkout.total([]))
Ejemplo n.º 19
0
 def test_percentage_discount_not_qualifed(self):
     checkout = Checkout((STUB_ITEM_PERCENT_DISCOUNT, ))
     eq_(20, checkout.total((STUB_ITEM_PERCENT_DISCOUNT.id, )))
Ejemplo n.º 20
0
from checkout import Checkout
from pricingrules import PricingRules

# Step 1: Create PricingRules object:
pricing_rules = PricingRules('/code/config/catalog.yaml',
    '/code/config/pricingrules.yaml')

# Step 2: Create Checkout object from previously created PricingRules objects.
checkout = Checkout(pricing_rules)

# Step 3: Scan a few SKUs
for sku in ['atv', 'atv', 'atv', 'vga']:
    checkout.scan(sku)

print(checkout.total())
Ejemplo n.º 21
0
    def price(self, goods):
        co = Checkout(items)
        for item in goods:
            co.scan(item)

        return co.total()