Skip to content

Commit

Permalink
routes and test cases for activate and deactivate
Browse files Browse the repository at this point in the history
  • Loading branch information
WYB929 committed Nov 21, 2023
1 parent 5812368 commit 8adf068
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
37 changes: 37 additions & 0 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:recommendation_id>/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/<int:recommendation_id>/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
######################################################################
Expand Down
40 changes: 40 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
######################################################################
Expand Down

0 comments on commit 8adf068

Please sign in to comment.