testdb

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package testdb is a test convenience that creates test databases, migrates schema and tears down cleanly.

Ideal for projects that use https://github.com/upper/db.

Migrations are backed by https://github.com/golang-migrate/migrate

The simplest way to use testdb in testing:

  package main
  import (
    "github.com/tnsimon/testdb"
  )
  func setup() {
	migrationPath := "/path/to/migrations.sql"

	cfg := testdb.Config{
		Host:     "localhost",
		Port:     5432,
		User:     "username",
		Password: "password",
		Database: "database",
		Options: map[string]string{
			"sslmode": "require",
		},
	}
	testDB, err := testdb.New(cfg,
		testdb.WithMigrate(migrationPath)
	)
  }

  func cleanup() {
	  testDB.Teardown()
  }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host     string
	Port     int
	User     string
	Password string
	Database string
	Options  map[string]string
}

Config holds configuration for connecting to a SQL database

type Options

type Options func(*TestDB) error

Options applies database options on the TestDB instance

func WithMigrate

func WithMigrate(migrationPath string) Options

WithMigrate migrates to the highest level

type TestDB

type TestDB struct {
	db.Session
	// contains filtered or unexported fields
}

TestDB connects to a test database, creates

func New

func New(cfg Config, options ...Options) (*TestDB, error)

New returns a TestDB instance

func (*TestDB) Close added in v1.0.1

func (t *TestDB) Close() error

Close removes the test database and closes all connections

func (*TestDB) MigrateDown

func (t *TestDB) MigrateDown(migrationPath string) error

MigrateDown migrates database to the lowest version

func (*TestDB) NewSession

func (t *TestDB) NewSession() error

NewSession establishes a new session for the test database

Jump to

Keyboard shortcuts

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