Files
multica/server/internal/testutil/doc.go
T
Jiayuan Zhang 877a71568f test(server): add internal/testutil and migrate the eight heaviest handler suites (MUL-6361) (#7160)
internal/handler held roughly 1000 hand-written `INSERT ... RETURNING id` blocks with a matching `t.Cleanup(DELETE ...)`, and roughly 1200 copies of the recorder / call / status-check / decode quartet. Each copy was an independent statement of the same contract, so changing a shared one meant editing every copy.

internal/testutil holds the two halves of that contract once: Fixture builds rows for a test workspace and deletes them again when the test ends; Call runs one handler and records what it wrote, with Want asserting the status and JSON decoding the payload.

The eight heaviest suites move onto it: 19,622 lines -> 16,984 (-13.4%). Assertions whose message says something the shared one cannot stay where the test wrote them.

Verified by comparing what actually ran: the package's test inventory is identical before and after, 2822 PASS / 44 SKIP / 0 FAIL over the same test names, and go test -race is green for both packages.
2026-08-19 03:09:59 +08:00

28 lines
1.4 KiB
Go

// Package testutil holds the fixtures the server's DB-backed tests build
// their scenarios from: row builders that insert a fixture row and delete it
// again when the test ends, and HTTP helpers that run one handler and assert
// on what it wrote.
//
// It exists because the cost of changing shared behaviour was scaling with
// the number of test files. Before this package `internal/handler` alone held
// ~1000 hand-written `INSERT INTO ... RETURNING id` blocks, ~1150
// `DELETE FROM` cleanups and ~1200 copies of the
// recorder/call/status/decode quartet, each one an independent statement of
// the same contract. Changing an insert's column set, or the shape of a
// failure message, meant editing every copy.
//
// Two rules keep it that way:
//
// - New DB-backed tests build their rows through a Fixture and drive
// handlers through Call. Do not open-code an INSERT + t.Cleanup pair or a
// recorder/status/decode quartet in a new test.
// - Helpers here stay behaviour-free. They insert rows, run handlers and
// report mismatches; they never assert a product rule on the test's
// behalf. A helper that knows what a correct response looks like moves
// the assertion out of the test that is supposed to make it.
//
// The package imports "testing" and must only be imported from _test files.
// Importing it from production code links the testing flag set into the
// binary.
package testutil