testingsuite

package
Version: v0.0.0-...-c81ff90 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringWithCharset

func StringWithCharset(length int, charset string) string

StringWithCharset returns a random string https://www.calhoun.io/creating-random-strings-in-go/

Types

type BaseTestSuite

type BaseTestSuite struct {
	suite.Suite
}

BaseTestSuite is a suite for testing

func (*BaseTestSuite) FatalFalse

func (suite *BaseTestSuite) FatalFalse(b bool, messages ...string)

FatalFalse ends a test if a value is not false

func (*BaseTestSuite) FatalNil

func (suite *BaseTestSuite) FatalNil(object interface{}, messages ...string)

FatalNil ends a test if an object is not nil

func (*BaseTestSuite) FatalNoError

func (suite *BaseTestSuite) FatalNoError(err error, messages ...string)

FatalNoError ends a test if an error is not nil

type PackageName

type PackageName string

PackageName represents the project-relative name of a Go package.

func CurrentPackage

func CurrentPackage() PackageName

CurrentPackage returns the project-relative name of the caller's package.

"github.com/transcom/mymove/pkg/" is removed from the beginning of the absolute package name, so the return value will be e.g. "handlers/internalapi".

func (PackageName) String

func (pn PackageName) String() string

func (PackageName) Suffix

func (pn PackageName) Suffix(suffix string) PackageName

Suffix returns a new PackageName with an underscore and the suffix appended to the end suffix should be a snake case string

type PopSuiteTxnStore

type PopSuiteTxnStore struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

PopSuiteTxnStore is a pop.Store that uses go-txdb to wrap the connection in a transaction that will be rolled back when closed

func (*PopSuiteTxnStore) Close

func (store *PopSuiteTxnStore) Close() error

Close closes any open transactions and db connections

func (*PopSuiteTxnStore) Commit

func (store *PopSuiteTxnStore) Commit() error

Commit the transaction if it exists Needed to implement the pop.store interface, but won't actually be called

func (*PopSuiteTxnStore) Rollback

func (store *PopSuiteTxnStore) Rollback() error

Rollback the transaction if it exists Needed to implement the pop.store interface, but won't actually be called

func (*PopSuiteTxnStore) Transaction

func (store *PopSuiteTxnStore) Transaction() (*pop.Tx, error)

Transaction starts a pop.Transaction

func (*PopSuiteTxnStore) TransactionContext

func (store *PopSuiteTxnStore) TransactionContext(ctx context.Context) (*pop.Tx, error)

TransactionContext returns the current pop.Transaction

func (*PopSuiteTxnStore) TransactionContextOptions

func (store *PopSuiteTxnStore) TransactionContextOptions(ctx context.Context, opts *sql.TxOptions) (*pop.Tx, error)

TransactionContextOptions returns the current pop.Transaction

type PopTestSuite

type PopTestSuite struct {
	BaseTestSuite
	PackageName
	// contains filtered or unexported fields
}

PopTestSuite is a suite for testing

func NewPopTestSuite

func NewPopTestSuite(packageName PackageName, opts ...PopTestSuiteOption) PopTestSuite

NewPopTestSuite returns a new PopTestSuite

func (*PopTestSuite) AppContextForTest

func (suite *PopTestSuite) AppContextForTest() appcontext.AppContext

AppContextForTest returns the AppContext for the test suite

func (*PopTestSuite) AppContextWithSessionForTest

func (suite *PopTestSuite) AppContextWithSessionForTest(session *auth.Session) appcontext.AppContext

AppContextWithSessionForTest returns the AppContext for the test suite

func (*PopTestSuite) DB

func (suite *PopTestSuite) DB() *pop.Connection

DB returns a db connection

func (*PopTestSuite) Logger

func (suite *PopTestSuite) Logger() *zap.Logger

Logger returns the logger for the test suite

func (*PopTestSuite) MustCreate

func (suite *PopTestSuite) MustCreate(model interface{})

MustCreate requires creating without errors

func (*PopTestSuite) MustDestroy

func (suite *PopTestSuite) MustDestroy(model interface{})

MustDestroy requires deleting without errors

func (*PopTestSuite) MustSave

func (suite *PopTestSuite) MustSave(model interface{})

MustSave requires saving without errors

func (*PopTestSuite) NilOrNoVerrs

func (suite *PopTestSuite) NilOrNoVerrs(err error)

NilOrNoVerrs checks that an error is effecively nil

func (*PopTestSuite) NoVerrs

func (suite *PopTestSuite) NoVerrs(verrs *validate.Errors) bool

NoVerrs prints any errors it receives

func (*PopTestSuite) Run

func (suite *PopTestSuite) Run(name string, subtest func()) bool

Run overrides the default testify Run to ensure that the testdb is torn down for per txn tests

It would be nice if subtests could start a new transaction inside the current connection so they could reuse db setup between subtests. Unfortunately, because database/sql and pop do not support nested transactions, this gets complicated and hairy quickly. When testing that approach, connections wouldn't get closed and cause other tests to hang or subtests would report incorrect errors about transactions already being closed.

And so, if per test transaction is enabled, each subtest gets a new connection. This means subtests are really just like main tests, but subtests are a helpful way to group tests together, which can be useful. Setup has to be moved to a function that can be run once per subtest. In testing, that was still faster with per test transactions than the old way of cloning a db per package.

If the code under test starts its own transaction, this is the approach that should be used. If it does not use transactions, you can probably get away with using RunWithRollback (see below).

When using per test transactions, watch out for subtests that do not use testify.suite as they won't use this code and thus won't get a per subtest connection. Tests that use the native testing subtests look like

suite.T().Run("name", func(t *testing.T) { ... })

instead of

suite.Run("name", func() { ... })

func (*PopTestSuite) RunWithRollback

func (suite *PopTestSuite) RunWithRollback(name string, subtest func()) bool

RunWithRollback runs a subtest inside a transaction that is rolled back. Not all tests will work with this approach

See Run above for more details, but if the code under test does not use transactions, this way of running subtests should work. If that is true, you can reuse database models created in the main test in each subtest.

func (*PopTestSuite) TearDown

func (suite *PopTestSuite) TearDown()

TearDown runs the teardown for step for the suite Important steps are to close open DB connections and drop the DB

func (*PopTestSuite) TearDownTest

func (suite *PopTestSuite) TearDownTest()

TearDownTest runs the teardown per test. It will only do something useful if per test transactions are enabled

func (*PopTestSuite) Truncate

func (suite *PopTestSuite) Truncate(tables []string) error

Truncate deletes all data from the specified tables.

func (*PopTestSuite) TruncateAll

func (suite *PopTestSuite) TruncateAll() error

TruncateAll deletes all data from all tables that are owned by the user connected to the database.

type PopTestSuiteOption

type PopTestSuiteOption func(*PopTestSuite)

PopTestSuiteOption is type intended to be used to change a PopTestSuite object.

func WithHighPrivPSQLRole

func WithHighPrivPSQLRole() PopTestSuiteOption

WithHighPrivPSQLRole is a functional option that can be passed into the NewPopTestSuite function to create a PopTestSuite that only uses the privileged SQL connection.

func WithPerTestTransaction

func WithPerTestTransaction() PopTestSuiteOption

WithPerTestTransaction is a functional option that can be passed into the NewPopTestSuite function to create a PopTestSuite that runs each test inside a transaction and rolls back at the end of the test. See also PopTestSuite#Run

Jump to

Keyboard shortcuts

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