dbassert

package module
v0.0.0-...-1bc1bd8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MPL-2.0 Imports: 9 Imported by: 0

README

dbassert

The dbassert package provides some helpful functions to help you write better tests when writing Go database applications. The package supports both sql.DB and Gorm assertions.

Example sql.DB asserts usage:
package your_brilliant_pkg

import (
    "testing"
    dbassert "github.com/hashicorp/dbassert"
)

func TestSomeDb(t *testing.T) {
	conn, err := sql.Open("postgres", "postgres://postgres:secret@localhost:%s?sslmode=disable")
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	
	dbassert := dbassert.New(t, conn, "postgres")
    
	// assert that the db column is nullable
	dbassert.Nullable("some_table", "some_column")

	// assert that the db column is a particular domain type
	dbassert.Domain("test_table_dbasserts", "public_id", "dbasserts_public_id")

}
Example Gorm asserts usage:
package your_brilliant_pkg

import (
    "testing"
    dbassert "github.com/hashicorp/dbassert/gorm"
)

func TestSomeGormModel(t *testing.T) {
	conn, err := sql.Open("postgres", "postgres://postgres:secret@localhost:%s?sslmode=disable")
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	db, err := gorm.Open("postgres", conn)
 	m := testModel{}
	if err = db.Create(&m).Error; err != nil {
    	t.Fatal(err)
	}
	dbassert := dbassert.New(t, conn, "postgres")
    
	// assert that the db field is null
	dbassert.IsNull(&someModel, "SomeField")

	// assert that the db field is not null
	dbassert.NotNull(&someModel, "SomeField")

	// assert that the db field nullable
	dbassert.Nullable(&someModel, "SomeField")

	// assert that the db field is a particular domain type
	dbassert.Domain(&someModel, "SomeField", "some_domain_type")
}

Documentation

Overview

Package dbassert provides a set of assertions for testing Go database applications.

Example Usage:

import (
	"testing"

	"github.com/hashicorp/dbassert"
)

func TestSomeDatabase(t *testing.T) {
	db, err := sql.Open("postgres", "postgres://postgres:secret@localhost:db?sslmode=disable")
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()
	dbassert := dbassert.New(t, conn, "postgres")
	dbassert.Nullable("some_table_name", "some_column")
}

Index

Constants

This section is empty.

Variables

View Source
var (
	StartDbInDocker = startDbInDockerUnsupported

	ErrDockerUnsupported = errors.New("docker is not currently supported on this platform")
)
View Source
var ErrNilTestingT = errors.New("TestingT is nil")

Functions

func NullableString

func NullableString(value sql.NullString) string

NullableString is a type alias for nullable database columns for strings.

func TestSetup

func TestSetup(t *testing.T, dialect string) (func() error, *sql.DB, string)

TestSetup sets up the testing env, including starting a docker container running the db dialect and initializing the test database schema.

Types

type ColumnInfo

type ColumnInfo struct {
	// TableName for the column.
	TableName string

	// Name of the column.
	Name string

	// Default value for the column.
	Default string

	// Type of the column.
	Type string

	// DomainName for the column.
	DomainName string

	// IsNullable defines if the column can be null.
	IsNullable bool
}

ColumnInfo defines a set of information about a column.

type DbAsserts

type DbAsserts struct {
	T       TestingT
	Db      *sql.DB
	Dialect string
}

DbAsserts provides database assertion methods around the TestingT interface.

func New

func New(t TestingT, db *sql.DB, dialect string) *DbAsserts

New creates a new DbAsserts.

func (*DbAsserts) Column

func (a *DbAsserts) Column(c ColumnInfo) bool

Column asserts c ColumnInfo is valid.

func (*DbAsserts) Domain

func (a *DbAsserts) Domain(tableName, colName, domainName string) bool

Domain asserts colName in tableName is domainName.

func (*DbAsserts) Nullable

func (a *DbAsserts) Nullable(tableName, colName string) bool

Nullable asserts colName in tableName is nullable.

type MockTesting

type MockTesting struct {
	// contains filtered or unexported fields
}

MockTesting provides a testing.T mock.

func (*MockTesting) AssertError

func (m *MockTesting) AssertError(t *testing.T)

AssertError asserts that the MockTesting has a current error.

func (*MockTesting) AssertNoError

func (m *MockTesting) AssertNoError(t *testing.T)

AssertNoError asserts that the MockTesting has no current error.

func (*MockTesting) ErrorMsg

func (m *MockTesting) ErrorMsg() string

ErrorMsg returns if the MockTesting has an error msg for a previous test.

func (*MockTesting) Errorf

func (m *MockTesting) Errorf(format string, args ...interface{})

Errorf provides a mock Errorf function.

func (*MockTesting) FailNow

func (m *MockTesting) FailNow()

FailNow provides a mock FailNow function.

func (*MockTesting) HasError

func (m *MockTesting) HasError() bool

HasError returns if the MockTesting has an error for a previous test.

func (*MockTesting) Reset

func (m *MockTesting) Reset()

Reset will reset the MockTesting for the next test.

type THelper

type THelper interface {
	Helper()
}

THelper is the helper interface used by the dbassert package.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
}

TestingT is the testing interface used by the dbassert package.

Directories

Path Synopsis
Package gorm provides a set of assertions for testing Go database applications that use gorm.
Package gorm provides a set of assertions for testing Go database applications that use gorm.

Jump to

Keyboard shortcuts

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