Esempio n. 1
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)
Esempio n. 2
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, [])
Esempio n. 3
0
    def test_get_shopcarts_with_a_specific_product(self):
        Shopcart(1, [{
            "pid": 1,
            "quantity": 7
        }, {
            "pid": 2,
            "quantity": 5
        }]).save()
        Shopcart(2, [{"pid": 3, "quantity": 1}]).save()
        Shopcart(3, [{
            "pid": 4,
            "quantity": 1
        }, {
            "pid": 5,
            "quantity": 4
        }, {
            "pid": 1,
            "quantity": 3
        }]).save()

        self.assertEqual(len(Shopcart.all()), 3)

        self.assertEqual(len(Shopcart.find_by_product(1)), 2)
        self.assertEqual(len(Shopcart.find_by_product(5)), 1)
        self.assertEqual(len(Shopcart.find_by_product(6)), 0)
Esempio n. 4
0
    def test_shopcarts_are_pruned(self):
        """ Test empty shopcarts are pruned """
        Shopcart(1).save()
        Shopcart(2).save()
        Shopcart(3, [{"pid": 5, "quantity": 7}]).save()

        Shopcart.prune()

        self.assertEqual(len(Shopcart.all()), 1)
Esempio n. 5
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)
Esempio n. 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}])
Esempio n. 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)
Esempio n. 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)