Skip to content

Commit

Permalink
Fix GET /items to match with api documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lai-YT committed Jan 12, 2023
1 parent 711320e commit 02a0107
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 53 deletions.
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

0 comments on commit 02a0107

Please sign in to comment.