Exemplo n.º 1
0
 def test_delete_a_product(self):
     """ Delete a Product """
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     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)
Exemplo n.º 2
0
 def test_add_a_product(self):
     """ Create a product and add it to the database """
     products = Product.all()
     self.assertEqual(products, [])
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     self.assertTrue(product != None)
     self.assertEqual(product.id, None)
     self.assertEqual(product.name, "Wagyu Tenderloin Steak")
     self.assertEqual(product.category, "food")
     self.assertAlmostEqual(product.price, Decimal(20.56))
     self.assertEqual(product.stock, 11)
     product.save()
     # Asert 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)
Exemplo n.º 3
0
 def test_update_a_product(self):
     """ Update a Product """
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     product.save()
     self.assertEqual(product.id, 1)
     # Change it an save it
     product.category = "beverage"
     product.save()
     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].category, "beverage")
Exemplo n.º 4
0
 def get(self):
     """Returns all of the Products"""
     app.logger.info('Request for product list')
     products = []
     category = request.args.get('category')
     name = request.args.get('name')
     price = request.args.get('price')
     if category:
         products = Product.find_by_category(category)
     elif name:
         products = Product.find_by_name(name)
     elif price and int(price) > 0 and int(
             price) < 4:  # query price by range
         if int(price) == 1:
             products = Product.find_by_price(0, 25)
         elif int(price) == 2:
             products = Product.find_by_price(25, 50)
         else:
             products = Product.find_by_price(50, 75)
     else:
         products = Product.all()
     results = [product.serialize() for product in products]
     return results, status.HTTP_200_OK