def on_put(self, req, resp, productId): try: productId = int(productId) product = bl.getProduct(productId) if product is None: return falcon.HTTP_404 doc = req.context['doc'] newProduct = Product.from_json(doc) if newProduct.productId is None: newProduct.productId = product.productId #If updated object has id which doesn't match old one, return http forbidden-status elif newProduct.productId != productId: return falcon.HTTP_403 #update product bl.setProduct(newProduct) resp.status = falcon.HTTP_200 #Status Ok except: #Return 500 - internal error resp.status = falcon.HTTP_500
def on_get(self, req, resp, productId): try: productId = int(productId) product = bl.getProduct(productId) if product is None: return falcon.HTTP_404 result = {'product':product} resp.body = json.dumps(result, sort_keys=True, default=Product.serialize) resp.status = falcon.HTTP_200 #Status Ok except: #Return 500 - internal error resp.status = falcon.HTTP_500
def test_product_serialize(): p = bl.getProduct(1) print(json.dumps(Product.serialize(p), sort_keys = True))
def test_product_remove(): p = bl.getProduct(code="POOLBL") db.removeProduct(p.productId)
def test_product_add(): print("Testing product insert..") db.updateProduct(Product(code="POOLBL",name="PoolBall",price=Decimal(9.80),in_stock=999)) t = bl.getProduct(code="POOLBL") assert(t is not None)