Capstone: Pila 1.2 · lesson 5 of 9
Ideas in this lesson
Agit switch -c 50-reminder-day-before5.4: a branch per story
BCloses #50links the pull request to its issue
CReview5.5 and 9.7: kind, specific, about the code

Build

Last updated

You're the developer. Build the reminders, one story at a time: a branch for each, small commits, tests alongside the code, and a pull request that says what changed and why. Halfway through, Marco opens a pull request of his own for you to review. This is the longest lesson of the capstone, and the one where everything from Stage 5 comes together.

By the end of this lesson you can

  • Build each story on its own branch, with small commits and tests
  • Open a pull request that explains the change and links its issue
  • Review someone else’s pull request helpfully

What to build

Build what you designed in C.3. If you want a starting shape, here's the one the reference used:

pila/reminders.py, the outline
NameWhat it does
ReminderA dataclass: phone, vaccine, due date, health centre
ReminderBook.add(phone, vaccine, due, centre)Enters a reminder, with the phone normalised
ReminderBook.due_for(today)The reminders to send today. Nothing on days the jobs don’t run: use jobs.runs_on(), like the other scheduled jobs
ReminderBook.stop(phone)No more reminders to this phone
ReminderBook.forget_old(today)Deletes reminders whose vaccine was more than 30 days ago
send_due(book, gateway, today)Sends today’s reminders through the gateway
sms.handle(…, book=None)Answers STOP

Write the tests with the code, or first (lesson 6.6). Use FakeGateway from pila/gateways.py to check what would be sent.

One story, one branch

Move the first story to In progress on your board, and start its branch from an up-to-date main (lesson 5.4). Naming it after the issue keeps the link obvious:

Terminal
git switch main
git pull
git switch -c 50-reminder-day-before
Switched to a new branch '50-reminder-day-before'

Without GitHub, skip git pull. Work on one story at a time: finish it, merge it, then branch for the next.

Commit as you go

Commit each small step that works, with the tests passing (lessons 5.3 and 7.2). Run the checks before each commit:

Terminal
(.venv) $ python -m pytest -q
..............................                    [100%]
30 passed in 0.09s
(.venv) $ ruff check .
All checks passed!
(.venv) $ git add pila/reminders.py tests/test_reminders.py
(.venv) $ git commit -m "Add ReminderBook with due_for and its tests"
The reference’s commits for #50
CommitWhy it’s its own step
Add ReminderBook with due_for and its testsThe core rule, tested on its own
Send due reminders through the gatewayUses the existing boundary; tested with FakeGateway
Run reminders from the nightly jobWiring, kept apart from logic
Add acceptance tests for #50One test per criterion, named after it

The pull request

Push the branch and open a pull request into main (lesson 5.5). Its description is for the reviewer: what changed, why, how you checked it, and what to look at closely.

Pull request description
## What
Vaccination reminders: a text the day before a baby's vaccine is due.

## Why
Closes #50. Ate Cora: mothers forget, and babies miss vaccines.

## How it's checked
- A test for each acceptance criterion, plus the edges
- Sent a few through FakeGateway and read the texts

## Look closely at
- text_for(): is the wording clear, and short enough for one SMS?

“Closes #50” makes GitHub close the issue when the pull request is merged. Merge it once it meets your definition of done, and move the card to Done.

Without GitHub: merge with git switch main then git merge --no-ff 50-reminder-day-before, so the history still shows each story as one piece. Review your own change the next day, reading the diff with git diff main...50-reminder-day-before as if someone else wrote it.

Review Marco’s pull request

While you were on #50, Marco took #51, STOP. He's asked you to review. Click every line you'd comment on, then finish the review.

Pull request · Marco wants to merge into main

Let mothers text STOP to stop reminders

Closes #51. STOP removes the phone’s reminders and replies to say so. Test added.

Read the change. Click any line you would comment on, then press Finish review.

pila/sms.py
tests/test_sms.py
Your review found four problems. What do you choose on GitHub?

How the reference does it

Here's the reference's reminders.py, as it was merged:

How the reference does it pila/reminders.py
"""Vaccination reminders: a text before a baby's vaccine is due (#50)."""

from dataclasses import dataclass
from datetime import date, timedelta

from pila.jobs import runs_on
from pila.phones import normalise

KEEP_FOR = timedelta(days=30)


@dataclass
class Reminder:
    """One vaccine due for one baby. No names: the text doesn't need them."""

    phone: str
    vaccine: str
    due: date
    centre: str


def text_for(reminder: Reminder) -> str:
    """The reminder's text: what, when and where, and how to stop."""
    when = f"{reminder.due:%A} {reminder.due.day} {reminder.due:%B}"
    return (
        f"Reminder: {reminder.vaccine} vaccine is due on {when} at {reminder.centre} health centre. "
        "Text STOP to stop reminders."
    )


class ReminderBook:
    """The reminders a health centre has entered, and the phones that texted STOP."""

    def __init__(self):
        self.reminders: list[Reminder] = []
        self.stopped: set[str] = set()

    def add(self, phone: str, vaccine: str, due: date, centre: str) -> Reminder:
        """Enter a reminder, after the mother has agreed at the counter.

        Agreeing again at the counter starts reminders again after a STOP (#61),
        and entering the same reminder twice keeps one (#60).
        """
        reminder = Reminder(normalise(phone), vaccine, due, centre)
        self.stopped.discard(reminder.phone)
        if reminder not in self.reminders:
            self.reminders.append(reminder)
        return reminder

    def stop(self, phone: str) -> bool:
        """Stop every reminder to this phone. Returns False if it had none."""
        phone = normalise(phone)
        self.stopped.add(phone)
        return any(r.phone == phone for r in self.reminders)

    def due_for(self, today: date) -> list[Reminder]:
        """The reminders to send today.

        Every vaccine due tomorrow, on the days the scheduled jobs run.
        """
        if not runs_on(today):
            return []
        tomorrow = today + timedelta(days=1)
        return [r for r in self.reminders if r.due == tomorrow and r.phone not in self.stopped]

    def forget_old(self, today: date) -> int:
        """Delete reminders whose vaccine was more than 30 days ago. Returns how many."""
        keep = [r for r in self.reminders if r.due >= today - KEEP_FOR]
        forgotten = len(self.reminders) - len(keep)
        self.reminders = keep
        return forgotten


def send_due(book: ReminderBook, gateway, today: date) -> int:
    """Send today's reminders through the gateway. Returns how many were sent."""
    due = book.due_for(today)
    for reminder in due:
        gateway.send(reminder.phone, text_for(reminder))
    return len(due)

And the start of its tests: each acceptance criterion has its own test, named after it.

How the reference does it tests/test_reminders.py, lines 1–35
from datetime import date, time

import pytest

from pila.gateways import FakeGateway
from pila.queue_rules import Queue
from pila.reminders import ReminderBook, send_due, text_for
from pila.sms import NOTHING_TO_STOP, STOPPED, handle

MONDAY = date(2027, 3, 8)
TUESDAY = date(2027, 3, 9)
WEDNESDAY = date(2027, 3, 10)
FRIDAY = date(2027, 3, 12)
SATURDAY = date(2027, 3, 13)
NEXT_MONDAY = date(2027, 3, 15)
NEXT_TUESDAY = date(2027, 3, 16)


def book_with(*dues):
    book = ReminderBook()
    for i, due in enumerate(dues):
        book.add(f"0917000{i:04}", "Pentavalent", due, "San Roque")
    return book


# Acceptance criterion 1: a text the day before the vaccine.
def test_a_vaccine_due_tomorrow_is_reminded_today():
    assert [r.due for r in book_with(TUESDAY).due_for(MONDAY)] == [TUESDAY]


def test_a_vaccine_due_in_two_days_is_not_reminded_yet():
    assert book_with(WEDNESDAY).due_for(MONDAY) == []


def test_a_vaccine_due_today_is_not_reminded_again():

Build the other stories the same way: a branch, small commits, a pull request, a review. When all three are merged, the sprint's building is done.

Check your build

Why a branch for each story, rather than one for the whole feature?
What does “Closes #50” in a pull request description do on GitHub?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development