diff --git a/src/locations.py b/src/locations.py index 2078266..c3a4d0c 100644 --- a/src/locations.py +++ b/src/locations.py @@ -19,14 +19,14 @@ def check_name(cls, value): return value -@app.post("/locations", response_model=Location, status_code=201) +@app.post("/locations", response_model=Location, status_code=201, tags=["Locations"]) def create_location(location: Location = Body(...)): location.id = random.randint(10000, 99999) # generate a random 5-digit number locations.append(location) return location -@app.get("/locations", response_model=List[Location]) +@app.get("/locations", response_model=List[Location], tags=["Locations"]) def get_locations( city: Optional[str] = None, country: Optional[str] = None, @@ -45,7 +45,7 @@ def get_locations( return filtered_locations -@app.get("/locations/{location_id}", response_model=Location) +@app.get("/locations/{location_id}", response_model=Location, tags=["Locations"]) def get_location(location_id: int = Path(..., ge=0)): for location in locations: if location.id == location_id: @@ -53,7 +53,7 @@ def get_location(location_id: int = Path(..., ge=0)): raise HTTPException(status_code=404, detail="Location not found") -@app.put("/locations/{location_id}", response_model=Location) +@app.put("/locations/{location_id}", response_model=Location, tags=["Locations"]) def update_location(location_id: int, location: Location = Body(...)): for l in locations: if l.id == location_id: @@ -63,7 +63,7 @@ def update_location(location_id: int, location: Location = Body(...)): raise HTTPException(status_code=404, detail="Location not found") -@app.patch("/locations/{location_id}", response_model=Location) +@app.patch("/locations/{location_id}", response_model=Location, tags=["Locations"]) def patch_location(location_id: int, location: Location = Body(...)): for l in locations: if l.id == location_id: @@ -75,7 +75,7 @@ def patch_location(location_id: int, location: Location = Body(...)): raise HTTPException(status_code=404, detail="Location not found") -@app.delete("/locations/{location_id}") +@app.delete("/locations/{location_id}", tags=["Locations"]) def delete_location(location_id: int): for location in locations: if location.id == location_id: diff --git a/src/main.py b/src/main.py index c1e3ba9..e81931b 100644 --- a/src/main.py +++ b/src/main.py @@ -15,7 +15,7 @@ class User(BaseModel): jobTitle: Optional[str] = None -@app.get("/users", response_model=List[User]) +@app.get("/users", response_model=List[User], tags=["Users"]) def get_users( name: Optional[str] = None, user_id: Optional[int] = None, @@ -54,7 +54,7 @@ def get_users( return filtered_users -@app.get("/users/{user_id}", response_model=User) +@app.get("/users/{user_id}", response_model=User, tags=["Users"]) def get_user(user_id: int = Path(..., ge=0)): for user in users: if user.userId == user_id: @@ -62,7 +62,7 @@ def get_user(user_id: int = Path(..., ge=0)): raise HTTPException(status_code=404, detail="User not found") -@app.post("/users", response_model=User, status_code=201) +@app.post("/users", response_model=User, status_code=201, tags=["Users"]) def create_user(user: User = Body(...)): for u in users: if u.userId == user.userId: @@ -74,7 +74,7 @@ def create_user(user: User = Body(...)): return user -@app.patch("/users/{user_id}", response_model=User) +@app.patch("/users/{user_id}", response_model=User, tags=["Users"]) def patch_user(user_id: int, user: User = Body(...)): if user.userId != user_id: raise HTTPException( @@ -91,7 +91,7 @@ def patch_user(user_id: int, user: User = Body(...)): raise HTTPException(status_code=404, detail="User not found") -@app.put("/users/{user_id}", response_model=User) +@app.put("/users/{user_id}", response_model=User, tags=["Users"]) def update_user(user_id: int, user: User = Body(...)): for u in users: if u.userId == user_id: @@ -103,7 +103,7 @@ def update_user(user_id: int, user: User = Body(...)): raise HTTPException(status_code=404, detail="User not found") -@app.delete("/users/{user_id}") +@app.delete("/users/{user_id}", tags=["Users"]) def delete_user(user_id: int): for user in users: if user.userId == user_id: diff --git a/tests/test_Locations.py b/tests/test_Locations.py index 0d1678b..255e044 100644 --- a/tests/test_Locations.py +++ b/tests/test_Locations.py @@ -37,7 +37,7 @@ def test_2_validate_object_creation(): # Validate PATCH request for updating a location record using /location endpoint def test_3_validate_object_updates_patch(): url = "http://127.0.0.1:8000/locations" - new_user = { + new_location = { "id": random.randint( 10000, 99999 ), # Creates a location with the following attributes @@ -45,28 +45,28 @@ def test_3_validate_object_updates_patch(): "country": "United States PATCH", } post_object = requests.post( - url, json=new_user + url, json=new_location ) # POST operation for updating the new location response_body = post_object.json() # Converts the response into JSON location_id = response_body[ "id" ] # Assigns the id to the location_id variable for later use - updated_user = { + updated_location = { "city": "Denver AUTOMATED (PATCH)", # Updates the location with the following attributes "country": "United States AUTOMATED (PATCH)", "id": location_id, # id must be included in the JSON payload which is why we captured it above } assert response_body["id"] == location_id patch_object = requests.patch( - url + f"/{location_id}", json=updated_user + url + f"/{location_id}", json=updated_location ) # PATCH operation for updated location info updated_response_body = patch_object.json() assert updated_response_body["id"] == location_id # Validate id matches as expected assert ( - updated_response_body["country"] == "United States AUTOMATED (PATCH)" + updated_response_body["country"] == "United States AUTOMATED (PATCH)" ) # Validate country was updated assert ( - updated_response_body["city"] == "Denver AUTOMATED (PATCH)" + updated_response_body["city"] == "Denver AUTOMATED (PATCH)" ) # Validate city was successfully updated print(updated_response_body) @@ -74,7 +74,7 @@ def test_3_validate_object_updates_patch(): # Validate PATCH request for updating a location record using /location endpoint def test_4_validate_object_updates_put(): url = "http://127.0.0.1:8000/locations" - new_user = { + new_location = { "id": random.randint( 10000, 99999 ), # Creates a location with the following attributes @@ -82,7 +82,7 @@ def test_4_validate_object_updates_put(): "country": "United States PUT", } post_object = requests.post( - url, json=new_user + url, json=new_location ) # POST operation for updating the new location response_body = post_object.json() # Converts the response into JSON location_id = response_body[ @@ -99,9 +99,37 @@ def test_4_validate_object_updates_put(): updated_response_body = patch_object.json() assert updated_response_body["id"] == location_id # Validate id matches as expected assert ( - updated_response_body["country"] == "United States AUTOMATED (PUT)" + updated_response_body["country"] == "United States AUTOMATED (PUT)" ) # Validate country was updated assert ( - updated_response_body["city"] == "Denver AUTOMATED (PUT)" + updated_response_body["city"] == "Denver AUTOMATED (PUT)" ) # Validate city was successfully updated print(updated_response_body) + + +# Validate DELETE request for deleting a location record using /location endpoint +def test_5_validate_location_object_delete(): + url = "http://127.0.0.1:8000/locations" + new_location = { + "id": random.randint( + 10000, 99999 + ), # Creates a location with the following attributes + "city": "Denver DELETE", + "country": "United States DELETE", + } + post_object = requests.post( + url, json=new_location + ) # POST operation for updating the new location + response_body = post_object.json() # Converts the response into JSON + location_id = response_body[ + "id" + ] # Assigns the id to the location_id variable for later use + delete_object = requests.delete( + url + f"/{location_id}" + ) # DELETE operation for test location + updated_response_body = delete_object.json() + assert delete_object.status_code == 200 + assert updated_response_body["message"] == "Location has been successfully deleted." + get_deleted_object = requests.get(url + f"{location_id}") + assert get_deleted_object.status_code == 404 + print(updated_response_body) diff --git a/tests/test_Users.py b/tests/test_Users.py index bacaabd..3db21e9 100644 --- a/tests/test_Users.py +++ b/tests/test_Users.py @@ -73,6 +73,46 @@ def test_3_validate_object_updates(): print(updated_response_body) +def test_5_validate_user_object_delete(): + url = "http://127.0.0.1:8000/users" + # POST operation for updating the new location + new_user = { + "userId": random.randint( + 10000, 99999 + ), # Creates a new user with the following attributes + "name": "John DELETE", + "city": "Denver DELETE", + "country": "United States DELETE", + "jobTitle": "DELETED", + } + # Converts the response into JSON + post_object = requests.post( + url, json=new_user + ) + # Assigns the id to the user_id variable for later use + response_body = post_object.json() + user_id = response_body[ + "userId" + ] + # Request to delete the test user with the user id captured above + delete_object = requests.delete( + url + f"/{user_id}" + ) + # Assertions to verify that the DELETE was successful + updated_response_body = delete_object.json() + assert delete_object.status_code == 200 + assert updated_response_body["message"] == "User has been successfully deleted." + print(updated_response_body) + get_deleted_object = requests.get(url + f"{user_id}") + assert get_deleted_object.status_code == 404 + deleted_response_body = get_deleted_object.json() + expected_response = { + "detail": "Not Found" + } + assert deleted_response_body == expected_response + print(deleted_response_body) + + def test_sample_items(): try: response = requests.get("http://127.0.0.1:8000/users")