Sam's Surf Shop

stuck at task 6. Please help.

import unittest
import surfshop

class SystemTest(unittest.TestCase):
  def setUp(self):
    self.cart = surfshop.ShoppingCart()
  def test_add1_surfboards(self):
    message = self.cart.add_surfboards()
    self.assertEqual(message, 'Successfully added 1 surfboard to cart!')
  def test_add2_surfboards(self):
    message = self.cart.add_surfboards(2)
    self.assertEqual(message, 'Successfully added 2 surfboards to cart!')
  def test_add_surfboards(self):
    self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(5))

unittest.main()

the output seems to be alright when i have integer 5 as input
…E

ERROR: test_add_surfboards (main.SystemTest)

Traceback (most recent call last):
File “tests.py”, line 15, in test_add_surfboards
self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(5))
File “/home/ccuser/workspace/sams-surf-shop/surfshop.py”, line 17, in add_surfboards
raise TooManyBoardsError
surfshop.TooManyBoardsError


Ran 3 tests in 0.000s

FAILED (errors=1)

But when I have 3 or 2 as input, the output seems odd

def test_add2_surfboards(self):
message = self.cart.add_surfboards(2)
self.assertEqual(message, ‘Successfully added 2 surfboards to cart!’)
def test_add_surfboards(self):
self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(3))

unittest.main()
Output-only Terminal
Output:
…E

ERROR: test_add_surfboards (main.SystemTest)

Traceback (most recent call last):
File “tests.py”, line 15, in test_add_surfboards
self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards(3))
File “/usr/lib/python3.6/unittest/case.py”, line 733, in assertRaises
return context.handle(‘assertRaises’, args, kwargs)
File “/usr/lib/python3.6/unittest/case.py”, line 178, in handle
callable_obj(*args, **kwargs)
TypeError: ‘str’ object is not callable


Ran 3 tests in 0.001s

FAILED (errors=1)

1 Like

the raise of the Exception “TooManyBoardsError” pops because the setUp function runs only once when you call the subTest. What I did is to create an new instance for each of the subTest:

def test_add_more_surfboards(self):
    num_surfboards = [2, 3, 4]

    for num in num_surfboards:
      self.cart = surfshop.ShoppingCart()
      with self.subTest(num):
        self.assertEqual(self.cart.add_surfboards(num), f'Successfully added {num} surfboards to cart!', "Check adding surfboards to a cart")

if not, you will cumulate the surfboards in the instance created at the beginning of the tests.

2 Likes

implementation of the test for checkout_date function:

import surfshop, unittest
from datetime import date


today = date.today()
yesterday = today.replace(day= (today.day)-1)

class surfShop_test(unittest.TestCase):
  def setUp(self):
    self.cart = surfshop.ShoppingCart()

  def test_dateValidation(self):
    self.assertRaises(surfshop.CheckoutDateError, self.cart.set_checkout_date, yesterday) 
  
    

In surfshop.py:

    def set_checkout_date(self, date):
        if date <= datetime.date.today():
            raise CheckoutDateError
        else:
            self.checkout_date = date

Put the argument for the ‘self.cart.add_surfboards’ function as the last argument for ‘self.assertRaises()’.
like this:

def test_add_surfboards(self):
    self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, 5)
2 Likes

The solutions does not even include step 6!!! Can we have some guidance or explanation for the solutions??? A video explanation would be really appreciated! Or at least a step by step explanation!

7 Likes

Hey, thank you for confirming my expectations. I was trying to figure what was happening that I was getting an error on this checkpoint, and I had arrived to the same conclusion.

Agreed. A video would be great.

I am trying to edit the error that popped up when working on #8. Pl

[codebyte] [/codebyte] import surfshop import unittest class testing_function(unittest.TestCase): def setUp(self): self.surfshop.ShoppingCart = cart def test_add1_surfboards(self): message = self.cart.add.surfboards(1) self.assertEqual(message, 'Successfully added 1 surfboard to cart!') def test_add2_surfboards(self): message = self.cart.add.surfboards(2) self.assertEqual(message, 'Successfully added 2 surfboards to cart!') def test_cart_limits(self): self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, 5) @unittest.expectedFailure def test_local_discounts(self): self.local_discounts self.assertTrue(cart.apply_locals_discount, cart.locals_discount()) unittest.main()

ease assist!

I had the setup function written the other way:

  def setUp(self):
    self.cart = surfshop.ShoppingCart()
import surfshop, unittest class system_tests(unittest.TestCase): def setUp(self): self.cart = surfshop.ShoppingCart() def test_add1_surfboards(self): num_of_boards = self.cart.add.surfboards(1) self.assertEqual(num_of_boards, 'Successfully added 1 surfboard to cart!') def test_add2_surfboards(self): num_of_boards = self.cart.add.surfboards(2) self.assertEqual(num_of_boards, 'Successfully added 2 surfboards to cart!') def test_cart_limits(self): self.assertRaises(surfshop.TooManyBoardsError, self.cart.add_surfboards, 5) @unittest.expectedFailure def test_local_discounts(self): self.local_discounts self.assertTrue(ShoppingCart.apply_locals_discount, ShoppingCart.locals_discount()) unittest.main()

I am having issues with the errors that are not supposed to show up in #8. Please advise. Thank you

1 Like

figured it out! Thanks people!

This is the lessons expected solution. I needed this similar to OP. tyvm

EDIT:
Having now finished this lesson i wanted to share my (working) result in a format and syntax I believe is faithful to the lesson
I also opted to add ‘# Assignment ?’ comments to identify where I did what for which assignment
Hope this can help someone.

tests.py:

# Write your code below: # Assignment 1 import unittest import surfshop # Assignment 13 from datetime import datetime from datetime import timedelta today = datetime.today() yesterday = today - timedelta(days = 1) # Assignment 2 class surfshopTestSuite(unittest.TestCase): # Assignment 3 def setUp(self): self.cart = surfshop.ShoppingCart() # Assignment 10 """ # Assignment 4 def test_add_1_surfboards(self): self.assertEqual(self.cart.add_surfboards(1),'Successfully added 1 surfboard to cart!') # Assignment 5 def test_add_2_surfboards(self): self.assertEqual(self.cart.add_surfboards(2),'Successfully added 2 surfboards to cart!') """ # Assignment 10 def test_add_multiple_surfboards(self): # Asignment 12 - add 5 to array and test for i in [2,3,4]: with self.subTest(): message = self.cart.add_surfboards(i) self.assertEqual(message, f'Successfully added {i} surfboards to cart!') self.cart = surfshop.ShoppingCart() # Assignment 9 @unittest.skip('off season') # Assignment 6 def test_add_too_many_surfboards(self): self.assertRaises(surfshop.TooManyBoardsError,self.cart.add_surfboards,5) # Assignment 7 # Assigment 11 # @unittest.expectedFailure def test_apply_local_discounts(self): discount_applied = self.cart.apply_locals_discount() self.assertTrue(discount_applied) # Assignment 13 def test_set_checkout_date(self): self.assertRaises(surfshop.CheckoutDateError, self.cart.set_checkout_date, yesterday) # Assignment 8 unittest.main()

surfshop.py:

import datetime class TooManyBoardsError(Exception): # Assigment 12 def __str__(self): return 'Cart cannot have more than 4 surfboards in it!' 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): # Assignment 11 return True