Overhaul test.CreateTestDb to use testing.T idioms#78
Conversation
- Accept *testing.T parameter instead of returning cleanup function - Use t.Context() for context instead of context.Background() - Register cleanup via t.Cleanup() instead of returning a closure - Use t.Fatal() for migration error handling - Update all callers across 3 test files to use new signature Resolves xmtp#74 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
t.Context() is cancelled when the test ends, but t.Cleanup runs after that — so truncation queries were failing with "context canceled", causing data to leak between tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| } | ||
|
|
||
| t.Cleanup(func() { | ||
| cleanupCtx := context.Background() |
There was a problem hiding this comment.
Can you double check that t.Context() isn't available in cleanup scripts? Point me to the relevant docs.
|
Re: comment — Yes, confirmed.
This is also in the Go 1.24 release notes:
The CI failure confirmed this — all four TRUNCATE TABLE queries failed with Using |
Resolves #74
Summary
test/helpers.go:CreateTestDbnow accepts*testing.T, usest.Context()for context, registers cleanup viat.Cleanup(), and callst.Fatal()on migration errors. Returns only*bun.DB.db, cleanup := test.CreateTestDb()/defer cleanup()withdb := test.CreateTestDb(t)acrossinstallations_test.go,subscriptions_test.go, andlistener_test.go.Test_Get: Previously had cleanup commented out — now handled automatically byt.Cleanup.Test plan
go vet ./...passes (verified locally)go test -p 1 ./...passes with Docker services runningt.Cleanup🤖 Generated with Claude Code
Note
Overhaul
test.CreateTestDbto usetesting.TidiomsCreateTestDbsignature from() (*bun.DB, func())to(t *testing.T) *bun.DBin test/helpers.go, eliminating the manual cleanup return value.t.Cleanupto truncate DB tables automatically and callst.Fatalif migration fails, rather than silently ignoring errors.defer cleanup()calls.Macroscope summarized bc8539c.