From 1fd1576d03674be730b4e2736b1d7db1bcca5eea Mon Sep 17 00:00:00 2001 From: Andrew Richardson Date: Thu, 3 Feb 2022 15:49:13 -0500 Subject: [PATCH] Add missing test coverage in txcommon Signed-off-by: Andrew Richardson --- internal/txcommon/txcommon.go | 4 +- internal/txcommon/txcommon_test.go | 92 ++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/internal/txcommon/txcommon.go b/internal/txcommon/txcommon.go index 6f5e64d941..28a10805a6 100644 --- a/internal/txcommon/txcommon.go +++ b/internal/txcommon/txcommon.go @@ -102,8 +102,8 @@ func (t *transactionHelper) PersistTransaction(ctx context.Context, ns string, i return true, nil } -// AddBlockchainTX is called when we know the tranasction should exist, and we don't need any validation -// just want to bolt an extra blockchain TXID on - if it's not there already. +// AddBlockchainTX is called when we know the transaction should exist, and we don't need any validation +// but just want to bolt on an extra blockchain TXID (if it's not there already). func (t *transactionHelper) AddBlockchainTX(ctx context.Context, id *fftypes.UUID, blockchainTXID string) error { tx, err := t.database.GetTransactionByID(ctx, id) diff --git a/internal/txcommon/txcommon_test.go b/internal/txcommon/txcommon_test.go index ea6c161d35..c57f1945e8 100644 --- a/internal/txcommon/txcommon_test.go +++ b/internal/txcommon/txcommon_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/hyperledger/firefly/mocks/databasemocks" + "github.com/hyperledger/firefly/pkg/database" "github.com/hyperledger/firefly/pkg/fftypes" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -283,3 +284,94 @@ func TestPersistTransactionExistingMismatchType(t *testing.T) { mdi.AssertExpectations(t) } + +func TestAddBlockchainTX(t *testing.T) { + + mdi := &databasemocks.Plugin{} + txHelper := NewTransactionHelper(mdi) + ctx := context.Background() + + txid := fftypes.NewUUID() + mdi.On("GetTransactionByID", ctx, txid).Return(&fftypes.Transaction{ + ID: txid, + Namespace: "ns1", + Type: fftypes.TransactionTypeContractInvoke, + Created: fftypes.Now(), + BlockchainIDs: fftypes.FFStringArray{"0x111111"}, + }, nil) + mdi.On("UpdateTransaction", ctx, txid, mock.MatchedBy(func(u database.Update) bool { + info, _ := u.Finalize() + assert.Equal(t, 1, len(info.SetOperations)) + assert.Equal(t, "blockchainids", info.SetOperations[0].Field) + val, _ := info.SetOperations[0].Value.Value() + assert.Equal(t, "0x111111,abc", val) + return true + })).Return(nil) + + err := txHelper.AddBlockchainTX(ctx, txid, "abc") + assert.NoError(t, err) + + mdi.AssertExpectations(t) + +} + +func TestAddBlockchainTXGetFail(t *testing.T) { + + mdi := &databasemocks.Plugin{} + txHelper := NewTransactionHelper(mdi) + ctx := context.Background() + + txid := fftypes.NewUUID() + mdi.On("GetTransactionByID", ctx, txid).Return(nil, fmt.Errorf("pop")) + + err := txHelper.AddBlockchainTX(ctx, txid, "abc") + assert.EqualError(t, err, "pop") + + mdi.AssertExpectations(t) + +} + +func TestAddBlockchainTXUpdateFail(t *testing.T) { + + mdi := &databasemocks.Plugin{} + txHelper := NewTransactionHelper(mdi) + ctx := context.Background() + + txid := fftypes.NewUUID() + mdi.On("GetTransactionByID", ctx, txid).Return(&fftypes.Transaction{ + ID: txid, + Namespace: "ns1", + Type: fftypes.TransactionTypeContractInvoke, + Created: fftypes.Now(), + BlockchainIDs: fftypes.FFStringArray{"0x111111"}, + }, nil) + mdi.On("UpdateTransaction", ctx, txid, mock.Anything).Return(fmt.Errorf("pop")) + + err := txHelper.AddBlockchainTX(ctx, txid, "abc") + assert.EqualError(t, err, "pop") + + mdi.AssertExpectations(t) + +} + +func TestAddBlockchainTXUnchanged(t *testing.T) { + + mdi := &databasemocks.Plugin{} + txHelper := NewTransactionHelper(mdi) + ctx := context.Background() + + txid := fftypes.NewUUID() + mdi.On("GetTransactionByID", ctx, txid).Return(&fftypes.Transaction{ + ID: txid, + Namespace: "ns1", + Type: fftypes.TransactionTypeContractInvoke, + Created: fftypes.Now(), + BlockchainIDs: fftypes.FFStringArray{"0x111111"}, + }, nil) + + err := txHelper.AddBlockchainTX(ctx, txid, "0x111111") + assert.NoError(t, err) + + mdi.AssertExpectations(t) + +}