def test_delete_a_product(self): """ Delete a Product """ product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) product.save() self.assertEqual(len(Product.all()), 1) # delete the product and make sure it isn't in the database product.delete() self.assertEqual(len(Product.all()), 0)
def test_add_a_product(self): """ Create a product and add it to the database """ products = Product.all() self.assertEqual(products, []) product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) self.assertTrue(product is not None) self.assertEqual(product.id, 1) product.save() # Assert that it was assigned an id and shows up in the database self.assertEqual(product.id, 1) products = Product.all() self.assertEqual(len(products), 1)
def get(self): """ Return all the products""" name = request.args.get('name') category = request.args.get('category') if name: products = Product.find_by_name(name) elif category: products = Product.find_by_category(category) else: products = Product.all() results = [product.serialize() for product in products] return results, status.HTTP_200_OK
def test_update_a_product_rating(self): """ Update a Product Rating""" product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) product.save() self.assertEqual(product.id, 1) # Change it and save it product.rating = 10 product.update() self.assertEqual(product.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change products = Product.all() self.assertEqual(len(products), 1) self.assertEqual(products[0].rating, 10)
def list_products(): """ Return all the products""" products = [] name = request.args.get('name') app.logger.info(name) category = request.args.get('category') id = request.args.get("id") if name: products = Product.find_by_name(name) elif category: products = Product.find_by_category(category) elif id: products = Product.find_by_id(id) else: products = Product.all() results = [product.serialize() for product in products] return make_response(jsonify(results), status.HTTP_200_OK)