A small library system: books, loans, and the questions a librarian asks - who has what, what's overdue, what else did this author write. The data lives in Tables, the builtin collection for rows of a struct, queried in memory without a database.
A Table<Row> holds structs like rows of an SQL table, one column per field. Table<Book, id> also has a primary key: the id field, with at most one row per value and lookups by it. withKey("id") turns a plain table into a keyed one, failing when two rows share a key.
Keys also enforce rules. Loans are keyed by book, so a book can only be lent once at a time: loans.insert(...) fails when a loan for that book already exists, and !! passes that error on from lend. The first test checks this.
where, join, orderBy and groupBy read like their SQL namesakes, but take lambdas, so a condition is ordinary code and every field access is type checked. overdue filters the loans, joins each one to its book by id, and builds an Overdue row from the pair. byAuthor sorts and groups into a Map from author to their books. column<String>("title") reads one field of every row.
Due dates come from &clock.now(). The overdue test injects a FixedClock set to 1 January, lends a book (due on the 15th), and then moves the clock 20 days forward with advance. The book is 6 days late, every time the test runs.
Table and keyed Table<Row, key>.where, join, orderBy, groupBy.Instant and Duration, with a FixedClock in tests.@testHelper for shared test data.