github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/jsonrpc/dbtxmanager_test.go (about)

     1  package jsonrpc
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/0xPolygon/supernets2-node/jsonrpc/mocks"
     9  	"github.com/0xPolygon/supernets2-node/jsonrpc/types"
    10  	"github.com/jackc/pgx/v4"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestNewDbTxScope(t *testing.T) {
    15  	type testCase struct {
    16  		Name           string
    17  		Fn             DBTxScopedFn
    18  		ExpectedResult interface{}
    19  		ExpectedError  types.Error
    20  		SetupMocks     func(s *mocks.StateMock, d *mocks.DBTxMock)
    21  	}
    22  
    23  	testCases := []testCase{
    24  		{
    25  			Name: "Run scoped func commits DB tx",
    26  			Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
    27  				return 1, nil
    28  			},
    29  			ExpectedResult: 1,
    30  			ExpectedError:  nil,
    31  			SetupMocks: func(s *mocks.StateMock, d *mocks.DBTxMock) {
    32  				d.On("Commit", context.Background()).Return(nil).Once()
    33  				s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
    34  			},
    35  		},
    36  		{
    37  			Name: "Run scoped func rollbacks DB tx",
    38  			Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
    39  				return nil, types.NewRPCError(types.DefaultErrorCode, "func returned an error")
    40  			},
    41  			ExpectedResult: nil,
    42  			ExpectedError:  types.NewRPCError(types.DefaultErrorCode, "func returned an error"),
    43  			SetupMocks: func(s *mocks.StateMock, d *mocks.DBTxMock) {
    44  				d.On("Rollback", context.Background()).Return(nil).Once()
    45  				s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
    46  			},
    47  		},
    48  		{
    49  			Name: "Run scoped func but fails create a db tx",
    50  			Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
    51  				return nil, nil
    52  			},
    53  			ExpectedResult: nil,
    54  			ExpectedError:  types.NewRPCError(types.DefaultErrorCode, "failed to connect to the state"),
    55  			SetupMocks: func(s *mocks.StateMock, d *mocks.DBTxMock) {
    56  				s.On("BeginStateTransaction", context.Background()).Return(nil, errors.New("failed to create db tx")).Once()
    57  			},
    58  		},
    59  		{
    60  			Name: "Run scoped func but fails to commit DB tx",
    61  			Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
    62  				return 1, nil
    63  			},
    64  			ExpectedResult: nil,
    65  			ExpectedError:  types.NewRPCError(types.DefaultErrorCode, "failed to commit db transaction"),
    66  			SetupMocks: func(s *mocks.StateMock, d *mocks.DBTxMock) {
    67  				d.On("Commit", context.Background()).Return(errors.New("failed to commit db tx")).Once()
    68  				s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
    69  			},
    70  		},
    71  		{
    72  			Name: "Run scoped func but fails to rollbacks DB tx",
    73  			Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
    74  				return nil, types.NewRPCError(types.DefaultErrorCode, "func returned an error")
    75  			},
    76  			ExpectedResult: nil,
    77  			ExpectedError:  types.NewRPCError(types.DefaultErrorCode, "failed to rollback db transaction"),
    78  			SetupMocks: func(s *mocks.StateMock, d *mocks.DBTxMock) {
    79  				d.On("Rollback", context.Background()).Return(errors.New("failed to rollback db tx")).Once()
    80  				s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
    81  			},
    82  		},
    83  	}
    84  
    85  	dbTxManager := DBTxManager{}
    86  	s := mocks.NewStateMock(t)
    87  	d := mocks.NewDBTxMock(t)
    88  
    89  	for _, testCase := range testCases {
    90  		t.Run(testCase.Name, func(t *testing.T) {
    91  			tc := testCase
    92  			tc.SetupMocks(s, d)
    93  
    94  			result, err := dbTxManager.NewDbTxScope(s, tc.Fn)
    95  			assert.Equal(t, tc.ExpectedResult, result)
    96  			assert.Equal(t, tc.ExpectedError, err)
    97  		})
    98  	}
    99  }