Esempio n. 1
0
 def test_find_by_price(self):
     """ Find Products by Price """
     Product(name="Wagyu Tenderloin Steak",
             category="food",
             stock=11,
             price=26.8,
             description="The most decadent, succulent cut of beef, ever."
             ).save()
     Product(name="shampos", category="Health Care", stock=48,
             price=12.34).save()
     products = Product.find_by_price(25, 50)
     self.assertEqual(products[0].category, "food")
     self.assertEqual(products[0].name, "Wagyu Tenderloin Steak")
     self.assertEqual(products[0].stock, 11)
Esempio n. 2
0
 def test_find_by_name(self):
     """ Find a Product by Name """
     Product(name="Wagyu Tenderloin Steak",
             category="food",
             stock=11,
             price=20.56,
             description="The most decadent, succulent cut of beef, ever."
             ).save()
     Product(name="shampos", category="Health Care", stock=48,
             price=12.34).save()
     products = Product.find_by_name("shampos")
     self.assertEqual(products[0].category, "Health Care")
     self.assertEqual(products[0].name, "shampos")
     print(products[0].price)
     print(getcontext())
     self.assertAlmostEqual(products[0].price, Decimal(12.34))
Esempio n. 3
0
 def delete(self, product_id):
     """Delete a Product by id"""
     app.logger.info('Request to delete product with the id [%s] provided',
                     product_id)
     product = Product.find(product_id)
     if product:
         product.delete()
     return '', status.HTTP_204_NO_CONTENT
Esempio n. 4
0
 def test_deserialize_a_product(self):
     """ Test deserialization of a Product """
     data = {
         "id": 1,
         "name": "shampos",
         "category": "Health Care",
         "stock": 48,
         "price": 12.34,
         "description": "Test"
     }
     product = Product()
     product.deserialize(data)
     self.assertNotEqual(product, None)
     self.assertEqual(product.id, None)
     self.assertEqual(product.name, "shampos")
     self.assertEqual(product.category, "Health Care")
     self.assertEqual(product.stock, 48)
     self.assertAlmostEqual(product.price, Decimal(12.34))
     self.assertEqual(product.description, "Test")
Esempio n. 5
0
 def get(self, product_id):
     """
     Retrieve a single Product 
     This endpoint will return a Product based on it's id
     """
     app.logger.info('Request for product with id: %s', product_id)
     product = Product.find(product_id)
     if not product:
         api.abort(status.HTTP_404_NOT_FOUND,
                   "Product with id '{}' was not found.".format(product_id))
     return product.serialize(), status.HTTP_200_OK
Esempio n. 6
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)
Esempio n. 7
0
 def put(self, product_id):
     app.logger.info('Request to update product with id: %s', product_id)
     check_content_type('application/json')
     product = Product.find(product_id)
     if not product:
         api.abort(status.HTTP_404_NOT_FOUND,
                   "Product with id {} was not found.".format(product_id))
     app.logger.debug('Payload = %s', api.payload)
     data = api.payload
     product.deserialize(data)
     product.id = product_id
     product.save()
     return product.serialize(), status.HTTP_200_OK
Esempio n. 8
0
 def test_create_a_product(self):
     """ Create a product and assert that it exists """
     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)
Esempio n. 9
0
 def test_serialize_a_product(self):
     """ Test serialization of a Product """
     product = Product(
         name="Wagyu Tenderloin Steak",
         category="food",
         stock=11,
         price=20.56,
         description="The most decadent, succulent cut of beef, ever.")
     data = product.serialize()
     self.assertNotEqual(data, None)
     self.assertIn('id', data)
     self.assertEqual(data['id'], None)
     self.assertIn('name', data)
     self.assertEqual(data['name'], "Wagyu Tenderloin Steak")
     self.assertIn('category', data)
     self.assertEqual(data['category'], "food")
     self.assertIn('stock', data)
     self.assertEqual(data['stock'], 11)
     self.assertIn('price', data)
     self.assertAlmostEqual(data['price'], 20.56)
     self.assertIn('description', data)
     self.assertEqual(data['description'],
                      "The most decadent, succulent cut of beef, ever.")
Esempio n. 10
0
 def put(self, product_id):
     """Buy a Product by id"""
     app.logger.info('Request for buy a product')
     product = Product.find(product_id)
     if not product:
         api.abort(status.HTTP_404_NOT_FOUND,
                   "Product with id '{}' was not found.".format(product_id))
     elif product.stock == 0:
         api.abort(
             status.HTTP_409_CONFLICT,
             "Product with id '{}' has been sold out!".format(product_id))
     else:
         product.stock = product.stock - 1
     product.save()
     app.logger.info('Product with id [%s] has been bought!', product.id)
     return product.serialize(), status.HTTP_200_OK
Esempio n. 11
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")
Esempio n. 12
0
 def post(self):
     """
     Creates a Product
     This endpoint will create a Product based the data in the body that is posted
     """
     app.logger.info('Request to create a product')
     check_content_type('application/json')
     product = Product()
     app.logger.debug('Payload = %s', api.payload)
     product.deserialize(api.payload)
     product.save()
     location_url = api.url_for(ProductResource,
                                product_id=product.id,
                                _external=True)
     return product.serialize(), status.HTTP_201_CREATED, {
         'Location': location_url
     }
Esempio n. 13
0
 def test_find_product(self):
     """ Find a Product by ID """
     Product(name="Wagyu Tenderloin Steak",
             category="food",
             stock=11,
             price=20.56,
             description="The most decadent, succulent cut of beef, ever."
             ).save()
     shampo = Product(name="shampos",
                      category="Health Care",
                      stock=48,
                      price=12.34)
     shampo.save()
     product = Product.find(shampo.id)
     self.assertIsNot(product, None)
     self.assertEqual(product.id, shampo.id)
     self.assertEqual(product.name, "shampos")
     self.assertEqual(product.category, "Health Care")
Esempio n. 14
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)
Esempio n. 15
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
Esempio n. 16
0
 def setUp(self):
     Product.init_db(app)
     db.drop_all()  # clean up the last tests
     db.create_all()  # make our sqlalchemy tables
Esempio n. 17
0
 def test_deserialize_a_miss_product(self):
     """ Test deserialization of product missing agrs """
     data = {"id": 1, "name": "shampos", "category": "Health Care"}
     product = Product()
     self.assertRaises(DataValidationError, product.deserialize, data)
Esempio n. 18
0
 def test_deserialize_bad_data(self):
     """ Test deserialization of bad data """
     data = "this is not a dictionary"
     product = Product()
     self.assertRaises(DataValidationError, product.deserialize, data)
Esempio n. 19
0
def init_db():
    """ Initialies the SQLAlchemy app """
    global app
    Product.init_db(app)
Esempio n. 20
0
def delete_products_all():
    """Delete all Products"""
    app.logger.info('Request to delete all products')
    Product.delete_all()
    return make_response('', status.HTTP_204_NO_CONTENT)