Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 PUT returns 204 (No Content) #13

Merged
merged 2 commits into from
Oct 22, 2020
Merged
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
11 changes: 1 addition & 10 deletions fasteve/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,7 @@ def register_resource(self, resource: Resource) -> None:
)

for method in resource.item_methods:
if method == "PUT":
router.add_api_route(
f"/{resource.name}/{{{str(resource.item_name) + '_id'}}}",
endpoint=item_endpoint_factory(resource, method),
response_model=ItemResponse,
response_model_exclude_unset=True,
methods=[method],
status_code=201,
)
elif method == "DELETE":
if method in ["PUT", "DELETE"]:
router.add_api_route(
f"/{resource.name}/{{{str(resource.item_name) + '_id'}}}",
endpoint=item_endpoint_factory(resource, method),
Expand Down
6 changes: 2 additions & 4 deletions fasteve/io/mongo/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,16 @@ async def remove_item(self, resource: Resource, item_id: ObjectID) -> None:
"""Removes a single document from a database collection."""
collection = await self.get_collection(resource)
try:
result = await collection.delete_one({"_id": item_id})
await collection.delete_one({"_id": item_id})
except Exception as e:
raise e

async def replace_item(
self, resource: Resource, item_id: ObjectID, payload: dict
) -> dict:
) -> None:
"""Replaces single document from a database collection"""
collection = await self.get_collection(resource)
try:
await collection.replace_one({"_id": item_id}, payload)
except Exception as e:
raise e
payload["_id"] = item_id
return payload
10 changes: 2 additions & 8 deletions fasteve/methods/put.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
from fasteve.core.utils import ObjectID


async def put_item(request: Request, item_id: Union[ObjectID, str]) -> dict:
async def put_item(request: Request, item_id: Union[ObjectID, str]) -> None:
orginal_document = await get_document(request, item_id)
if not orginal_document:
# insert
response = await post(request)
return response
document = await post(request)

# replace
payload = getattr(request, "payload")
Expand All @@ -26,8 +25,3 @@ async def put_item(request: Request, item_id: Union[ObjectID, str]) -> dict:
)
except Exception as e:
raise e

response = {}
response[config.DATA] = [document]
print(response)
return response
3 changes: 2 additions & 1 deletion tests/test_aplications.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,15 @@ def test_delete_item(test_client, path, data, expected_status):
@pytest.mark.parametrize(
"path,data,expected_status",
[
("/people", {"name": "Lovelace"}, 201),
("/people", {"name": "Lovelace"}, 204),
],
)
def test_put_replace_item(test_client, path, data, expected_status):
response = test_client.post(path, json={"name": "Curie"}) # insert data for test
item_id = response.json()[app.config.DATA][0]["_id"]
response = test_client.put(path + f"/{item_id}", json=data)
assert response.status_code == expected_status
response = test_client.get(path + f"/{item_id}")
item = response.json()[app.config.DATA][0]
assert item["name"] == data["name"]
assert item["_id"] == item_id