def get_promotions(promotion_id): """ Retrieves a single Promotion This endpoint will return a Promotion based on it's id --- tags: - promotions parameters: - name: promotion_id in: path description: Numeric ID of the promotion to get required: true type: integer responses: 200: description: promotion retrieved schema: $ref: '#/definitions/ResponsePromotionObject' 400: description: invalid input, object invalid 404: description: promotion not found, object invalid """ promotion = Promotion.find(promotion_id) if not promotion: raise NotFound( "Promotion with id '{}' was not found.".format(promotion_id)) return make_response(jsonify(promotion.serialize()), status.HTTP_200_OK)
def test_find_promotion(self): """ Find a Promotion by id """ Promotion("A1234", "BOGO", True, "20").save() saved_promotion = Promotion("B4321", "dollar", False, "5") saved_promotion.save() promotion = Promotion.find(saved_promotion.id) self.assertIsNot(promotion, None) self.assertEqual(promotion.id, saved_promotion.id) self.assertEqual(promotion.productid, "B4321")
def test_redeem_promotion(self): """ Redeem a Promoion """ Promotion(name="20%OFF", product_id=9527, discount_ratio=80).save() promotion = Promotion.find(1) self.assertEqual(promotion.counter, 0) Promotion.redeem_promotion(1) self.assertEqual(promotion.counter, 1) Promotion.redeem_promotion(1) self.assertEqual(promotion.counter, 2)
def cancel_promotions(promotion_id): """ Purchasing a Promotion makes it unavailable """ promotion = Promotion.find(promotion_id) if not promotion: abort(status.HTTP_404_NOT_FOUND, "Promotion with id '{}' was not found.".format(promotion_id)) promotion.available = False promotion.save() return make_response(jsonify(promotion.serialize()), status.HTTP_200_OK)
def delete_promotions(promotion_id): """ Delete a Promotion This endpoint will delete a Promotion based the id specified in the path """ promotion = Promotion.find(promotion_id) if promotion: promotion.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def test_find_promotion(self): """ Find a Promotion by ID """ Promotion(name="20%OFF", product_id=9527, discount_ratio=80).save() black_friday_promotion = Promotion(name="50%OFF", product_id=26668) black_friday_promotion.save() promotion = Promotion.find(black_friday_promotion.promotion_id) self.assertIsNot(promotion, None) self.assertEqual(promotion.promotion_id, black_friday_promotion.promotion_id) self.assertEqual(promotion.name, "50%OFF")
def get_promotions(promotion_id): """ Retrieve a single Promotion This endpoint will return a Promotion based on it's id """ promotion = Promotion.find(promotion_id) if not promotion: raise NotFound( "Promotion with id '{}' was not found.".format(promotion_id)) return make_response(jsonify(promotion.serialize()), status.HTTP_200_OK)
def delete_promotions(promotion_id): """ Delete a Promotion This endpoint will delete a Promotion based the id specified in the path """ app.logger.info('Request to Delete a promotion with id [%s]', promotion_id) promotion = Promotion.find(promotion_id) if promotion: promotion.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def update_promotions(promotion_id): """ Update a Promotion This endpoint will update a Promotion based the body that is posted """ check_content_type('application/json') promotion = Promotion.find(promotion_id) if not promotion: raise NotFound( "Promotion with id '{}' was not found.".format(promotion_id)) promotion.deserialize_partial(request.get_json()) promotion.id = promotion_id promotion.save() return make_response(jsonify(promotion.serialize()), status.HTTP_200_OK)
def update_promotions(promotion_id): """ Updates a Promotion It isn't necessary to fill in all three fields. Only the non-empty fields will be updated. The empty fields will be left as it is. --- tags: - promotions parameters: - name: promotion_id in: path description: Numeric ID of the promotion to get required: true type: integer - in: body name: Promotion description: Promotion entry to update required: false schema: $ref: '#/definitions/Promotion' responses: 200: description: promotion retrieved schema: $ref: '#/definitions/ResponsePromotionObject' 400: description: invalid input, object invalid 404: description: promotion not found, object invalid """ check_content_type('application/json') promotion = Promotion.find(promotion_id) if not promotion: raise NotFound( "Promotion with id '{}' was not found.".format(promotion_id)) promotion.deserialize_partial(request.get_json()) promotion.id = promotion_id promotion.save() return make_response(jsonify(promotion.serialize()), status.HTTP_200_OK)
def delete_promotions(promotion_id): """ Deletes a Promotion This endpoint will delete a Promotion based the id specified in the path --- tags: - promotions parameters: - name: promotion_id in: path description: Numeric ID of the promotion to get required: true type: integer responses: 204: description: promotion deleted """ promotion = Promotion.find(promotion_id) if promotion: promotion.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def test_find_promotion(self): """ Find a Promotion by ID """ Promotion(promo_name="random", goods_name="random_good", category="random_category", price=20, discount=20, available=True).save() random2 = Promotion(promo_name="random2", goods_name="random2_good", category="random2_category", price=2, discount=2, available=False) random2.save() promotion = Promotion.find(random2.id) self.assertIsNot(promotion, None) self.assertEqual(promotion.id, random2.id) self.assertEqual(promotion.promo_name, "random2") self.assertEqual(promotion.goods_name, "random2_good") self.assertEqual(promotion.category, "random2_category") self.assertEqual(promotion.price, 2) self.assertEqual(promotion.discount, 2) self.assertEqual(promotion.available, False)
def test_promotion_not_found(self): """ Find a Promotion that doesnt exist """ Promotion("A1234", "BOGO", True, "20").save() promotion = Promotion.find("2") self.assertIs(promotion, None)
def test_find_with_no_promotions(self): """ Find a Promotion with empty database """ promotion = Promotion.find("1") self.assertIs(promotion, None)
def test_find_promotion_input_nonexistent_id(self): """ Test find promotion function with nonexistent ID """ Promotion(name="20%OFF", product_id=9527, discount_ratio=80).save() promotion = Promotion.find(2) self.assertEqual(promotion, None)