예제 #1
0
    def test_adding_a_product_that_already_exists(self):
        """ Test to add a product that exists in a cart """
        shopcart = Shopcart(7, [{"pid": 1, "quantity": 5}])
        shopcart.save()

        shopcart.add_product(1, 5)

        self.assertEqual(shopcart.products[0].quantity, 10)
예제 #2
0
    def test_delete_a_shopcart(self):
        """ Test A Shopcart Can Be Deleted """
        cart = Shopcart(1, 1)
        cart.save()
        self.assertEqual(len(Shopcart.all()), 1)

        # Delete the shopcart and make sure it isn't in the database
        cart.delete()
        self.assertEqual(len(Shopcart.all()), 0)
예제 #3
0
 def test_delete_products_from_shopcart(self):
     """ Test a product in a shopcart can be deleted """
     cart = Shopcart(1, [{
         "pid": 1,
         "quantity": 2
     }, {
         "pid": 5,
         "quantity": 7
     }])
     cart.save()
     cart.delete_product(5)
     self.assertEqual(len(cart.products), 1)
예제 #4
0
    def test_it_can_be_saved(self):
        """ Test Model is Saved to database """
        # Check that there are no shopcarts
        items = Shopcart.all()
        self.assertEqual(len(items), 0)

        # Save a shopcart and check it was added to memory
        cart = Shopcart(1)
        cart.save()
        items = Shopcart.all()
        self.assertEqual(len(items), 1)

        # Check that the saved item is actually the one we saved
        fetched = items[0]
        self.assertEqual(fetched.user_id, 1)
        self.assertEqual(fetched.products, [])
예제 #5
0
    def test_add_multiple_products(self):
        """ Add multiple products to an existing cart """
        cart = Shopcart(1, [{"pid": 1, "quantity": 1}])
        cart.save()

        cart.add_products([{
            "pid": 1,
            "quantity": 2
        }, {
            "pid": 2,
            "quantity": 4
        }])

        self.assertEqual(len(cart.products), 2)
        self.assertEqual(cart.products[0].quantity, 3)
        self.assertEqual(cart.products[1].quantity, 4)
예제 #6
0
    def test_adding_an_invalid_product(self):
        """ Test to add invalid product"""
        shopcart = Shopcart(21)
        shopcart.save()
        shopcarts = shopcart.all()

        s = shopcarts[0]
        self.assertEqual(s.user_id, 21)
        self.assertEqual(len(s.products), 0)

        # Adding product 21.5
        with self.assertRaises(DataValidationError):
            s.add_product([{"pid": 21.5}])

        # Adding a second error
        with self.assertRaises(DataValidationError):
            s.add_product([{"pid": 1, "quantity": 0.5}])
예제 #7
0
    def test_initializing_with_products(self):
        """Testing initializing a cart with products """
        # Create a new shopcart without a product
        shopcart = Shopcart(0)
        self.assertTrue(len(shopcart.products) == 0)

        # Create a new shopcart with a product
        shopcart = Shopcart(0, 5)
        self.assertEqual(len(shopcart.products), 1)
        self.assertEqual(shopcart.products[0].quantity, 1)

        # Creating a valid dictionary
        shopcart = Shopcart(0, [{
            "pid": 1,
            "quantity": 7
        }, {
            "pid": 2,
            "quantity": 21
        }, {
            "pid": 3,
            "quantity": 55
        }])
        self.assertEqual(len(shopcart.products), 3)
        self.assertEqual(shopcart.products[0].quantity, 7)
        self.assertEqual(shopcart.products[1].quantity, 21)
        self.assertEqual(shopcart.products[2].quantity, 55)

        # Saving products
        shopcart.save()
        shopcarts = Shopcart.all()
        self.assertEqual(len(shopcarts), 1)

        #C orrect Type
        s = shopcarts[0]
        self.assertEqual(type(s.products), InstrumentedList)

        # Correct Length
        self.assertEqual(len(s.products), 3)
예제 #8
0
    def test_adding_with_a_product(self):
        """ Test to add a product to an exiting shopcart"""
        shopcart = Shopcart(7)
        shopcart.save()
        shopcarts = shopcart.all()

        s = shopcarts[0]
        self.assertEqual(s.user_id, 7)
        self.assertEqual(len(s.products), 0)

        # Adding product 1 with quant 34
        s.add_product(1, 34)
        #There's only one product
        self.assertEqual(len(s.products), 1)
        # It's the correct one with correct quant
        self.assertEqual(s.products[0].quantity, 34)

        # Adding a second
        s.add_product(2, 55)
        # #There's two  products
        self.assertEqual(len(s.products), 2)
        # # It's the correct one with correct quant
        self.assertEqual(s.products[1].quantity, 55)
예제 #9
0
    def test_get_all_shopcarts(self):
        """ Test All Shopcarts Can Be Retrieved """
        # Add 3 shopcarts to memory and check that we can retrieve them all
        cart1 = Shopcart(1)
        cart2 = Shopcart(2)
        cart3 = Shopcart(3)
        cart1.save()
        cart2.save()
        cart3.save()

        # Invoke method and check the returned data
        shopcarts = Shopcart.all()
        self.assertEqual(len(shopcarts), 3)
        self.assertEqual(shopcarts[0].user_id, 1)
        self.assertEqual(shopcarts[1].user_id, 2)
        self.assertEqual(shopcarts[2].user_id, 3)