Infra
Testing and CI
Where tests live
Every test in a generated app is a Ginkgo v2 spec with Gomega matchers: Describe, Context, BeforeEach, It and Expect. Unit and edge-case specs sit beside the code they test, in an external _test package (package adapter_test), so they use a package the way its callers do. When a spec needs something unexported, an export_test.go in the package exports it for tests only. There's no mocking library.
Four things more than one package needs live in test/:
test/
testdb/ a clean, migrated test database per spec
testapp/ the whole application on that database, with a request helper
factories/ valid records with overrides, saved through the real store
requests/ request specs that belong to no single controller
There's no spec/ folder and no fixtures folder: fixed data belongs in seeds. A generator writes the specs for what it generates every time it runs.
bogie test
bogie t
bogie test ./db/adapter
BLOG_ENV=test go test -p 1 -race -cover ./...
These run the suite with the race detector and coverage, in the test environment. Pass a package path to run just that package. Packages run one at a time (-p 1) because they share one test database.
Suites
Each package with tests has one <pkg>_suite_test.go. It runs the package's specs and owns their shared setup, a package variable rebuilt in a BeforeEach before every spec:
// db/adapter/adapter_suite_test.go
var store *adapter.Store
var _ = BeforeEach(func() {
store = testdb.Store(GinkgoT())
})
func TestModels(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Models")
}
That is the store's suite. app/models has a suite of its own with no database in it, since a model's rules are plain calls. app/controllers and test/requests get app (testapp.New(GinkgoT())) in the same way. The helpers in test/ take a small T interface, which both GinkgoT() and *testing.T satisfy.
The test database
testdb.Store(t) opens the real test database, migrates it, and truncates every table of the app's (CASCADE, across the foreign keys between them), so each spec starts from nothing. goose's version table and River's schema are left alone, so migration and job state survive between specs. testdb.Pool(t) is a second connection for raw SQL. The environment is forced to test, so go test run by hand in a development shell can never truncate development data.
There's no in-memory substitute on purpose: a store spec that never touches the database tests nothing. That means the specs need Postgres up and the test database prepared once first:
make up
BLOG_ENV=test bogie db:prepare
bogie test
bogie db:prepare on its own prepares only the development database. The variable's prefix is the app's name in upper case (bogie new shop gives SHOP_ENV). bin/ci rebuilds the test database itself.
Factories
bogie g model writes test/factories/<table>.go with two functions: NewPost builds a valid post without saving it, and Post saves one through the real store and fails the spec if it can't. Every attribute has a default, and an override changes only what the spec cares about:
post := factories.Post(GinkgoT(), store)
draft := factories.Post(GinkgoT(), store, func(p *models.Post) { p.Published = false })
Values that must be unique (emails, names, tokens) come from factories.Sequence("user"), which returns user1, then user2, so two records made in one spec never collide. The factories are written by hand, with no library. Once written, a factory is yours: the generator leaves a changed file alone unless you pass --force. Add a column's default there when you add the column, or let bogie model:sync do it: it merges the new default into your copy and keeps your edits (a clash leaves conflict markers).
Request specs
A feature starts outside in, with a request spec: real HTTP through the real router to the real application and the test database. g controller and g scaffold write one beside the controller, against the suite's app:
var _ = Describe("GET /posts/:id", func() {
Context("when the post exists", func() {
var record models.Post
BeforeEach(func() {
record = factories.Post(GinkgoT(), app.Store)
})
It("shows it", func() {
var shown views.PostResponse
app.Get("/posts/" + fmt.Sprint(record.ID)).Do().Status(http.StatusOK).JSON(&shown)
Expect(shown.ID).To(Equal(record.ID))
})
})
})
app.Get, Post, Put (since v0.6.5), Patch and Delete build the request (with a JSON body for the writes), and WithHeader and WithAuthorization set headers. Do sends it. The response checks are Status, which shows the body on a mismatch, JSON and Contains. Background jobs are inserted, not worked: app.Jobs("send_welcome_email") counts the queued jobs of a kind and app.JobArgs returns their arguments. After g authentication token, .As(subject) on a request sends it as a signed-in user: app.Get("/me").As("user-1").Do().
A controller made by hand, g controller comments show, gets a smaller spec until its actions are written: a Describe("GET /comments/:id") with one It("is mounted") that looks the method and path up in app.Server.Router.Routes(). It does not send a request, so the action can start answering 404 for a missing record without breaking it, and it fails if the route was never registered or the namespace lost its mount line. Specs written before v0.5.5 sent a request and expected anything but 404; they still pass, and re-running the generator replaces them (reported conflict until --force).
test/requests/wiring_test.go checks that every controller is constructed and every route has a handler, the same thing bogie doctor checks from the source.
Store, model and service specs
Below the request specs, the edge cases. g model writes a store spec, db/adapter/<table>_test.go, in package adapter_test on the suite's store, with a Describe per store method: an id that is not a number, is empty or matches nothing is ErrNotFound, an update to a missing record is too, and a saved record is read back, listed, updated and deleted. It also writes the model's own spec, app/models/<name>_test.go: a plain call to Validate on the factory's defaults, which are valid by construction, with nothing to set up. Add a Context there for each rule you put in Validate. A service spec (from g service) fakes the store interface the service itself declares, and g job and g authentication write specs for what they add.
Apps from before v0.4
Apps generated before v0.4.0 used stdlib testing, a testStore helper in app/models/store_test.go, and controller tests on a fake store. bogie app:update adds the suites and test/, but leaves the tests that generators wrote earlier as they are, so both kinds run side by side. Re-running g model or g scaffold for a resource gives it the new specs. See Upgrading an app.
Adding a query
There is no where builder; a query is SQL with a name, in db/queries/<table>.sql:
-- name: ListCommentsForPost :many
SELECT * FROM comments WHERE post_id = $1 ORDER BY created_at DESC;
then make sqlc, which writes ListCommentsForPost into db/sqlc/comments.sql.go with typed parameters, or fails if a column is misspelled. A store method, in db/adapter/comments_extra.go so the generator never overwrites it, then calls it and converts the rows, as the generated methods do. Annotations: :one, :many, :exec, :execrows. The generated file is committed; bin/ci fails if it is stale.
bin/ci
The whole pipeline, run locally rather than on a hosted runner. Every step runs even if an earlier one failed, so one run reports everything:
bin/ci
bogie ci
make ci
- Setup: modules. Downloads and verifies the module graph.
- Setup: database. Brings up the compose stack, drops the test database, and prepares it fresh from the migrations.
- Schema: down and up. Migrates all the way down to zero and back up, so a migration with a broken Down is caught before anyone else hits it.
- Schema: sqlc current. Runs
sqlc diff(called "Schema: models current" before v0.5.0): the code indb/sqlcmust match what sqlc would generate fromdb/queriesand the migrations, so a stale generated file is a failure here, not a silent drift. - Style: gofmt. Fails if any file is not gofmt-clean.
- Style: go vet.
- Style: golangci-lint, against
.golangci.yml. - Build.
go build ./.... - Tests.
go test -p 1 -race -cover ./..., one package at a time because they share the test database. - Worker: boots and drains (apps made with
--jobsonly). Builds the binary and runs it asworkerfor six seconds, to prove it starts.
A green run on a clean tree ends with one more step, Signoff: gh signoff records a status on the commit, if the basecamp/gh-signoff extension is installed; without it, the run is still green but says so. A failing run never signs off, and never should be merged or deployed.
bogie lint
bogie lint
gofmt check, go vet, then golangci-lint, the three style steps of bin/ci on their own, useful while iterating without waiting for the database steps.
bogie doctor
bogie doctor
Checks the things the generators rely on, since Go has no autoload to catch a missing piece:
bogie.tomlexists.- Every marker (
bogie:controllers,bogie:wire,bogie:routes, the three inconfig/config.go,bogie:commandsinmain.go,bogie:seeds, andbogie:jobsif the app has jobs) is present exactly once, in the root files and in every namespace package's ownserver.goandroutes.go. - The pinned tools (goose, sqlc, credentials and Air) are still under the
tooldirective ingo.mod. - The layout files generators depend on exist:
db/sqlc.yaml,db/db.go,bin/ci,docker-compose.yml,AGENTS.md,config/master.key. - Every controller it finds, plain or namespaced, is both constructed in
app/application.goand mounted in the rightroutes.go. A mount with no construction is a nil controller whose first request panics; a construction with no mount serves nothing, and both are caught here rather than at runtime.
It prints one line per check and exits non-zero if anything failed, which is also how a generator test for a removed marker is written: expect the named error, never a partial write.