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.
- Define a function called
test_update_expense. - Inside it, create an expense with
description="Lunch",amount=12.50,category="food"and extract itsid. - Call
client.patch(f"/expenses/{expense_id}", json={"amount": 15.00})and store the result inresponse. This sends a partial update — only the amount changes. - Add three assertions, each on its own line:
response.status_codeequals200response.json()["amount"]equals15.00(the updated value)response.json()["description"]equals"Lunch"(unchanged)
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding