Documentation
ยถ
Overview ยถ
Package pgtestkit provides an easy way to create and manage test PostgreSQL databases for Go tests. It automatically handles the lifecycle of an embedded PostgreSQL server and provides utilities for database setup and teardown.
Key Features:
- ๐ Automatic embedded PostgreSQL server management
- ๐งช Isolated test databases for each test case
- โก๏ธ High performance with server reuse
- ๐ Automatic resource cleanup
- ๐ ๏ธ Interface-based architecture supporting any database driver or ORM
- ๐ฆ Built-in support for GORM (see example/gorm)
Basic Usage with database/sql:
func TestWithSQL(t *testing.T) {
// Create a test database with the default SQL connector
dbClient, err := pgtestkit.CreateTestDB(nil)
if err != nil {
t.Fatalf("Failed to create test DB: %v", err)
}
defer dbClient.Close()
// Get the *sql.DB instance
db := dbClient.Client.(*sql.DB)
// ... your test code ...
}
Using with GORM (see example/gorm for more details):
import "github.com/tidylogic/pgtestkit/example/gorm"
func TestWithGORM(t *testing.T) {
// Create a test database with GORM connector
dbClient, err := pgtestkit.CreateTestDB(&gorm.GORMConnector{})
if err != nil {
t.Fatalf("Failed to create test DB: %v", err)
}
defer dbClient.Close()
// Get the *gorm.DB instance
gormDB := dbClient.Client.(*gorm.DB)
// ... your test code ...
}
For more examples and advanced usage, see the example directory.
Index ยถ
Constants ยถ
const ( // ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ธฐ๋ณธ ์ค์ DefaultUser = "postgres" DefaultPassword = "postgres" DefaultDB = "postgres" DefaultLocale = "en_US.UTF-8" TestDBPrefix = "testdb_" )
Variables ยถ
This section is empty.
Functions ยถ
func IsLoggingEnabled ยถ
func IsLoggingEnabled() bool
IsLoggingEnabled returns whether logging is currently enabled
func StartEmbeddedPostgres ยถ
func StartEmbeddedPostgres(dbConfig *embeddedpostgres.Config) error
StartEmbeddedPostgres ์๋ฒ ๋๋ PostgreSQL ์๋ฒ๋ฅผ ์์ํฉ๋๋ค. ์ด ํจ์๋ ์ค๋ ๋ ์์ ํ๋ฉฐ, ์ฌ๋ฌ ๋ฒ ํธ์ถ๋์ด๋ ์๋ฒ๋ ํ ๋ฒ๋ง ์์๋ฉ๋๋ค.
func StopPostgres ยถ
func StopPostgres() error
StopPostgres ์๋ฒ ๋๋ PostgreSQL ์๋ฒ๋ฅผ ์ค์งํฉ๋๋ค. ์ด ํจ์๋ ์ค๋ ๋ ์์ ํ๋ฉฐ, ์ฌ๋ฌ ๋ฒ ํธ์ถ๋์ด๋ ์์ ํฉ๋๋ค.
func TestMainWrapper ยถ
func TestMainWrapper(m *testing.M, dbConfig *embeddedpostgres.Config) int
TestMainWrapper ํ ์คํธ ๋ฉ์ธ ํจ์๋ฅผ ๋ํํ์ฌ ํ ์คํธ ํ๊ฒฝ์ ์ค์ ํฉ๋๋ค. ์ด ํจ์๋ ํ ์คํธ ํจํค์ง์ TestMain ํจ์์์ ํธ์ถ๋์ด์ผ ํฉ๋๋ค.
์ฌ์ฉ ์์:
func TestMain(m *testing.M) {
os.Exit(testing.TestMainWrapper(m, nil))
}
Types ยถ
type DBClient ยถ
type DBClient struct {
Client interface{} // ๋ชจ๋ ์ข
๋ฅ์ DB ํด๋ผ์ด์ธํธ๋ฅผ ์ ์ฅ
DBName string
ConnectionString string
// contains filtered or unexported fields
}
DBClient ๋ฐ์ดํฐ๋ฒ ์ด์ค ํด๋ผ์ด์ธํธ๋ฅผ ๋ํ๋ ๋๋ค.
func CreateTestDB ยถ
func CreateTestDB(connector DBConnector) (*DBClient, error)
CreateTestDB ์ง์ ๋ ์ปค๋ฅํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์์ฑํฉ๋๋ค. ์ฌ์ฉ์๋ ๋ฐ๋์ DBConnector ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ปค๋ฅํฐ๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
func (*DBClient) Close ยถ
Close ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ์์ ํ๊ฒ ์ข ๋ฃํ๊ณ ํ ์คํธ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ญ์ ํฉ๋๋ค. ์ด ๋ฉ์๋๋ ์ฌ๋ฌ ๋ฒ ํธ์ถํด๋ ์์ ํฉ๋๋ค.
์ผ๋ฐ์ ์ผ๋ก ํ ์คํธ ํจ์ ์์ ์ defer์ ํจ๊ป ์ฌ์ฉํฉ๋๋ค:
func TestSomething(t *testing.T) {
dbClient, err := CreateTestDB()
if err != nil {
t.Fatalf("Failed to create test DB: %v", err)
}
defer func() {
if err := dbClient.Close(); err != nil {
t.Logf("Warning: error closing database client: %v", err)
}
}()
// ... ํ
์คํธ ์ฝ๋ ...
}
type DBConnector ยถ
type DBConnector interface {
// Connect ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฐ๊ฒฐํ๊ณ , ORM ํด๋ผ์ด์ธํธ์ SQL DB๋ฅผ ๋ฐํํฉ๋๋ค.
Connect(connString string) (interface{}, error)
// Close ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ์ข
๋ฃํฉ๋๋ค.
Close() error
// Reset ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ด๊ธฐ ์ํ๋ก ๋๋๋ฆฝ๋๋ค.
Reset() error
}
DBConnector ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ๊ด๋ฆฌํ๋ ์ธํฐํ์ด์ค์ ๋๋ค.
type TestHelper ยถ
type TestHelper struct {
// contains filtered or unexported fields
}
TestHelper ํ ์คํธ์ ์ ์ฉํ ํฌํผ ํจ์๋ค์ ์ ๊ณตํฉ๋๋ค.
func NewTestHelper ยถ
func NewTestHelper(dbClient *DBClient) *TestHelper
NewTestHelper ์๋ก์ด TestHelper ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค.
func (*TestHelper) Close ยถ
func (h *TestHelper) Close() error
Close ํ ์คํธ ํฌํผ์์ ์ฌ์ฉํ ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํฉ๋๋ค.
func (*TestHelper) MustResetDB ยถ
func (h *TestHelper) MustResetDB(t testing.TB)
MustResetDB ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ด๊ธฐ ์ํ๋ก ๋๋๋ฆฌ๊ณ , ์คํจํ๋ฉด ํ ์คํธ๋ฅผ ์ฆ์ ์ค๋จํฉ๋๋ค.
func (*TestHelper) ResetDB ยถ
func (h *TestHelper) ResetDB() error
ResetDB ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ด๊ธฐ ์ํ๋ก ๋๋๋ฆฝ๋๋ค.