From fd1d045e9ac4784042c50a1177056d5de6a6ce0a Mon Sep 17 00:00:00 2001 From: mathcolombo Date: Tue, 10 Nov 2026 20:42:59 -0300 Subject: [PATCH] =?UTF-8?q?fix(catalog):=20altera=C3=A7=C3=A3o=20de=20pre?= =?UTF-8?q?=C3=A7o=20n=C3=A3o=20chega=20ao=20carrinho?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Demanda: CAT-BUG-01 Execução: 20261110T234154-cat-bug-01 --- services/catalog/catalog_service/app.py | 10 +++++--- services/catalog/tests/test_catalog.py | 33 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/services/catalog/catalog_service/app.py b/services/catalog/catalog_service/app.py index 5d3873f..dd82dd9 100644 --- a/services/catalog/catalog_service/app.py +++ b/services/catalog/catalog_service/app.py @@ -88,15 +88,17 @@ async def get_product(product_id: str) -> dict: @app.patch("/products/{product_id}") async def update_product(product_id: str, body: ProductUpdate) -> dict: - product = db.update_product(product_id, body.name, body.price_cents, body.stock) - if product is None: + previous = db.get_product(product_id) + if previous is None: raise HTTPException(status_code=404, detail="produto não encontrado") - if body.price_cents is not None and body.price_cents != product["price_cents"]: + old_price_cents = previous["price_cents"] + product = db.update_product(product_id, body.name, body.price_cents, body.stock) + if body.price_cents is not None and body.price_cents != old_price_cents: await bus.publish( new_event( ProductPriceChanged( product_id=product_id, - old_price_cents=product["price_cents"], + old_price_cents=old_price_cents, new_price_cents=body.price_cents, ), PRODUCER, diff --git a/services/catalog/tests/test_catalog.py b/services/catalog/tests/test_catalog.py index 6189610..fd55d66 100644 --- a/services/catalog/tests/test_catalog.py +++ b/services/catalog/tests/test_catalog.py @@ -11,3 +11,36 @@ def test_create_product_publishes_event(): assert response.status_code == 201 assert [event.event_type for event in bus.published] == ["product.created"] + + +def test_patch_price_publishes_price_changed_with_old_and_new_price(): + """CAT-BUG-01 (RN-02): alterar o preço pelo PATCH deve publicar + product.price_changed, para que o carrinho atualize seu snapshot. + Sem a correção, o preço antigo era lido após o update e o evento + nunca era publicado (antigo == novo).""" + bus = InMemoryBus() + with TestClient(create_app(":memory:", bus)) as client: + created = client.post("/products", json={"name": "Mouse", "price_cents": 5000, "stock": 3}).json() + response = client.patch(f"/products/{created['id']}", json={"price_cents": 7500}) + + assert response.status_code == 200 + assert client.get(f"/products/{created['id']}").json()["price_cents"] == 7500 + + changed = [event for event in bus.published if event.event_type == "product.price_changed"] + assert len(changed) == 1 + assert changed[0].payload == { + "product_id": created["id"], + "old_price_cents": 5000, + "new_price_cents": 7500, + } + + +def test_patch_without_price_change_does_not_publish_price_changed(): + """Complemento CAT-BUG-01: PATCH que não muda o preço não gera evento.""" + bus = InMemoryBus() + with TestClient(create_app(":memory:", bus)) as client: + created = client.post("/products", json={"name": "Mouse", "price_cents": 5000, "stock": 3}).json() + client.patch(f"/products/{created['id']}", json={"name": "Mouse Gamer"}) + client.patch(f"/products/{created['id']}", json={"price_cents": 5000}) + + assert [event.event_type for event in bus.published] == ["product.created"]