code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/snapshot_data_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 sqlstore_test
    17  
    18  import (
    19  	"context"
    20  	"encoding/hex"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func addSnapshot(t *testing.T, ctx context.Context, ss *sqlstore.CoreSnapshotData, bs *sqlstore.Blocks, entity entities.CoreSnapshotData) {
    30  	t.Helper()
    31  	block := addTestBlock(t, ctx, bs)
    32  	entity.VegaTime = block.VegaTime
    33  	entity.BlockHash = hex.EncodeToString(block.Hash)
    34  	entity.TxHash = generateTxHash()
    35  	require.NoError(t, ss.Add(ctx, entity))
    36  }
    37  
    38  func TestGetSnapshots(t *testing.T) {
    39  	ctx := tempTransaction(t)
    40  
    41  	ss := sqlstore.NewCoreSnapshotData(connectionSource)
    42  	bs := sqlstore.NewBlocks(connectionSource)
    43  
    44  	addSnapshot(t, ctx, ss, bs, entities.CoreSnapshotData{BlockHeight: 100, VegaCoreVersion: "v0.65.0"})
    45  
    46  	var rowCount int
    47  	err := connectionSource.QueryRow(ctx, `select count(*) from core_snapshots`).Scan(&rowCount)
    48  	require.NoError(t, err)
    49  	require.Equal(t, 1, rowCount)
    50  
    51  	entities, _, err := ss.List(ctx, entities.DefaultCursorPagination(true))
    52  	require.NoError(t, err)
    53  	require.Equal(t, 1, len(entities))
    54  	require.Equal(t, uint64(100), entities[0].BlockHeight)
    55  	require.Equal(t, "v0.65.0", entities[0].VegaCoreVersion)
    56  }