Skip to content

Refactor tests for improved clarity and maintainability#44

Merged
djthorpe merged 1 commit intomainfrom
djt/0416/test-refactor
Apr 16, 2026
Merged

Refactor tests for improved clarity and maintainability#44
djthorpe merged 1 commit intomainfrom
djt/0416/test-refactor

Conversation

@djthorpe
Copy link
Copy Markdown
Member

This pull request refactors the test setup pattern across several packages to use a functional approach for initializing database connections in tests. The main change is replacing the previous pattern of passing a pointer to a connection struct into the test helper with a setup function that receives a pg.PoolConn and returns an optional cleanup function. This improves flexibility and consistency in test initialization.

Test setup refactoring:

  • Updated the test.Main and pgtest.Main functions (and their usage in test files) to accept a setup function that receives a pg.PoolConn and returns a cleanup function, instead of passing a pointer to a connection struct and a setup function. This change affects pkg/test/main.go, pkg/broadcaster/broadcaster_test.go, pkg/manager/database_test.go, pkg/queue/manager_test.go, and pool_test.go. [1] [2] [3] [4] [5] [6]

  • Updated documentation and usage examples in pkg/test/README.md and pkg/test/doc.go to reflect the new setup pattern for test initialization. [1] [2]

Code improvements and minor changes:

  • Changed the Begin method on Conn to return a pg.PoolConn interface instead of a pointer to Conn, improving abstraction.

  • Added a missing import for types in pkg/test/main.go to support the use of types.Ptr.

  • Internally, the main test helper now constructs and passes the connection struct via the setup function, rather than setting it via pointer assignment.

These changes make the test setup more modular and easier to extend for future needs.

Copilot AI review requested due to automatic review settings April 16, 2026 08:24
@djthorpe djthorpe merged commit 13f8d78 into main Apr 16, 2026
3 checks passed
@djthorpe djthorpe deleted the djt/0416/test-refactor branch April 16, 2026 08:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the integration-test bootstrap pattern to use a functional setup callback that receives a pg.PoolConn and returns an optional cleanup function, updating test suites and documentation to match the new approach.

Changes:

  • Updated pkg/test.Main to accept setup func(pg.PoolConn) (func(), error) and adjusted internal setup/cleanup handling.
  • Updated package TestMain implementations to initialize their package-level test.Conn via the new setup callback.
  • Updated pkg/test documentation (README + package docs) to reflect the new pattern and Begin return type change.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pool_test.go Updates root integration tests to initialize the global test connection via the new setup callback.
pkg/test/main.go Refactors Main signature and setup plumbing; changes Conn.Begin to return pg.PoolConn.
pkg/test/doc.go Updates package-level usage example to the new Main pattern.
pkg/test/README.md Updates README usage snippet to the new Main setup callback pattern.
pkg/queue/manager_test.go Updates queue tests’ TestMain to the new setup callback pattern.
pkg/manager/database_test.go Updates manager tests’ TestMain to the new setup callback pattern.
pkg/broadcaster/broadcaster_test.go Updates broadcaster tests’ TestMain to the new setup callback pattern.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/test/main.go
Comment on lines +79 to 80
cleanup_, err := setup(types.Ptr(Conn{pool, nil}))
if err != nil {
Comment thread pkg/test/doc.go
Comment on lines +13 to +19
// // Start up a container and return the connection
// func TestMain(m *testing.M) {
// test.Main(m, func(pool pg.PoolConn) (func(), error) {
// conn = test.Conn{PoolConn: pool}
// return nil, nil
// })
// }
Comment thread pkg/test/README.md
Comment on lines 23 to +27
func TestMain(m *testing.M) {
pgtest.Main(m, &conn, nil)
pgtest.Main(m, func(pool pg.PoolConn) (func(), error) {
conn = pgtest.Conn{PoolConn: pool}
return nil, nil
})
Comment thread pkg/test/main.go
Comment on lines 77 to 80
cleanup := func() {}
if setup != nil {
cleanup_, err := setup(conn)
cleanup_, err := setup(types.Ptr(Conn{pool, nil}))
if err != nil {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants