{"id":16064384,"url":"https://github.com/micheam/go-pgtest","last_synced_at":"2025-10-26T15:33:47.973Z","repository":{"id":257814390,"uuid":"868703753","full_name":"micheam/go-pgtest","owner":"micheam","description":"A Go module that simplifies testing with PostgreSQL by providing utilities to set up temporary test databases, ensuring clean and isolated testing environments.","archived":false,"fork":false,"pushed_at":"2024-10-10T00:34:18.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T22:51:12.108Z","etag":null,"topics":["database","docker","dockertest","golang","postgresql","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/micheam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-07T03:07:46.000Z","updated_at":"2024-10-10T00:34:21.000Z","dependencies_parsed_at":"2024-10-08T09:23:27.522Z","dependency_job_id":"2893b9e2-a45a-4e30-808e-0d8812b293c4","html_url":"https://github.com/micheam/go-pgtest","commit_stats":null,"previous_names":["micheam/go-pgtest"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micheam%2Fgo-pgtest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micheam%2Fgo-pgtest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micheam%2Fgo-pgtest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micheam%2Fgo-pgtest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/micheam","download_url":"https://codeload.github.com/micheam/go-pgtest/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248326461,"owners_count":21085105,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","docker","dockertest","golang","postgresql","testing"],"created_at":"2024-10-09T05:06:44.820Z","updated_at":"2025-10-26T15:33:47.864Z","avatar_url":"https://github.com/micheam.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-pgtest\n\n[![Go](https://github.com/micheam/go-pgtest/actions/workflows/go.yml/badge.svg)](https://github.com/micheam/go-pgtest/actions/workflows/go.yml)\n\n`go-pgtest` is a Go module specifically designed to assist in testing applications that depend on PostgreSQL databases. It provides utilities for setting up temporary test databases that are automatically initiated when `go test` is executed and disposed of once the tests are complete. This functionality ensures a clean and isolated testing environment, making it particularly beneficial for developers aiming to streamline and enhance their database testing workflows.\n\n## Usage\n\nTo use `go-pgtest`, integrate it into your Go testing environment. The package offers\nseveral functions to help manage and assert database states during tests.\n\n### Functions\n\n- `Start(ctx context.Context) (func() error, error)`: Starts a new database session.\n    This function may be called at the beginning of a test to create a new database session.\n    We intent to call this from `TestMain` function.\n\n- `Open(t *testing.T, migrationFn func(db *sql.DB) error) *sql.DB`: Opens a new database connection and applies migrations.\n    This function may be called within a test to open a new database connection and apply migrations.\n    The `migrationFn` parameter is a function that accepts a `*sql.DB` and returns an error.\n\n    *Note*: This may block your process until the database is ready.\n\n## Examples\n\nHere's a basic example of how to use `go-pgtest` in a test:\n\n```go\npackage mypackage_test\n\nimport (\n\t...\n\n\tpgtest \"github.com/micheam/go-pgtest\"\n)\n\nfunc TestMain(m *testing.M) {\n\tslog.SetLogLoggerLevel(slog.LevelDebug)\n\n\tctx := context.Background()\n\tcleanup, err := pgtest.Start(ctx)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tm.Run()\n\n\tif err := cleanup(); err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"failed to cleanup pg-test: %v\\n\", err)\n\t}\n}\n\nfunc TestDatabaseOperations(t *testing.T) {\n\t// Setup\n\tmigrationfn := func(db *sql.DB) error {\n\t\t// Add your migration code here\n\t\t_, err := db.Exec(\"CREATE TABLE IF NOT EXISTS test (id uuid not null primary key)\")\n\t\treturn err\n\t}\n\tdb := pgtest.Open(t, migrationfn)\n\tdefer db.Close()\n\n\ttx := db.MustBegin()\n\tdefer tx.Rollback()\n\n\t// Now, you can perform database operations\n\t_, err := db.Exec(\"INSERT INTO test (id) VALUES ($1);\", uuid.NewString())\n\trequire.NoError(t, err)\n}\n```\n\nFor more detailed examples, please refer to the [pgtest_test.go](pgtest_test.go) file.\n\n## Author\n\nThis module was developed by [Michito Maeda](https://micheam.com)\nContributions and feedback are welcome.\n\n## Acknowledgements\n\n`go-pgtest` relies heavily on the `github.com/ory/dockertest` library. We would like to express our gratitude to the maintainers of `dockertest` for their excellent work, which makes this module possible.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicheam%2Fgo-pgtest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicheam%2Fgo-pgtest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicheam%2Fgo-pgtest/lists"}