Develop book availability in Python
This is a new feature in an existing application, not a request to regenerate the entire library. Track the change from menu input to observed result.
Lab briefing

Original concept illustration (SVG)
| At a glance | Your route |
|---|---|
| Level and time | 200; 70 minutes (facilitation estimate) |
| Starting action | Use controlled dates and test one available and one borrowed copy. |
| Learner materials | Download 03-python.zip |
| Workspace | Open the extracted kit root; run the baseline from library relative to that root |
| Expected initial check | The existing unittest tests are discovered and pass. |
| Setup help | Download, extract, local Git and optional GitHub |
[!NOTE] A passing helper is incomplete until the console menu reaches it.
Concepts · First task · Evidence checklist · Reset
Learning objectives
- Define search and availability semantics before implementation.
- Use the actual Python interfaces and import root.
- Connect a tested feature to existing console actions.
Before you start
Prepare 03-python using the common setup and open the
copy alone. Run the following commands from its library directory. The bundled
feature fixture
uses synthetic JSON data.
Concepts and use cases
Search normalizes input for matching, not necessarily for storage. Availability
belongs to a book_item, not to every copy of a book.
An active loan is one with no return date; a due date in the past does not return it.
Exercise scenario
The librarian requests partial title search and copy-level availability. New loans, reservations, accounts and persistence changes are out of scope.
Task 1 - Inspect the workflow and baseline
python -m unittest discover -s tests -p "test_*.py" -v
- Record test discovery and results.
- Read
console/common_actions.py,console/console_app.py, the entities, and the JSON repository methods. - Ask for a trace of existing patron search and the proposed book-search insertion points. Verify method names against this Python fixture.
- Inspect how loans are linked to
book_itemandpatron.
Task 2 - Set the contract
| ID | Case | Expected behavior |
|---|---|---|
| BOOK-1 | Mixed case and surrounding whitespace | Defined normalized partial match |
| BOOK-2 | Blank input | Explicit prompt/error; no silent fallback |
| BOOK-3 | Missing title | No-results message |
| BOOK-4 | Two copies, one borrowed | Separate availability for each copy |
| BOOK-5 | Historical returned loan | Does not block availability |
| BOOK-6 | Overdue unreturned loan | Still unavailable |
Construct these scenarios in tests with controlled dates. Choose result ordering
explicitly. Do not claim lower() and casefold() have identical behavior for all
Unicode strings; record the normalization you choose for the feature.
Task 3 - Plan before editing
Plan the book-availability feature in the current Python library. Reuse the
existing input loop and repository contracts. Map BOOK-1..6 to tests, distinguish
Book from BookItem, and keep search read-only. Do not implement loans/reservations.
Inspect data access, active-loan joins and menu wiring. A design that joins a loan to a title instead of a copy cannot satisfy BOOK-4.
Task 4 - Implement and test
- Write the two-copy regression before implementation.
- Ask Agent to implement one reviewed slice and no unrelated cleanup.
- Connect the new action to prompt, input mapping and dispatch.
- Add boundary tests and run the whole small fixture suite.
- Run
python console/main.pyand exercise search manually. - Compare JSON files before/after search; search must not persist changes.
- Temporarily treat every past due date as returned. Confirm BOOK-6 fails, then restore the correct implementation.
Verify your work
- The menu reaches the feature.
- Search behavior and ordering are explicit.
- Availability is calculated for each physical copy.
- A negative mutation is detected.
- Borrowing, membership and existing tests remain unchanged.
Troubleshooting
Run from library for imports. Distinguish a no-results response from a loader
failure. If the supplied tests all pass before the feature exists, add the missing
regression rather than assuming they cover the new behavior.
Independent practice
Add an author filter as a separately specified feature. Preserve existing title normalization and declare how combined filters behave.
Reset
Stop the console, save evidence, and restore only named feature/test files in the disposable copy. Do not change the original data or publish a repository automatically.