Beispiel #1
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)
Beispiel #2
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()
Beispiel #3
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)
Beispiel #4
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)
 def test_three_strawberries_one_fruit_tea(self):
     co = Checkout(PricingRules().list())
     co.scan("SR1")
     co.scan("SR1")
     co.scan("FR1")
     co.scan("SR1")
     assert co.calculate() == 16.61
Beispiel #6
0
 def test_three_b(self):
     checkout = Checkout()
     checkout.scan("B")
     checkout.scan("B")
     checkout.scan("B")
     checkout.scan("B")
     self.assertEqual(90, checkout.total)
Beispiel #7
0
 def test_a_offer(self):
     checkout = Checkout()
     checkout.scan("A")
     checkout.scan("A")
     checkout.scan("A")
     print(checkout.receipt.get_text())
     self.assertEqual(checkout.total, 130)
Beispiel #8
0
 def test_one_of_each(self):
     checkout = Checkout()
     checkout.scan("A")
     checkout.scan("B")
     checkout.scan("C")
     checkout.scan("D")
     self.assertEqual(checkout.receipt_text(),
                      "A: 50\nB: 30\nC: 20\nD: 15\nTotal: 115")
Beispiel #9
0
 def test_add_invalid_product(self):
     rules = {
             "A": (50, 3, 130),
             "D": (15, None)
         }
     cashier = Checkout(rules)
     with self.assertRaises(InvalidProductException):
         cashier.scan('A')
         cashier.scan('B')
Beispiel #10
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())
Beispiel #11
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())
Beispiel #12
0
    def test_incremental(self):
        checkout = Checkout(RULES)

        def assert_total(expected):
            self.assertEqual(expected, checkout.total())

        assert_total(0)
        checkout.scan('A')
        assert_total(50)
        checkout.scan('B')
        assert_total(80)
        checkout.scan('A')
        assert_total(130)
        checkout.scan('A')
        assert_total(160)
        checkout.scan('B')
        assert_total(175)
Beispiel #13
0
def test_offers(self):
    checkout = Checkout()
    checkout.scan("A")
    checkout.scan("A")
    checkout.scan("B")
    checkout.scan("A")
    checkout.scan("C")
    checkout.scan("D")
    checkout.scan("B")
    self.assertEqual(checkout.total, 2)
    self.assertEqual(
        checkout.receipt_text(),
        "A: 50\nA: 50\nB: 30\nA: 50 - 20 (3 for 130)\nC: 20\nD: 15\nB: 30 - 15 (2 for 45)\nTotal: 210"
    )
Beispiel #14
0
    def test_simple(self):
        checkout = Checkout()

        checkout.scan("A")
        self.assertEqual(50, checkout.total)

        checkout.scan("B")
        self.assertEqual(80, checkout.total)

        checkout.scan("C")
        self.assertEqual(100, checkout.total)

        checkout.scan("D")
        self.assertEqual(115, checkout.total)
Beispiel #15
0
 def test_three_a(self):
     checkout = Checkout()
     checkout.scan("A")
     checkout.scan("A")
     checkout.scan("A")
     checkout.scan("A")
     checkout.scan("A")
     checkout.scan("A")
     self.assertEqual(260, checkout.total)
Beispiel #16
0
 def test_one_b(self):
     checkout = Checkout()
     checkout.scan("B")
     self.assertEqual(30, checkout.total)
Beispiel #17
0
 def test_two_a(self):
     checkout = Checkout()
     checkout.scan("A")
     checkout.scan("A")
     self.assertEqual(100, checkout.total)
    def test_incremental(self):
        checkout = Checkout()

        checkout.scan("A")
        self.assertEqual(50, checkout.total)

        checkout.scan("B")
        self.assertEqual(80, checkout.total)

        checkout.scan("A")
        self.assertEqual(130, checkout.total)

        checkout.scan("A")
        self.assertEqual(160, checkout.total)

        checkout.scan("B")
        self.assertEqual(175, checkout.total)

        checkout.scan("C")
        self.assertEqual(195, checkout.total)

        checkout.scan("B")
        self.assertEqual(225, checkout.total)

        checkout.scan("C")
        self.assertEqual(245, checkout.total)

        checkout.scan("D")
        self.assertEqual(260, checkout.total)

        checkout.scan("D")
        self.assertEqual(275, checkout.total)

        checkout.scan("D")
        self.assertEqual(290, checkout.total)

        checkout.scan("C")
        self.assertEqual(310, checkout.total)
Beispiel #19
0
 def test_calculate_checkout(self):
     rules = {
             "A": (50, 3, 130),
             "B": (30, 2, 45),
             "C": (20, None),
             "D": (15, None)
         }
     cashier = Checkout(rules)
     try:
         cashier.scan('A')
         cashier.scan('D')
         cashier.scan('A')
         cashier.scan('a')
         cashier.scan('B')
         cashier.scan('b')
         cashier.scan('d')
         cashier.scan('A')
         cashier.scan('b')
         cashier.scan('C')
         cashier.scan('A')
         cashier.scan('C')
         # expected total = 375
         self.assertEqual(cashier.calculate_checkout(), 375)
     except InvalidProductException as e:
         print(e.message)
         self.fail("No exception should have been raised here")
Beispiel #20
0
 def test_one_a(self):
     checkout = Checkout()
     checkout.scan("A")
     self.assertEqual(50, checkout.total)
Beispiel #21
0
 def test_raises_error_on_exception(self):
     """**Scenario** User scans product which doesn't exist in the system"""
     checkout = Checkout()
     with self.assertRaises(ProductDoesntExist):
         checkout.scan('long-sku')
Beispiel #22
0
 def test_single_item(self):
     checkout = Checkout()
     checkout.scan("A")
     print(checkout.receipt_text())
     self.assertIn("A: 50\nTotal: 50", checkout.receipt_text())
 def price(self, goods):
     co = Checkout(RULES)
     for item in list(goods):
         co.scan(item)
     return co.total
Beispiel #24
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)
Beispiel #25
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()
Beispiel #26
0
def checkout_total(goods):
    checkout = Checkout(RULES)
    for item in goods:
        checkout.scan(item)
    return checkout.total()
Beispiel #27
0
 def create_checkout(self, sku_list):
     """Helper method to create a checkout populated with skus"""
     checkout = Checkout(self.price_rules)
     for sku in sku_list:
         checkout.scan(sku)
     return checkout
Beispiel #28
0
 def test_scan_works(self):
     """**Scenario** User scans a product"""
     checkout = Checkout()
     product = checkout.scan('vga')
     self.assertIsNotNone(product)
     self.assertEqual(len(checkout.items), 1)
Beispiel #29
0
 def test_two_b(self):
     checkout = Checkout()
     checkout.scan("B")
     checkout.scan("B")
     self.assertEqual(45, checkout.total)
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()
Beispiel #31
0
 def test_two_fruit_teas(self):
     co = Checkout(PricingRules().list())
     co.scan("FR1")
     co.scan("FR1")
     assert co.calculate() == 3.11
Beispiel #32
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())
Beispiel #33
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()
Beispiel #34
0
 def test_scan_raises_exception_for_not_in_catalog(self):
     """**Scenario** User scans a sku which doesn't exist in the catalog"""
     checkout = Checkout()
     product = checkout.scan('vga')
     self.assertIsNotNone(product)
     self.assertEqual(len(checkout.items), 1)