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 1Defaults
Section titled “Defaults”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.
Concurrency
Section titled “Concurrency”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.
API reference
Section titled “API reference”type Call
Section titled “type Call”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
Section titled “type MockMigrationManager”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 (*MockMigrationManager) CallCount
Section titled “func (*MockMigrationManager) CallCount”func (m *MockMigrationManager) CallCount(method string) intCallCount returns how many calls were made to method.
func (*MockMigrationManager) Calls
Section titled “func (*MockMigrationManager) Calls”func (m *MockMigrationManager) Calls() []CallCalls returns a copy of the calls, in the order they were made.
func (*MockMigrationManager) Close
Section titled “func (*MockMigrationManager) Close”func (m *MockMigrationManager) Close(ctx context.Context) errorClose records the call and returns CloseFn’s result.
func (*MockMigrationManager) Down
Section titled “func (*MockMigrationManager) Down”func (m *MockMigrationManager) Down(n int) errorDown records the call and returns DownFn’s result.
func (*MockMigrationManager) DownContext
Section titled “func (*MockMigrationManager) DownContext”func (m *MockMigrationManager) DownContext(ctx context.Context, n int) errorDownContext records the call, with ctx and n, and returns DownFn’s result.
func (*MockMigrationManager) Init
Section titled “func (*MockMigrationManager) Init”func (m *MockMigrationManager) Init(ctx context.Context) errorInit records the call and returns InitFn’s result.
func (*MockMigrationManager) Name
Section titled “func (*MockMigrationManager) Name”func (m *MockMigrationManager) Name() stringName records the call and returns NameFn’s result.
func (*MockMigrationManager) Register
Section titled “func (*MockMigrationManager) Register”func (m *MockMigrationManager) Register(service gas.Service, migration gas.Migration)Register records the call and calls RegisterFn.
func (*MockMigrationManager) RegisterFS
Section titled “func (*MockMigrationManager) RegisterFS”func (m *MockMigrationManager) RegisterFS(service gas.Service, fsys fs.FS) errorRegisterFS 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 (*MockMigrationManager) Reset
Section titled “func (*MockMigrationManager) Reset”func (m *MockMigrationManager) Reset()Reset forgets every call.
func (*MockMigrationManager) RunPending
Section titled “func (*MockMigrationManager) RunPending”func (m *MockMigrationManager) RunPending() errorRunPending records the call and returns RunPendingFn’s result.
func (*MockMigrationManager) RunPendingContext
Section titled “func (*MockMigrationManager) RunPendingContext”func (m *MockMigrationManager) RunPendingContext(ctx context.Context) errorRunPendingContext records the call, with ctx, and returns RunPendingFn’s result.