-
-
Save codecademydev/4404a1e91d99d73605098d78f3509141 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
class TooManyBoardsError(Exception): | |
pass | |
class CheckoutDateError(Exception): | |
pass | |
class ShoppingCart: | |
def __init__(self): | |
self.num_surfboards = 0 | |
self.checkout_date = None | |
self.locals_discount = False | |
def add_surfboards(self, quantity=1): | |
if self.num_surfboards + quantity > 4: | |
raise TooManyBoardsError | |
else: | |
self.num_surfboards += quantity | |
suffix = '' if quantity == 1 else 's' | |
return f'Successfully added {quantity} surfboard{suffix} to cart!' | |
def set_checkout_date(self, date): | |
if date <= datetime.datetime.now(): | |
raise CheckoutDateError | |
else: | |
self.checkout_date = date | |
def apply_locals_discount(self): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write your code below: | |
import surfshop | |
import unittest | |
class Tests(unittest.TestCase): | |
def setUp(self): | |
self.Cart = surfshop.ShoppingCart() | |
def test_add_surfboards(self): | |
for num in [2, 3, 4]: | |
with self.subTest(num): | |
s = '' | |
if num > 1: | |
s = 's' | |
else: | |
s = '' | |
self.assertEqual(self.Cart.add_surfboards(num), f'Successfully added {num} surfboard{s} to cart!') | |
@unittest.skip | |
def test_too_many_boards(self): | |
self.assertRaises(surfshop.TooManyBoardsError, self.Cart.add_surfboards, 5) | |
@unittest.expectedFailure | |
def test_apply_discount(self): | |
self.assertTrue(self.Cart.apply_locals_discount()) | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment