Test Update

Write a test for the PATCH endpoint that partially updates an expense

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Testing partial updates

The PATCH /expenses/{expense_id} endpoint accepts a partial update — you send only the fields you want to change, and the rest stay the same. This is different from a PUT, which replaces the entire resource.

The key assertion for a partial update test: check that the changed field has the new value and that an unchanged field still has its original value. If the endpoint accidentally wipes fields that were not included in the request, this assertion catches it.

Instructions

Add a test for the update endpoint.

  1. Define a function called test_update_expense.
  2. Inside it, create an expense with description="Lunch", amount=12.50, category="food" and extract its id.
  3. Call client.patch(f"/expenses/{expense_id}", json={"amount": 15.00}) and store the result in response. This sends a partial update — only the amount changes.
  4. Add three assertions, each on its own line:
    • response.status_code equals 200
    • response.json()["amount"] equals 15.00 (the updated value)
    • response.json()["description"] equals "Lunch" (unchanged)