Test Spec Failed Your solution failed all the tests


#1

Hi, I have a task i am trying to achieve. I have tried everything i can but am still stuck:
this is it question:

this my code and the result after testing

now when i want to submit it this is what i get

please can anyone help me with this


#2

visual debugging without running the code and test cases is really difficult, can we run the code somewhere? Please provide some more details


#3

test case code is this


#4

can i get the code so i can run it? visualing debugging is really difficult

If you find someone capable of debugging this code just from the images, just let me know, i would love to meet that person


#5

ok i have modified it to check for the following
1. if the type entered is string for input
2. int, long or float for number input
3. to check if item is added before being removed but yet am still getting the same tine.

the question is in the first image i uploaded
test case code

import unittest

class ShoppingCartTestCases(unittest.TestCase):
  def setUp(self):
    self.cart = ShoppingCart()
    self.shop = Shop()
    
  def test_cart_property_initialization(self):
    self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
    self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')
    
  def test_add_item(self):
    self.cart.add_item('Mango', 3, 10)
    
    self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
    self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')
    
  def test_remove_item(self):
    self.cart.add_item('Mango', 3, 10)
    self.cart.remove_item('Mango', 2, 10)
    
    self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
    self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
    
  def test_checkout_returns_correct_balance(self):
    self.cart.add_item('Mango', 3, 10)
    self.cart.add_item('Orange', 16, 10)
    
    self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
    self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
    
  def test_shop_is_instance_of_shopping_cart(self):
    self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')

  def test_shop_remove_item_method(self):
    for i in range(15):
      self.shop.remove_item()

    self.assertEqual(self.shop.quantity, 85)


**my code** 
class ShoppingCart(object):
    def __init__(self):
        self.total = 0
        self.items = {}

    def add_item(self, item_name, quantity, price):
        if not (isinstance(item_name, str)):
            raise TypeError("item name should be string")
        elif not (isinstance(quantity, (int, long, float))):
            raise TypeError("quantity should be number")
        elif not (isinstance(price, (int, long, float))):
            raise TypeError("price should be number")
        else:
            self.total += (price * quantity)
            self.items[item_name] = quantity

    def remove_item(self, item_name, quantity, price):
        if not (isinstance(item_name, str)):
            raise TypeError("item name should be string")
        elif not (isinstance(quantity, (int, long, float))):
            raise TypeError("quantity should be number")
        elif not (isinstance(price, (int, long, float))):
            raise TypeError("price should be number")
        elif self.items == {}:
            return "list is empty. Add item before removing"
        elif not (item_name in self.items):
            return "Item not found in cart"
        elif self.items[item_name] > quantity:
            self.items[item_name] = self.items[item_name] - quantity
            self.total -= (price * quantity)
        else:
            del self.items[item_name]

    def checkout(self, cash_paid):
        if not (isinstance(cash_paid, (int, long, float))):
            raise TypeErroe("cash paid must be number")
        elif cash_paid < self.total:
            return "Cash paid not enough"
        else:
            return cash_paid - self.total


class Shop(ShoppingCart):
    def __init__(self):
        self.quantity = 100

    def remove_item(self):
        self.quantity -= 1
        return self.quantity

thanks


#6

i ran this code:

class ShoppingCart(object):
    def __init__(self):
        self.total = 0
        self.items = {}

    def add_item(self, item_name, quantity, price):
        if not (isinstance(item_name, str)):
            raise TypeError("item name should be string")
        elif not (isinstance(quantity, (int, long, float))):
            raise TypeError("quantity should be number")
        elif not (isinstance(price, (int, long, float))):
            raise TypeError("price should be number")
        else:
            self.total += (price * quantity)
            self.items[item_name] = quantity

    def remove_item(self, item_name, quantity, price):
        if not (isinstance(item_name, str)):
            raise TypeError("item name should be string")
        elif not (isinstance(quantity, (int, long, float))):
            raise TypeError("quantity should be number")
        elif not (isinstance(price, (int, long, float))):
            raise TypeError("price should be number")
        elif self.items == {}:
            return "list is empty. Add item before removing"
        elif not (item_name in self.items):
            return "Item not found in cart"
        elif self.items[item_name] > quantity:
            self.items[item_name] = self.items[item_name] - quantity
            self.total -= (price * quantity)
        else:
            del self.items[item_name]

    def checkout(self, cash_paid):
        if not (isinstance(cash_paid, (int, long, float))):
            raise TypeErroe("cash paid must be number")
        elif cash_paid < self.total:
            return "Cash paid not enough"
        else:
            return cash_paid - self.total


class Shop(ShoppingCart):
    def __init__(self):
        self.quantity = 100

    def remove_item(self):
        self.quantity -= 1
        return self.quantity

import unittest

class ShoppingCartTestCases(unittest.TestCase):
  def setUp(self):
    self.cart = ShoppingCart()
    self.shop = Shop()

  def test_cart_property_initialization(self):
    self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
    self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')

  def test_add_item(self):
    self.cart.add_item('Mango', 3, 10)

    self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
    self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')

  def test_remove_item(self):
    self.cart.add_item('Mango', 3, 10)
    self.cart.remove_item('Mango', 2, 10)

    self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
    self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')

  def test_checkout_returns_correct_balance(self):
    self.cart.add_item('Mango', 3, 10)
    self.cart.add_item('Orange', 16, 10)

    self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
    self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')

  def test_shop_is_instance_of_shopping_cart(self):
    self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')

  def test_shop_remove_item_method(self):
    for i in range(15):
      self.shop.remove_item()

    self.assertEqual(self.shop.quantity, 85)

if __name__ == '__main__':
        unittest.main()

all test cases pass, how do i help you, if what i get, can't reproduce the problem?


#7

I know if u look at the images I sent that is what I got but if I want to submit it after testing I will get an error message. Failed to pass some hidden test am confused.


#8

Please just go through the first images and read the questions if I made any mistakes in the code I have not later than 11.59pm today to submit it


#9

There is no error message at the hidden test case?


#10

I'm just wondering....did you do anything to the code between your success the first time vs your failed submission?


#11

very unlikely, when going for the final submit, the six test cases are usually run again. Without the hidden test case its failing on, debugging this for us becomes really difficult


#12

Oh...hidden tests as in tests that are not revealed in the "Test" tab and the tests given? That is hard but it reminds me of how my courses do things. We don't get to see the tests.

@ayogu.chibueze I think it's best to create extra test cases on your own that is not in those tests given.


#13

i didn't do anything to it


#14

@datfatcat, this particular hidden test you have been trying to solve is raising error due
to the fact that your code failed to remove_item_completely if quantity of item to be removed exceeds quantity of item in the entry

So I think the remove_item() should be revised
this is the error i got:

I need your help too


#15

I didn't write any code for this :stuck_out_tongue: