code.vegaprotocol.io/vega@v0.79.0/core/parties/helpers_for_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 parties_test
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	bmocks "code.vegaprotocol.io/vega/core/broker/mocks"
    23  	"code.vegaprotocol.io/vega/core/events"
    24  	"code.vegaprotocol.io/vega/core/integration/stubs"
    25  	"code.vegaprotocol.io/vega/core/parties"
    26  	"code.vegaprotocol.io/vega/core/snapshot"
    27  	"code.vegaprotocol.io/vega/core/stats"
    28  	"code.vegaprotocol.io/vega/core/types"
    29  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    30  	"code.vegaprotocol.io/vega/logging"
    31  	"code.vegaprotocol.io/vega/paths"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/stretchr/testify/assert"
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  type testEngine struct {
    39  	engine *parties.SnapshottedEngine
    40  	broker *bmocks.MockBroker
    41  }
    42  
    43  func assertEqualProfiles(t *testing.T, expected, actual []types.PartyProfile) {
    44  	t.Helper()
    45  
    46  	parties.SortByPartyID(expected)
    47  	parties.SortByPartyID(actual)
    48  
    49  	assert.Equal(t, expected, actual)
    50  }
    51  
    52  func expectPartyProfileUpdatedEvent(t *testing.T, engine *testEngine) {
    53  	t.Helper()
    54  
    55  	engine.broker.EXPECT().Send(gomock.Any()).Do(func(evt events.Event) {
    56  		_, ok := evt.(*events.PartyProfileUpdated)
    57  		assert.True(t, ok, "Event should be a PartyProfileUpdated, but is %T", evt)
    58  	}).Times(1)
    59  }
    60  
    61  func newSnapshotEngine(t *testing.T, vegaPath paths.Paths, now time.Time, engine *parties.SnapshottedEngine) *snapshot.Engine {
    62  	t.Helper()
    63  
    64  	log := logging.NewTestLogger()
    65  	timeService := stubs.NewTimeStub()
    66  	timeService.SetTime(now)
    67  	statsData := stats.New(log, stats.NewDefaultConfig())
    68  	config := snapshot.DefaultConfig()
    69  
    70  	snapshotEngine, err := snapshot.NewEngine(vegaPath, config, log, timeService, statsData.Blockchain)
    71  	require.NoError(t, err)
    72  
    73  	snapshotEngine.AddProviders(engine)
    74  
    75  	return snapshotEngine
    76  }
    77  
    78  func newEngine(t *testing.T) *testEngine {
    79  	t.Helper()
    80  
    81  	ctrl := gomock.NewController(t)
    82  
    83  	broker := bmocks.NewMockBroker(ctrl)
    84  
    85  	engine := parties.NewSnapshottedEngine(broker)
    86  
    87  	return &testEngine{
    88  		engine: engine,
    89  		broker: broker,
    90  	}
    91  }
    92  
    93  func newPartyID(t *testing.T) types.PartyID {
    94  	t.Helper()
    95  
    96  	return types.PartyID(vgrand.RandomStr(5))
    97  }