From fed1f89cb6c5bcac6f0e58cd3323b6d291ce062c Mon Sep 17 00:00:00 2001 From: pshapovalov Date: Sat, 9 Mar 2024 13:26:05 +0000 Subject: [PATCH] feat: txmanager mock --- Makefile | 8 +- go.mod | 3 + go.sum | 2 + pkg/pgtxmanager/mock/transactor_minimock.go | 292 ++++++++++++++++++++ pkg/pgtxmanager/mock/tx_manager_minimock.go | 291 +++++++++++++++++++ pkg/pgtxmanager/transaction.go | 4 + 6 files changed, 599 insertions(+), 1 deletion(-) create mode 100644 pkg/pgtxmanager/mock/transactor_minimock.go create mode 100644 pkg/pgtxmanager/mock/tx_manager_minimock.go diff --git a/Makefile b/Makefile index 54cccf4..87a07a7 100644 --- a/Makefile +++ b/Makefile @@ -6,4 +6,10 @@ install-lint: lint: GOBIN=$(LOCAL_BIN) golangci-lint run ./... --config .golangci.pipeline.yaml - GOBIN=$(LOCAL_BIN) govulncheck ./... \ No newline at end of file + GOBIN=$(LOCAL_BIN) govulncheck ./... + +install-deps: + go install github.com/gojuno/minimock/v3/cmd/minimock@v3.3.6 + +mock-generate: + go generate ./... \ No newline at end of file diff --git a/go.mod b/go.mod index 4c46272..c640e3f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,10 @@ go 1.22.0 require github.com/jackc/pgx/v5 v5.5.4 require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gojuno/minimock/v3 v3.3.6 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/sync v0.1.0 // indirect ) diff --git a/go.sum b/go.sum index fd7ef85..5e45394 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/georgysavva/scany/v2 v2.1.0 h1:jEAX+yPQ2AAtnv0WJzAYlgsM/KzvwbD6BjSjLI github.com/georgysavva/scany/v2 v2.1.0/go.mod h1:fqp9yHZzM/PFVa3/rYEC57VmDx+KDch0LoqrJzkvtos= github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= +github.com/gojuno/minimock/v3 v3.3.6 h1:tZQQaDgKSxsKiVia9vt6zZ/qsKNGBw2D0ubHQPr+mHc= +github.com/gojuno/minimock/v3 v3.3.6/go.mod h1:kjvubEBVT8aUQ9e+g8x/hPfAhiOoqW7WinzzJgzr4ws= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= diff --git a/pkg/pgtxmanager/mock/transactor_minimock.go b/pkg/pgtxmanager/mock/transactor_minimock.go new file mode 100644 index 0000000..bbe9946 --- /dev/null +++ b/pkg/pgtxmanager/mock/transactor_minimock.go @@ -0,0 +1,292 @@ +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +package mock + +//go:generate minimock -i github.com/pillarion/practice-platform/pkg/pgtxmanager.Transactor -o transactor_minimock.go -n TransactorMock -p mock + +import ( + "context" + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" + "github.com/jackc/pgx/v5" +) + +// TransactorMock implements pgtxmanager.Transactor +type TransactorMock struct { + t minimock.Tester + finishOnce sync.Once + + funcBeginTx func(ctx context.Context, txOptions pgx.TxOptions) (t1 pgx.Tx, err error) + inspectFuncBeginTx func(ctx context.Context, txOptions pgx.TxOptions) + afterBeginTxCounter uint64 + beforeBeginTxCounter uint64 + BeginTxMock mTransactorMockBeginTx +} + +// NewTransactorMock returns a mock for pgtxmanager.Transactor +func NewTransactorMock(t minimock.Tester) *TransactorMock { + m := &TransactorMock{t: t} + + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.BeginTxMock = mTransactorMockBeginTx{mock: m} + m.BeginTxMock.callArgs = []*TransactorMockBeginTxParams{} + + t.Cleanup(m.MinimockFinish) + + return m +} + +type mTransactorMockBeginTx struct { + mock *TransactorMock + defaultExpectation *TransactorMockBeginTxExpectation + expectations []*TransactorMockBeginTxExpectation + + callArgs []*TransactorMockBeginTxParams + mutex sync.RWMutex +} + +// TransactorMockBeginTxExpectation specifies expectation struct of the Transactor.BeginTx +type TransactorMockBeginTxExpectation struct { + mock *TransactorMock + params *TransactorMockBeginTxParams + results *TransactorMockBeginTxResults + Counter uint64 +} + +// TransactorMockBeginTxParams contains parameters of the Transactor.BeginTx +type TransactorMockBeginTxParams struct { + ctx context.Context + txOptions pgx.TxOptions +} + +// TransactorMockBeginTxResults contains results of the Transactor.BeginTx +type TransactorMockBeginTxResults struct { + t1 pgx.Tx + err error +} + +// Expect sets up expected params for Transactor.BeginTx +func (mmBeginTx *mTransactorMockBeginTx) Expect(ctx context.Context, txOptions pgx.TxOptions) *mTransactorMockBeginTx { + if mmBeginTx.mock.funcBeginTx != nil { + mmBeginTx.mock.t.Fatalf("TransactorMock.BeginTx mock is already set by Set") + } + + if mmBeginTx.defaultExpectation == nil { + mmBeginTx.defaultExpectation = &TransactorMockBeginTxExpectation{} + } + + mmBeginTx.defaultExpectation.params = &TransactorMockBeginTxParams{ctx, txOptions} + for _, e := range mmBeginTx.expectations { + if minimock.Equal(e.params, mmBeginTx.defaultExpectation.params) { + mmBeginTx.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmBeginTx.defaultExpectation.params) + } + } + + return mmBeginTx +} + +// Inspect accepts an inspector function that has same arguments as the Transactor.BeginTx +func (mmBeginTx *mTransactorMockBeginTx) Inspect(f func(ctx context.Context, txOptions pgx.TxOptions)) *mTransactorMockBeginTx { + if mmBeginTx.mock.inspectFuncBeginTx != nil { + mmBeginTx.mock.t.Fatalf("Inspect function is already set for TransactorMock.BeginTx") + } + + mmBeginTx.mock.inspectFuncBeginTx = f + + return mmBeginTx +} + +// Return sets up results that will be returned by Transactor.BeginTx +func (mmBeginTx *mTransactorMockBeginTx) Return(t1 pgx.Tx, err error) *TransactorMock { + if mmBeginTx.mock.funcBeginTx != nil { + mmBeginTx.mock.t.Fatalf("TransactorMock.BeginTx mock is already set by Set") + } + + if mmBeginTx.defaultExpectation == nil { + mmBeginTx.defaultExpectation = &TransactorMockBeginTxExpectation{mock: mmBeginTx.mock} + } + mmBeginTx.defaultExpectation.results = &TransactorMockBeginTxResults{t1, err} + return mmBeginTx.mock +} + +// Set uses given function f to mock the Transactor.BeginTx method +func (mmBeginTx *mTransactorMockBeginTx) Set(f func(ctx context.Context, txOptions pgx.TxOptions) (t1 pgx.Tx, err error)) *TransactorMock { + if mmBeginTx.defaultExpectation != nil { + mmBeginTx.mock.t.Fatalf("Default expectation is already set for the Transactor.BeginTx method") + } + + if len(mmBeginTx.expectations) > 0 { + mmBeginTx.mock.t.Fatalf("Some expectations are already set for the Transactor.BeginTx method") + } + + mmBeginTx.mock.funcBeginTx = f + return mmBeginTx.mock +} + +// When sets expectation for the Transactor.BeginTx which will trigger the result defined by the following +// Then helper +func (mmBeginTx *mTransactorMockBeginTx) When(ctx context.Context, txOptions pgx.TxOptions) *TransactorMockBeginTxExpectation { + if mmBeginTx.mock.funcBeginTx != nil { + mmBeginTx.mock.t.Fatalf("TransactorMock.BeginTx mock is already set by Set") + } + + expectation := &TransactorMockBeginTxExpectation{ + mock: mmBeginTx.mock, + params: &TransactorMockBeginTxParams{ctx, txOptions}, + } + mmBeginTx.expectations = append(mmBeginTx.expectations, expectation) + return expectation +} + +// Then sets up Transactor.BeginTx return parameters for the expectation previously defined by the When method +func (e *TransactorMockBeginTxExpectation) Then(t1 pgx.Tx, err error) *TransactorMock { + e.results = &TransactorMockBeginTxResults{t1, err} + return e.mock +} + +// BeginTx implements pgtxmanager.Transactor +func (mmBeginTx *TransactorMock) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (t1 pgx.Tx, err error) { + mm_atomic.AddUint64(&mmBeginTx.beforeBeginTxCounter, 1) + defer mm_atomic.AddUint64(&mmBeginTx.afterBeginTxCounter, 1) + + if mmBeginTx.inspectFuncBeginTx != nil { + mmBeginTx.inspectFuncBeginTx(ctx, txOptions) + } + + mm_params := TransactorMockBeginTxParams{ctx, txOptions} + + // Record call args + mmBeginTx.BeginTxMock.mutex.Lock() + mmBeginTx.BeginTxMock.callArgs = append(mmBeginTx.BeginTxMock.callArgs, &mm_params) + mmBeginTx.BeginTxMock.mutex.Unlock() + + for _, e := range mmBeginTx.BeginTxMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return e.results.t1, e.results.err + } + } + + if mmBeginTx.BeginTxMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmBeginTx.BeginTxMock.defaultExpectation.Counter, 1) + mm_want := mmBeginTx.BeginTxMock.defaultExpectation.params + mm_got := TransactorMockBeginTxParams{ctx, txOptions} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmBeginTx.t.Errorf("TransactorMock.BeginTx got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + mm_results := mmBeginTx.BeginTxMock.defaultExpectation.results + if mm_results == nil { + mmBeginTx.t.Fatal("No results are set for the TransactorMock.BeginTx") + } + return (*mm_results).t1, (*mm_results).err + } + if mmBeginTx.funcBeginTx != nil { + return mmBeginTx.funcBeginTx(ctx, txOptions) + } + mmBeginTx.t.Fatalf("Unexpected call to TransactorMock.BeginTx. %v %v", ctx, txOptions) + return +} + +// BeginTxAfterCounter returns a count of finished TransactorMock.BeginTx invocations +func (mmBeginTx *TransactorMock) BeginTxAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmBeginTx.afterBeginTxCounter) +} + +// BeginTxBeforeCounter returns a count of TransactorMock.BeginTx invocations +func (mmBeginTx *TransactorMock) BeginTxBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmBeginTx.beforeBeginTxCounter) +} + +// Calls returns a list of arguments used in each call to TransactorMock.BeginTx. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmBeginTx *mTransactorMockBeginTx) Calls() []*TransactorMockBeginTxParams { + mmBeginTx.mutex.RLock() + + argCopy := make([]*TransactorMockBeginTxParams, len(mmBeginTx.callArgs)) + copy(argCopy, mmBeginTx.callArgs) + + mmBeginTx.mutex.RUnlock() + + return argCopy +} + +// MinimockBeginTxDone returns true if the count of the BeginTx invocations corresponds +// the number of defined expectations +func (m *TransactorMock) MinimockBeginTxDone() bool { + for _, e := range m.BeginTxMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.BeginTxMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterBeginTxCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcBeginTx != nil && mm_atomic.LoadUint64(&m.afterBeginTxCounter) < 1 { + return false + } + return true +} + +// MinimockBeginTxInspect logs each unmet expectation +func (m *TransactorMock) MinimockBeginTxInspect() { + for _, e := range m.BeginTxMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TransactorMock.BeginTx with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.BeginTxMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterBeginTxCounter) < 1 { + if m.BeginTxMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TransactorMock.BeginTx") + } else { + m.t.Errorf("Expected call to TransactorMock.BeginTx with params: %#v", *m.BeginTxMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcBeginTx != nil && mm_atomic.LoadUint64(&m.afterBeginTxCounter) < 1 { + m.t.Error("Expected call to TransactorMock.BeginTx") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *TransactorMock) MinimockFinish() { + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockBeginTxInspect() + m.t.FailNow() + } + }) +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *TransactorMock) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *TransactorMock) minimockDone() bool { + done := true + return done && + m.MinimockBeginTxDone() +} diff --git a/pkg/pgtxmanager/mock/tx_manager_minimock.go b/pkg/pgtxmanager/mock/tx_manager_minimock.go new file mode 100644 index 0000000..1f3b37f --- /dev/null +++ b/pkg/pgtxmanager/mock/tx_manager_minimock.go @@ -0,0 +1,291 @@ +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +package mock + +//go:generate minimock -i github.com/pillarion/practice-platform/pkg/pgtxmanager.TxManager -o tx_manager_minimock.go -n TxManagerMock -p mock + +import ( + "context" + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" + mm_pgtxmanager "github.com/pillarion/practice-platform/pkg/pgtxmanager" +) + +// TxManagerMock implements pgtxmanager.TxManager +type TxManagerMock struct { + t minimock.Tester + finishOnce sync.Once + + funcReadCommitted func(ctx context.Context, f mm_pgtxmanager.Handler) (err error) + inspectFuncReadCommitted func(ctx context.Context, f mm_pgtxmanager.Handler) + afterReadCommittedCounter uint64 + beforeReadCommittedCounter uint64 + ReadCommittedMock mTxManagerMockReadCommitted +} + +// NewTxManagerMock returns a mock for pgtxmanager.TxManager +func NewTxManagerMock(t minimock.Tester) *TxManagerMock { + m := &TxManagerMock{t: t} + + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.ReadCommittedMock = mTxManagerMockReadCommitted{mock: m} + m.ReadCommittedMock.callArgs = []*TxManagerMockReadCommittedParams{} + + t.Cleanup(m.MinimockFinish) + + return m +} + +type mTxManagerMockReadCommitted struct { + mock *TxManagerMock + defaultExpectation *TxManagerMockReadCommittedExpectation + expectations []*TxManagerMockReadCommittedExpectation + + callArgs []*TxManagerMockReadCommittedParams + mutex sync.RWMutex +} + +// TxManagerMockReadCommittedExpectation specifies expectation struct of the TxManager.ReadCommitted +type TxManagerMockReadCommittedExpectation struct { + mock *TxManagerMock + params *TxManagerMockReadCommittedParams + results *TxManagerMockReadCommittedResults + Counter uint64 +} + +// TxManagerMockReadCommittedParams contains parameters of the TxManager.ReadCommitted +type TxManagerMockReadCommittedParams struct { + ctx context.Context + f mm_pgtxmanager.Handler +} + +// TxManagerMockReadCommittedResults contains results of the TxManager.ReadCommitted +type TxManagerMockReadCommittedResults struct { + err error +} + +// Expect sets up expected params for TxManager.ReadCommitted +func (mmReadCommitted *mTxManagerMockReadCommitted) Expect(ctx context.Context, f mm_pgtxmanager.Handler) *mTxManagerMockReadCommitted { + if mmReadCommitted.mock.funcReadCommitted != nil { + mmReadCommitted.mock.t.Fatalf("TxManagerMock.ReadCommitted mock is already set by Set") + } + + if mmReadCommitted.defaultExpectation == nil { + mmReadCommitted.defaultExpectation = &TxManagerMockReadCommittedExpectation{} + } + + mmReadCommitted.defaultExpectation.params = &TxManagerMockReadCommittedParams{ctx, f} + for _, e := range mmReadCommitted.expectations { + if minimock.Equal(e.params, mmReadCommitted.defaultExpectation.params) { + mmReadCommitted.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmReadCommitted.defaultExpectation.params) + } + } + + return mmReadCommitted +} + +// Inspect accepts an inspector function that has same arguments as the TxManager.ReadCommitted +func (mmReadCommitted *mTxManagerMockReadCommitted) Inspect(f func(ctx context.Context, f mm_pgtxmanager.Handler)) *mTxManagerMockReadCommitted { + if mmReadCommitted.mock.inspectFuncReadCommitted != nil { + mmReadCommitted.mock.t.Fatalf("Inspect function is already set for TxManagerMock.ReadCommitted") + } + + mmReadCommitted.mock.inspectFuncReadCommitted = f + + return mmReadCommitted +} + +// Return sets up results that will be returned by TxManager.ReadCommitted +func (mmReadCommitted *mTxManagerMockReadCommitted) Return(err error) *TxManagerMock { + if mmReadCommitted.mock.funcReadCommitted != nil { + mmReadCommitted.mock.t.Fatalf("TxManagerMock.ReadCommitted mock is already set by Set") + } + + if mmReadCommitted.defaultExpectation == nil { + mmReadCommitted.defaultExpectation = &TxManagerMockReadCommittedExpectation{mock: mmReadCommitted.mock} + } + mmReadCommitted.defaultExpectation.results = &TxManagerMockReadCommittedResults{err} + return mmReadCommitted.mock +} + +// Set uses given function f to mock the TxManager.ReadCommitted method +func (mmReadCommitted *mTxManagerMockReadCommitted) Set(f func(ctx context.Context, f mm_pgtxmanager.Handler) (err error)) *TxManagerMock { + if mmReadCommitted.defaultExpectation != nil { + mmReadCommitted.mock.t.Fatalf("Default expectation is already set for the TxManager.ReadCommitted method") + } + + if len(mmReadCommitted.expectations) > 0 { + mmReadCommitted.mock.t.Fatalf("Some expectations are already set for the TxManager.ReadCommitted method") + } + + mmReadCommitted.mock.funcReadCommitted = f + return mmReadCommitted.mock +} + +// When sets expectation for the TxManager.ReadCommitted which will trigger the result defined by the following +// Then helper +func (mmReadCommitted *mTxManagerMockReadCommitted) When(ctx context.Context, f mm_pgtxmanager.Handler) *TxManagerMockReadCommittedExpectation { + if mmReadCommitted.mock.funcReadCommitted != nil { + mmReadCommitted.mock.t.Fatalf("TxManagerMock.ReadCommitted mock is already set by Set") + } + + expectation := &TxManagerMockReadCommittedExpectation{ + mock: mmReadCommitted.mock, + params: &TxManagerMockReadCommittedParams{ctx, f}, + } + mmReadCommitted.expectations = append(mmReadCommitted.expectations, expectation) + return expectation +} + +// Then sets up TxManager.ReadCommitted return parameters for the expectation previously defined by the When method +func (e *TxManagerMockReadCommittedExpectation) Then(err error) *TxManagerMock { + e.results = &TxManagerMockReadCommittedResults{err} + return e.mock +} + +// ReadCommitted implements pgtxmanager.TxManager +func (mmReadCommitted *TxManagerMock) ReadCommitted(ctx context.Context, f mm_pgtxmanager.Handler) (err error) { + mm_atomic.AddUint64(&mmReadCommitted.beforeReadCommittedCounter, 1) + defer mm_atomic.AddUint64(&mmReadCommitted.afterReadCommittedCounter, 1) + + if mmReadCommitted.inspectFuncReadCommitted != nil { + mmReadCommitted.inspectFuncReadCommitted(ctx, f) + } + + mm_params := TxManagerMockReadCommittedParams{ctx, f} + + // Record call args + mmReadCommitted.ReadCommittedMock.mutex.Lock() + mmReadCommitted.ReadCommittedMock.callArgs = append(mmReadCommitted.ReadCommittedMock.callArgs, &mm_params) + mmReadCommitted.ReadCommittedMock.mutex.Unlock() + + for _, e := range mmReadCommitted.ReadCommittedMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return e.results.err + } + } + + if mmReadCommitted.ReadCommittedMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmReadCommitted.ReadCommittedMock.defaultExpectation.Counter, 1) + mm_want := mmReadCommitted.ReadCommittedMock.defaultExpectation.params + mm_got := TxManagerMockReadCommittedParams{ctx, f} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmReadCommitted.t.Errorf("TxManagerMock.ReadCommitted got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + mm_results := mmReadCommitted.ReadCommittedMock.defaultExpectation.results + if mm_results == nil { + mmReadCommitted.t.Fatal("No results are set for the TxManagerMock.ReadCommitted") + } + return (*mm_results).err + } + if mmReadCommitted.funcReadCommitted != nil { + return mmReadCommitted.funcReadCommitted(ctx, f) + } + mmReadCommitted.t.Fatalf("Unexpected call to TxManagerMock.ReadCommitted. %v %v", ctx, f) + return +} + +// ReadCommittedAfterCounter returns a count of finished TxManagerMock.ReadCommitted invocations +func (mmReadCommitted *TxManagerMock) ReadCommittedAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmReadCommitted.afterReadCommittedCounter) +} + +// ReadCommittedBeforeCounter returns a count of TxManagerMock.ReadCommitted invocations +func (mmReadCommitted *TxManagerMock) ReadCommittedBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmReadCommitted.beforeReadCommittedCounter) +} + +// Calls returns a list of arguments used in each call to TxManagerMock.ReadCommitted. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmReadCommitted *mTxManagerMockReadCommitted) Calls() []*TxManagerMockReadCommittedParams { + mmReadCommitted.mutex.RLock() + + argCopy := make([]*TxManagerMockReadCommittedParams, len(mmReadCommitted.callArgs)) + copy(argCopy, mmReadCommitted.callArgs) + + mmReadCommitted.mutex.RUnlock() + + return argCopy +} + +// MinimockReadCommittedDone returns true if the count of the ReadCommitted invocations corresponds +// the number of defined expectations +func (m *TxManagerMock) MinimockReadCommittedDone() bool { + for _, e := range m.ReadCommittedMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.ReadCommittedMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterReadCommittedCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcReadCommitted != nil && mm_atomic.LoadUint64(&m.afterReadCommittedCounter) < 1 { + return false + } + return true +} + +// MinimockReadCommittedInspect logs each unmet expectation +func (m *TxManagerMock) MinimockReadCommittedInspect() { + for _, e := range m.ReadCommittedMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TxManagerMock.ReadCommitted with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.ReadCommittedMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterReadCommittedCounter) < 1 { + if m.ReadCommittedMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TxManagerMock.ReadCommitted") + } else { + m.t.Errorf("Expected call to TxManagerMock.ReadCommitted with params: %#v", *m.ReadCommittedMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcReadCommitted != nil && mm_atomic.LoadUint64(&m.afterReadCommittedCounter) < 1 { + m.t.Error("Expected call to TxManagerMock.ReadCommitted") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *TxManagerMock) MinimockFinish() { + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockReadCommittedInspect() + m.t.FailNow() + } + }) +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *TxManagerMock) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *TxManagerMock) minimockDone() bool { + done := true + return done && + m.MinimockReadCommittedDone() +} diff --git a/pkg/pgtxmanager/transaction.go b/pkg/pgtxmanager/transaction.go index 6f34b64..c1179bc 100644 --- a/pkg/pgtxmanager/transaction.go +++ b/pkg/pgtxmanager/transaction.go @@ -7,6 +7,8 @@ import ( ) // TxManager represents a transaction manager +// +//go:generate minimock -o mock/ -s "_minimock.go" type TxManager interface { ReadCommitted(ctx context.Context, f Handler) error } @@ -15,6 +17,8 @@ type TxManager interface { type Handler func(ctx context.Context) error // Transactor represents a transactor +// +//go:generate minimock -o mock/ -s "_minimock.go" type Transactor interface { BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) }