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

Fix GET /items to match with API documentation #176

Open
wants to merge 1 commit into
base: backend
Choose a base branch
from
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
33 changes: 18 additions & 15 deletions backend/src/item/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@ def fetch_all_items() -> Response:
)

items: list[Item] = db.session.execute(db.select(Item)).scalars().all()
payload: list[dict[str, Any]] = [
{
"avatar": item.avatar,
"count": item.count,
"description": item.description,
"id": item.id,
"name": item.name,
"price": {
"discount": item.discount,
"original": item.original,
},
"tags": item_id_to_tags.get(item.id, []),
}
for item in items
]
payload: dict[str, Any] = {
"count": len(items),
"items": [
{
"avatar": item.avatar,
"count": item.count,
"description": item.description,
"id": item.id,
"name": item.name,
"price": {
"discount": item.discount,
"original": item.original,
},
"tags": item_id_to_tags.get(item.id, []),
}
for item in items
],
}
return make_response(payload)


Expand Down
82 changes: 44 additions & 38 deletions backend/tests/unit_tests/item/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,20 +588,23 @@ def test_with_empty_tag_item_in_database_should_respond_correct_payload(
"original": 30,
"discount": 25,
}
excepted_payload: list[dict[str, Any]] = [
{
"avatar": "xx-S0m3-aVA7aR-0f-a991e-xx",
"count": 10,
"description": "This is an apple.",
"id": 1,
"name": "apple",
"price": {
"original": 30,
"discount": 25,
},
"tags": [],
}
]
excepted_payload: dict[str, Any] = {
"count": 1,
"items": [
{
"avatar": "xx-S0m3-aVA7aR-0f-a991e-xx",
"count": 10,
"description": "This is an apple.",
"id": 1,
"name": "apple",
"price": {
"original": 30,
"discount": 25,
},
"tags": [],
}
],
}
with app.app_context():
item = Item(**item_data)
db.session.add(item)
Expand All @@ -616,32 +619,35 @@ def test_with_empty_tag_item_in_database_should_respond_correct_payload(
def test_with_route_should_respond_correct_payload(
self, client: FlaskClient, setup_item: None
) -> None:
excepted_payload: list[dict[str, Any]] = [
{
"avatar": "xx-S0m3-aVA7aR-0f-a991e-xx",
"count": 10,
"description": "This is an apple.",
"id": 1,
"name": "apple",
"price": {
"original": 30,
"discount": 25,
excepted_payload: dict[str, Any] = {
"count": 2,
"items": [
{
"avatar": "xx-S0m3-aVA7aR-0f-a991e-xx",
"count": 10,
"description": "This is an apple.",
"id": 1,
"name": "apple",
"price": {
"original": 30,
"discount": 25,
},
"tags": [{"id": 1, "name": "fruit"}, {"id": 3, "name": "grocery"}],
},
"tags": [{"id": 1, "name": "fruit"}, {"id": 3, "name": "grocery"}],
},
{
"avatar": "xx-S0m3-aVA7aR-0f-ti1a9iA-xx",
"count": 3,
"description": "This is a tilapia.",
"id": 2,
"name": "tilapia",
"price": {
"original": 50,
"discount": 45,
{
"avatar": "xx-S0m3-aVA7aR-0f-ti1a9iA-xx",
"count": 3,
"description": "This is a tilapia.",
"id": 2,
"name": "tilapia",
"price": {
"original": 50,
"discount": 45,
},
"tags": [{"id": 2, "name": "fish"}, {"id": 3, "name": "grocery"}],
},
"tags": [{"id": 2, "name": "fish"}, {"id": 3, "name": "grocery"}],
},
]
],
}

response: TestResponse = client.get("/items")

Expand Down