def test_update_product_review(self): """ Update an existing Product Review """ product = Product.find_by_name('Athens Table')[0] resp = self.app.put('/products/review', query_string='id=1&newrev=Average', content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['review'], 'Average') product = Product.find_by_name('Athens Table')[0] resp = self.app.put('/products/review', query_string='id=1&newrev=Awesome', content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['review'], 'Average|Awesome')
def test_get_product(self): """ Get a single Product """ # get the id of a product product = Product.find_by_name('Athens Table')[0] resp = self.app.get('/products/{}'.format(product.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = json.loads(resp.data) self.assertEqual(data['name'], product.name)
def test_delete_product(self): """ Delete a Product """ product = Product.find_by_name('Athens Table')[0] # save the current number of products for later comparison product_count = self.get_product_count() resp = self.app.delete('/products/{}'.format(product.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) new_count = self.get_product_count() self.assertEqual(new_count, product_count - 1)
def test_update_product_rating(self): """ Update an existing Product Rating """ product = Product.find_by_name('Athens Table')[0] # new_product = dict(id=1,name='Athens Table', description='Stupid Table', category="Fancy Table",price=20, condition="Boxed", inventory=2, review="", rating=8) # data = json.dumps(new_product) resp = self.app.put('/products/rating', query_string='id=1&stars=10', content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['rating'], 7)
def test_update_product(self): """ Update an existing Product """ product = Product.find_by_name('Athens Table')[0] new_product = dict(id=1,name='Athens Table', description='Stupid Table', category="Fancy Table",price=20, condition="Boxed", inventory=2, review="", rating=8) data = json.dumps(new_product) resp = self.app.put('/products/{}'.format(product.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['category'], 'Fancy Table')
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_find_by_name(self): """ Find a Product by Name """ Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8).save() Product(2, "Table", "Oak table", "Furniture", 150, "Boxed", 100, " ", 7).save() products = Product.find_by_name("Couch") self.assertEqual(products[0].id, 1) self.assertEqual(products[0].category, "Furniture") self.assertEqual(products[0].name, "Couch") self.assertEqual(products[0].description, "White couch") self.assertEqual(products[0].price, 200) self.assertEqual(products[0].condition, "Boxed") self.assertEqual(products[0].inventory, 50) self.assertEqual(products[0].rating, 8)
def test_update_product_bad_request(self): """ Update an existing Product with bad data""" product = Product.find_by_name('Athens Table')[0] new_product = dict(id=1, name='Athens Table', category="Fancy Table", price=20, condition="Boxed", inventory=2, review="So so", rating=8) data = json.dumps(new_product) resp = self.app.put('/products/{}'.format(product.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
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)