pgonthefly

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

pgonthefly

pgonthefly is a Golang package providing an interface for creating and deleting PostgreSQL databases on-the-fly. This tool is intended for use in integration testing, allowing developers to verify that database migrations and interfaces are functioning correctly in a controlled environment.

Use Cases

This package is useful for:

  • Testing if migrations work.
  • Testing if the database interfaces work correctly with the database (integration testing).

Limitations

  • This package is designed for integration testing and should not be used for unit testing.
  • Databases created by this package require manual deletion.

Usage

Here is an example of how to use pgonthefly:

package mypackage_test

import (
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"

	pgonthefly "github.com/problem-company-toolkit/pgonthefly"
)

var db *pgonthefly.DB

var _ = BeforeEach(func() {
	var err error

	automigrateAll := func(db *pgonthefly.DB) error {
		// Implement your logic here
		return nil
	}

	db, err = pgonthefly.CreateDatabase("my_db", "localhost", "5432", "my_user", "my_password", pgonthefly.DatabaseOptions{
		AutomigrateFunc: automigrateAll,
	})
	Expect(err).NotTo(HaveOccurred())

	// Initiate your DAO here using the db.Conn
})

var _ = AfterEach(func() {
	// Maybe read from your ENVs here if you're using a devcontainer.
	err := pgonthefly.DeleteDatabase("my_db", "localhost", "5432", "my_user", "my_password", db.Name)
	Expect(err).NotTo(HaveOccurred())
})

var _ = Describe("My DAO tests", func() {
	It("should interact with the real database correctly", func() {
		// Your tests here
	})
})

The example illustrates a typical testing scenario where:

  • In a top-level BeforeEach, we create a test database and initiate the DAO.
  • In the tests, we interact with the real database.
  • In a top-level AfterEach, we delete the test database to clean up.

IMPORTANT: Databases created by this package need to be deleted manually. Ensure you delete any test databases created during your testing to avoid resource clutter.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteDatabase

func DeleteDatabase(dbName, host, port, user, password, target string) error

func GenerateDSN

func GenerateDSN(host, port, dbName, user, password string) string

func GetSchemaConnection

func GetSchemaConnection(dsn string) (*gorm.DB, error)

Types

type DB

type DB struct {
	Conn   *gorm.DB
	Name   string
	Schema string
}

func CreateDatabase

func CreateDatabase(dbName, host, port, user, password string, opts DatabaseOptions) (*DB, error)

func NewDB

func NewDB(conn *gorm.DB, dbName, schema string) *DB

type DatabaseOptions

type DatabaseOptions struct {
	DbSchema        string
	AutomigrateFunc func(*DB) error
}

Jump to

Keyboard shortcuts

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