Skip to content
Gasv0.10.0

gas/migrate

migrate/migratetest

import "github.com/gasmod/gas/migrate/migratetest"

Package migratetest holds a mock of gas.MigrationManager, MockMigrationManager, for tests of services that register migrations.

The mock records each call, and returns what the function in the field named after the method returns:

m := &migratetest.MockMigrationManager{
RunPendingFn: func() error { return errBoom },
}
err := m.RunPending()
// err is errBoom, and m.CallCount("RunPending") is 1

With a field nil, its method succeeds and does nothing:

  • Name returns migrate.ServiceName.
  • Init, Close, RegisterFS, RunPending, RunPendingContext, Down and DownContext return nil.
  • Register and RegisterSlice return nothing.

RunPendingContext returns what RunPendingFn returns, and DownContext what DownFn returns.

Each method appends a Call with its name and arguments, the context of Init and Close included, before it calls its field. A call is recorded under the method called only: gas’s Start calls RunPendingContext, so after it CallCount(“RunPendingContext”) is 1 and CallCount(“RunPending”) is 0. To read them:

  • Calls returns a copy of them in the order they were made.
  • CallCount counts those of one method.
  • Reset forgets them all.

The methods may be called from several goroutines at once, as the calls are kept under a lock, which Calls, CallCount and Reset take too. Setting a field while a method runs is a data race.

type Call struct {
// Method is the name of the method called.
Method string
// Args are the method's arguments, in order.
Args []any
}

Call is one call of the mock’s method.

type MockMigrationManager struct {
// NameFn is what Name returns. When it is nil, Name returns
// migrate.ServiceName.
NameFn func() string
// InitFn is what Init returns. When it is nil, Init returns nil.
InitFn func(ctx context.Context) error
// CloseFn is what Close returns. When it is nil, Close returns nil.
CloseFn func(ctx context.Context) error
// RegisterFn is called by Register, when it is set.
RegisterFn func(service gas.Service, m gas.Migration)
// RegisterSliceFn is called by RegisterSlice, when it is set.
RegisterSliceFn func(service gas.Service, migrations []gas.Migration)
// RegisterFSFn is what RegisterFS returns. When it is nil, RegisterFS
// returns nil.
RegisterFSFn func(service gas.Service, fsys fs.FS) error
// RunPendingFn is what RunPending returns. When it is nil, RunPending
// returns nil.
RunPendingFn func() error
// DownFn is what Down returns. When it is nil, Down returns nil.
DownFn func(n int) error
// contains filtered or unexported fields
}

MockMigrationManager is a gas.MigrationManager that records its calls, as described in the package documentation.

func (m *MockMigrationManager) CallCount(method string) int

CallCount returns how many calls were made to method.

func (m *MockMigrationManager) Calls() []Call

Calls returns a copy of the calls, in the order they were made.

func (m *MockMigrationManager) Close(ctx context.Context) error

Close records the call and returns CloseFn’s result.

func (m *MockMigrationManager) Down(n int) error

Down records the call and returns DownFn’s result.

func (m *MockMigrationManager) DownContext(ctx context.Context, n int) error

DownContext records the call, with ctx and n, and returns DownFn’s result.

func (m *MockMigrationManager) Init(ctx context.Context) error

Init records the call and returns InitFn’s result.

func (m *MockMigrationManager) Name() string

Name records the call and returns NameFn’s result.

func (m *MockMigrationManager) Register(service gas.Service, migration gas.Migration)

Register records the call and calls RegisterFn.

func (m *MockMigrationManager) RegisterFS(service gas.Service, fsys fs.FS) error

RegisterFS records the call and returns RegisterFSFn’s result.

func (*MockMigrationManager) RegisterSlice

Section titled “func (*MockMigrationManager) RegisterSlice”
func (m *MockMigrationManager) RegisterSlice(service gas.Service, migrations []gas.Migration)

RegisterSlice records the call and calls RegisterSliceFn.

func (m *MockMigrationManager) Reset()

Reset forgets every call.

func (m *MockMigrationManager) RunPending() error

RunPending records the call and returns RunPendingFn’s result.

func (*MockMigrationManager) RunPendingContext

Section titled “func (*MockMigrationManager) RunPendingContext”
func (m *MockMigrationManager) RunPendingContext(ctx context.Context) error

RunPendingContext records the call, with ctx, and returns RunPendingFn’s result.