pgtestkit

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 15 Imported by: 0

README ยถ

๐Ÿš€ pgtestkit

ํ•œ๊ตญ์–ด | English

Go Reference Go Report Card License GitHub release Build Status codecov

A lightweight PostgreSQL database management tool for Go testing. It helps you easily run and manage an embedded PostgreSQL server in your test environment.

โœจ Features

  • PostgreSQL Specialized: Optimized testing tool specifically for PostgreSQL
  • Zero Configuration: Ready to use with default settings
  • Fast Test Execution: Reuses embedded server for faster test runs
  • Complete Isolation: Ensures database isolation between tests
  • Flexible Architecture: Interface-based design works with any ORM or driver
  • Flexible Database Connection: Compatible with various database clients through the DBConnector interface

Code Quality

This project uses golangci-lint to maintain code quality.

Running Linters
# Run linters
golangci-lint run

# Fix auto-fixable issues
golangci-lint run --fix

Or use the Makefile:

# Format code and run linters
make all
Included Linters
  • gofmt/goimports: Code formatting
  • govet: Go vet checks
  • staticcheck: Static analysis
  • errcheck: Error check verification
  • gocritic: Code quality suggestions
  • And other useful linters

Key Features

  • ๐Ÿš€ Automatically starts/stops embedded PostgreSQL server during tests
  • ๐Ÿงช Ensures test isolation with automatic database reset between tests
  • โšก๏ธ High performance - server starts only once and is reused across tests
  • ๐Ÿ”„ Automatic resource cleanup
  • ๐Ÿ› ๏ธ Supports both GORM and standard database/sql interfaces
  • ๐Ÿ”Œ Customizable database settings

Installation

go get github.com/tidylogic/pgtestkit
Implementing DBConnector

You can use any database client by implementing the DBConnector interface:

type DBConnector interface {
    // Connect connects to the database and returns the ORM client and SQL DB
    Connect(connString string) (interface{}, error)
    // Close closes the database connection
    Close() error
    // Reset resets the database to its initial state
    Reset() error
}

See the example directory for implementation examples.

Quick Start

Basic Usage
package yourpackage_test

import (
    "database/sql"
    "testing"

    "github.com/tidylogic/pgtestkit"
    "github.com/tidylogic/pgtestkit/example/sql"
)

func TestWithSQL(t *testing.T) {
    // Create test DB with SQL connector
    dbClient, err := pgtestkit.CreateTestDB(&sql.SQLConnector{})
    if err != nil {
        t.Fatalf("Failed to create test DB: %v", err)
    }
    defer dbClient.Close()

    // Get *sql.DB instance
    db := dbClient.Client.(*sql.DB)
    
    // Write your test code here...
}
Using with GORM
package yourpackage_test

import (
    "testing"

    "github.com/tidylogic/pgtestkit"
    "github.com/tidylogic/pgtestkit/example/gorm"
    "gorm.io/gorm"
)

func TestWithGORM(t *testing.T) {
    // Create test DB 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 *gorm.DB instance
    gormDB := dbClient.Client.(*gorm.DB)
    
    // Write your test code using GORM...
}
Using with TestMain
package yourpackage_test

import (
    "os"
    "testing"
    "github.com/tidylogic/pgtestkit"
)

func TestMain(m *testing.M) {
    // Automatically manages DB server before/after tests
    os.Exit(pgtestkit.TestMainWrapper(m))
}
Using Test Helpers
func TestWithHelper(t *testing.T) {
    dbClient, err := pgtestkit.CreateTestDB(&gorm.GORMConnector{})
    if err != nil {
        t.Fatalf("Failed to create test DB: %v", err)
    }
    defer dbClient.Close()
    
    // Create test helper
    helper := pgtestkit.NewTestHelper(dbClient.GormDB)
    defer helper.Close()
    
    t.Run("Test case 1", func(t *testing.T) {
        // Reset database before test case
        helper.MustResetDB(t)
        
        // Test code...
    })
    
    t.Run("Test case 2", func(t *testing.T) {
        // Another test case (completely isolated from previous test)
        helper.MustResetDB(t)
        
        // Another test code...
    })
}

Advanced Usage

Implementing Custom Connector

You can create your own database connector by implementing the DBConnector interface:

package yourpackage

type YourConnector struct {
    // Store connector state
}

func (c *YourConnector) Connect(connString string) (interface{}, error) {
    // Implement database connection logic
    // Return type depends on your ORM/driver
    return yourDBClient, nil
}

View in Korean | View in English

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 ยถ

View Source
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 SetLogging ยถ

func SetLogging(enabled bool)

SetLogging enables or disables logging

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 ยถ

func (c *DBClient) Close() error

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 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL