Skip to content
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
10 changes: 6 additions & 4 deletions services/catalog/catalog_service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions services/catalog/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Loading