From 8adf0681d0373f298512e6c1e6de6a171433a35a Mon Sep 17 00:00:00 2001 From: WYB929 Date: Tue, 21 Nov 2023 22:31:28 +0000 Subject: [PATCH] routes and test cases for activate and deactivate --- service/routes.py | 37 +++++++++++++++++++++++++++++++++++++ tests/test_routes.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/service/routes.py b/service/routes.py index f61a280..ad94312 100644 --- a/service/routes.py +++ b/service/routes.py @@ -206,6 +206,43 @@ def like_recommendation(recommendation_id): return jsonify(recommendation.serialize()), status.HTTP_200_OK +###################################################################### +# Activate and Deactivate a RECOMMENDATION +###################################################################### +@app.route("/recommendations//deactivation", methods=["PUT"]) +def deactivate_recommendation(recommendation_id): + """ + Deactivate a Recommendation + """ + app.logger.info( + "Request to deactivate recommendation with id: %s", recommendation_id + ) + recommendation = Recommendation.find(recommendation_id) + if not recommendation: + abort(status.HTTP_404_NOT_FOUND, "recommendation not found") + recommendation.deactivate() + + return jsonify(recommendation.serialize()), status.HTTP_200_OK + + +@app.route("/recommendations//activation", methods=["PUT"]) +def activate_recommendation(recommendation_id): + """ + Activate a Recommendation + """ + app.logger.info("Request to activate recommendation with id: %s", recommendation_id) + activated_status = request.args.get("status", default=None) + valid_status = ["VALID", "OUT_OF_STOCK"] + recommendation = Recommendation.find(recommendation_id) + if not recommendation: + abort(status.HTTP_404_NOT_FOUND, "recommendation not found") + if activated_status not in valid_status: + abort(status.HTTP_400_BAD_REQUEST, "status for activation is required") + recommendation.activate(activated_status) + + return jsonify(recommendation.serialize()), status.HTTP_200_OK + + ###################################################################### # PURCHASE A PET ###################################################################### diff --git a/tests/test_routes.py b/tests/test_routes.py index efeb5a8..afeab20 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -279,6 +279,35 @@ def test_like_recommendation(self): logging.debug("Response data: %s", data) self.assertEqual(data["number_of_likes"], 1) + def test_deactivate_recommendation(self): + """It should deactivate a Recommendation""" + recommendations = self._create_recommendations(1) + rec = recommendations[0] + response = self.client.put(f"{BASE_URL}/{rec.id}/deactivation") + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.get_json() + logging.debug("Response data: %s", data) + self.assertEqual(data["status"], "DEPRECATED") + + def test_activate_recommendation(self): + """It should deactivate a Recommendation""" + recommendations = self._create_recommendations(2) + rec = recommendations[0] + response = self.client.put(f"{BASE_URL}/{rec.id}/activation?status=VALID") + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.get_json() + logging.debug("Response data: %s", data) + self.assertEqual(data["status"], "VALID") + + rec = recommendations[1] + response = self.client.put( + f"{BASE_URL}/{rec.id}/activation?status=OUT_OF_STOCK" + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + data = response.get_json() + logging.debug("Response data: %s", data) + self.assertEqual(data["status"], "OUT_OF_STOCK") + def test_read_recommendations_by_type(self): """It should get a list recommendations by its recommendation type""" recommendations = self._create_recommendations(10) @@ -373,6 +402,17 @@ def test_like_recommendation_bad_id(self): logging.debug("Response data: %s", data) self.assertEqual(data["number_of_likes"], 0) + def test_activate_recommendation_bad_status(self): + """It should return 400 when sending PUT to /recommendations/rec_id/activation without valid query of status""" + recommendations = self._create_recommendations(2) + rec = recommendations[0] + response = self.client.put(f"{BASE_URL}/{rec.id}/activation") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + rec = recommendations[1] + response = self.client.put(f"{BASE_URL}/{rec.id}/activation?status=null") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + ###################################################################### # T E S T A C T I O N S ######################################################################