code.vegaprotocol.io/vega@v0.79.0/datanode/service/chain_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package service_test
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	"code.vegaprotocol.io/vega/datanode/service"
    25  	"code.vegaprotocol.io/vega/datanode/service/mocks"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestChainService(t *testing.T) {
    32  	ctrl := gomock.NewController(t)
    33  	ctx := context.Background()
    34  
    35  	store := mocks.NewMockChainStore(ctrl)
    36  	svc := service.NewChain(store)
    37  
    38  	t.Run("fetching unset chain", func(t *testing.T) {
    39  		// Should not be cached so expect another call to the store
    40  		store.EXPECT().Get(ctx).Return(entities.Chain{}, entities.ErrNotFound).Times(2)
    41  		for i := 0; i < 2; i++ {
    42  			chainID, err := svc.GetChainID()
    43  			assert.NoError(t, err)
    44  			assert.Equal(t, "", chainID)
    45  		}
    46  	})
    47  
    48  	t.Run("error when fetching chain", func(t *testing.T) {
    49  		ourError := errors.New("oops")
    50  		// should not be cached so expect another call to the store
    51  		store.EXPECT().Get(ctx).Return(entities.Chain{}, ourError).Times(2)
    52  		for i := 0; i < 2; i++ {
    53  			chainID, err := svc.GetChainID()
    54  			assert.ErrorIs(t, err, ourError)
    55  			assert.Equal(t, "", chainID)
    56  		}
    57  	})
    58  
    59  	t.Run("fetching already set chain", func(t *testing.T) {
    60  		// *should* be cached so do not expect another call to the store
    61  		store.EXPECT().Get(ctx).Return(entities.Chain{ID: "my-test-chain"}, nil)
    62  		for i := 0; i < 2; i++ {
    63  			chainID, err := svc.GetChainID()
    64  			assert.NoError(t, err)
    65  			assert.Equal(t, "my-test-chain", chainID)
    66  		}
    67  	})
    68  
    69  	t.Run("setting chain", func(t *testing.T) {
    70  		store.EXPECT().Set(ctx, entities.Chain{ID: "my-test-chain"}).Return(nil)
    71  		err := svc.SetChainID("my-test-chain")
    72  		assert.NoError(t, err)
    73  	})
    74  }