Ordering backend project · Chapter 11 of 12

Writing tests

By the end of this chapter you can write pytest tests that call DabbaDrop's whole API through FastAPI's TestClient, use the fixtures that give every run a database of its own, test a race between two members and a signed webhook, and tell a test that protects the code from one that only passes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for production
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.

Why you need this

DabbaDrop's backend now does everything launch week needs: accounts, admin routes, browsing, holds, orders and payments. It will keep changing: Bhavna wants a dinner slot, Rafiq wants refunds through the API, Farah wants a second payment provider. Every change can break something that works today, and the three things that must never break are the ones that cost money or trust:

  • login: a wrong password must never get a session;
  • holds: two members must never take the last place of a slot;
  • payments: an unsigned webhook must never confirm an order, and the same event delivered twice must confirm it once.

Until now the Check has tested your code. A Check is not there when Kunal changes hold_slot on a Friday evening. Tests in the repository are: they run on every change, in under a second, on every developer's machine, and they say what broke before twelve cold lunches do. In this chapter you write them.

The catch is that a test can pass and protect nothing. A test with no assertion passes. A test that checks the status but not the cookie passes on a login that hands out a cookie any script on the page can read. So this chapter's Check grades your tests the hard way: it runs them on our code, where they must pass, and on broken copies of our code, where they must fail.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    test_auth.py: test the login

    In tests/test_auth.py, write tests (functions named test_...) for POST /auth/login with Tara (the people fixture's "tara"). With her email and password: assert the status is 200, and that the set-cookie header starts with dabba_session= and contains HttpOnly in any case. With her email and a wrong password: assert the status is 401 and that there is no set-cookie header.

    Use the fixtures tests/conftest.py gives by naming them as parameters (client, people). Run pytest in the terminal.

    The Check runs your file on our code, where every test must pass, and on two broken copies of the project, where at least one test must fail: one whose login takes any password, and one whose session cookie is not HttpOnly.

  2. Task 2

    test_holds.py: test the race for the last place

    In tests/test_holds.py, write a test where Tara and Advait hold the last place of a slot at the same moment: make slot seeded["slot"] (17) one place with the sql fixture (update slots set capacity = 1), log both in with login_as, send both POST /holds ({"slot_id": 17}) together in two threads and wait for both answers, then assert that the two statuses, sorted, are [201, 409], that the 409's json()["error"]["code"] is "slot_full", and that slot_holds has exactly one row for the slot.

    The Check runs your file on our code, where it must pass, and on two broken copies of hold_slot, where it must fail: one that never says the slot is full (both get 201), and one that answers the second member with a 500.

  3. Task 3 · interview question

    Interview: test_payments.py, test the webhook

    In tests/test_payments.py, the given order fixture holds slot 17 for Tara and orders two Dal Tadka (a pending order, a dict with "id" and "total"), and captured(event_id, order) makes a payment.captured event for it. Write two tests. First: post captured("evt_unsigned", order) to /webhooks/payments with client.post(..., json=...), with no signature; assert the status is 401, and that the order's status in the orders table (the sql fixture) is still pending. Second: send captured("evt_1", order) with the webhook fixture, which signs it; assert 200, duplicate False and status confirmed; send the same event again; assert 200 and duplicate True.

    The Check runs your file on our code, where it must pass, and on two broken copies, where it must fail: a signature check that lets every body in, and a payment handler that acts on the same event a second time.

    Kunal will ask: a test passes on your code. How do you know it would fail if the code were wrong?

What you will learn

  • pytest: test functions, plain assert, and what a failure tells you.
  • Fixtures: how a test asks for what it needs by naming it as a parameter.
  • TestClient: calling the app with no server and no port; the status, the headers and the JSON.
  • A database per run: why tests never share rows, and what tests/conftest.py does.
  • Testing a race: two requests at once from two threads, and an assertion that does not care who wins.
  • Testing a webhook: a signed body, an unsigned one, and the same event twice.
  • Mutation testing: breaking the code on purpose to see whether a test notices.

More in Ready for production

Build "Writing tests" yourself

DabbaDrop delivers tiffins across Mumbai. You build its ordering API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.