Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -45,15 +45,15 @@ 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:
return location
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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -54,15 +54,15 @@ 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:
return user
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:
Expand All @@ -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(
Expand All @@ -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:
Expand All @@ -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:
Expand Down
48 changes: 38 additions & 10 deletions tests/test_Locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,52 +37,52 @@ 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
"city": "Denver 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)


# 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
"city": "Denver 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[
Expand All @@ -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)
40 changes: 40 additions & 0 deletions tests/test_Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down