github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/account_local_id_generator_test.go (about)

     1  package environment_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/cadence/runtime/common"
    10  
    11  	"github.com/onflow/flow-go/fvm/environment"
    12  	envMock "github.com/onflow/flow-go/fvm/environment/mock"
    13  	"github.com/onflow/flow-go/fvm/tracing"
    14  	"github.com/onflow/flow-go/model/flow"
    15  )
    16  
    17  func Test_accountLocalIDGenerator_GenerateAccountID(t *testing.T) {
    18  	address, err := common.HexToAddress("0x1")
    19  	require.NoError(t, err)
    20  
    21  	t.Run("success", func(t *testing.T) {
    22  		meter := envMock.NewMeter(t)
    23  		meter.On(
    24  			"MeterComputation",
    25  			common.ComputationKind(environment.ComputationKindGenerateAccountLocalID),
    26  			uint(1),
    27  		).Return(nil)
    28  
    29  		accounts := envMock.NewAccounts(t)
    30  		accounts.On("GenerateAccountLocalID", flow.ConvertAddress(address)).
    31  			Return(uint64(1), nil)
    32  
    33  		generator := environment.NewAccountLocalIDGenerator(
    34  			tracing.NewMockTracerSpan(),
    35  			meter,
    36  			accounts,
    37  		)
    38  
    39  		id, err := generator.GenerateAccountID(address)
    40  		require.NoError(t, err)
    41  		require.Equal(t, uint64(1), id)
    42  	})
    43  	t.Run("error in meter", func(t *testing.T) {
    44  		expectedErr := errors.New("error in meter")
    45  
    46  		meter := envMock.NewMeter(t)
    47  		meter.On(
    48  			"MeterComputation",
    49  			common.ComputationKind(environment.ComputationKindGenerateAccountLocalID),
    50  			uint(1),
    51  		).Return(expectedErr)
    52  
    53  		accounts := envMock.NewAccounts(t)
    54  
    55  		generator := environment.NewAccountLocalIDGenerator(
    56  			tracing.NewMockTracerSpan(),
    57  			meter,
    58  			accounts,
    59  		)
    60  
    61  		_, err := generator.GenerateAccountID(address)
    62  		require.ErrorIs(t, err, expectedErr)
    63  	})
    64  	t.Run("err in accounts", func(t *testing.T) {
    65  		expectedErr := errors.New("error in accounts")
    66  
    67  		meter := envMock.NewMeter(t)
    68  		meter.On(
    69  			"MeterComputation",
    70  			common.ComputationKind(environment.ComputationKindGenerateAccountLocalID),
    71  			uint(1),
    72  		).Return(nil)
    73  
    74  		accounts := envMock.NewAccounts(t)
    75  		accounts.On("GenerateAccountLocalID", flow.ConvertAddress(address)).
    76  			Return(uint64(0), expectedErr)
    77  
    78  		generator := environment.NewAccountLocalIDGenerator(
    79  			tracing.NewMockTracerSpan(),
    80  			meter,
    81  			accounts,
    82  		)
    83  
    84  		_, err := generator.GenerateAccountID(address)
    85  		require.ErrorIs(t, err, expectedErr)
    86  	})
    87  }