Categorygithub.com/hashicorp/dbassert
modulepackage
0.0.0-20231012105025-1bc1bd88e22b
Repository: https://github.com/hashicorp/dbassert.git
Documentation: pkg.go.dev

# 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")
}

# Packages

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

# Functions

New creates a new DbAsserts.
NullableString is a type alias for nullable database columns for strings.
TestSetup sets up the testing env, including starting a docker container running the db dialect and initializing the test database schema.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

ColumnInfo defines a set of information about a column.
DbAsserts provides database assertion methods around the TestingT interface.
MockTesting provides a testing.T mock.

# Interfaces

TestingT is the testing interface used by the dbassert package.
THelper is the helper interface used by the dbassert package.