Skip to content

Commit

Permalink
Add unti tests for the right bound determination
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Oct 3, 2023
1 parent a31f8bb commit 332fc68
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ func getBlockData(
// logProgress logs the backup progress
func logProgress(logger log.Logger, from, to, current uint64) {
total := to - from
progress := 100 * (float64(current) - float64(from)) / float64(total)
status := (float64(current) - float64(from)) / float64(total) * 100

logger.Info(
fmt.Sprintf("Total of %d blocks backed up", current-from+1),
"total", total,
"from", from,
"to", true,
"status", fmt.Sprintf("%.2f%%", progress),
"status", fmt.Sprintf("%.2f%%", status),
)
}
73 changes: 73 additions & 0 deletions backup/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package backup

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBackup_DetermineRightBound(t *testing.T) {
t.Parallel()

t.Run("unable to fetch latest block number", func(t *testing.T) {
t.Parallel()

var (
fetchErr = errors.New("unable to fetch latest height")
mockClient = &mockClient{
getLatestBlockNumberFn: func() (uint64, error) {
return 0, fetchErr
},
}
)

// Determine the right bound
_, err := determineRightBound(mockClient, nil)

assert.ErrorIs(t, err, fetchErr)
})

t.Run("excessive right range", func(t *testing.T) {
t.Parallel()

var (
chainLatest uint64 = 10
requestedTo = chainLatest + 10 // > chain latest

mockClient = &mockClient{
getLatestBlockNumberFn: func() (uint64, error) {
return chainLatest, nil
},
}
)

// Determine the right bound
rightBound, err := determineRightBound(mockClient, &requestedTo)
require.NoError(t, err)

assert.Equal(t, chainLatest, rightBound)
})

t.Run("valid right range", func(t *testing.T) {
t.Parallel()

var (
chainLatest uint64 = 10
requestedTo = chainLatest / 2 // < chain latest

mockClient = &mockClient{
getLatestBlockNumberFn: func() (uint64, error) {
return chainLatest, nil
},
}
)

// Determine the right bound
rightBound, err := determineRightBound(mockClient, &requestedTo)
require.NoError(t, err)

assert.Equal(t, requestedTo, rightBound)
})
}
29 changes: 29 additions & 0 deletions backup/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package backup

import "github.com/gnolang/gno/tm2/pkg/std"

type (
getLatestBlockNumberDelegate func() (uint64, error)
getBlockTransactionsDelegate func(uint64) ([]std.Tx, error)
)

type mockClient struct {
getLatestBlockNumberFn getLatestBlockNumberDelegate
getBlockTransactionsFn getBlockTransactionsDelegate
}

func (m *mockClient) GetLatestBlockNumber() (uint64, error) {
if m.getLatestBlockNumberFn != nil {
return m.getLatestBlockNumberFn()
}

return 0, nil
}

func (m *mockClient) GetBlockTransactions(blockNum uint64) ([]std.Tx, error) {
if m.getBlockTransactionsFn != nil {
return m.getBlockTransactionsFn(blockNum)
}

return nil, nil
}

0 comments on commit 332fc68

Please sign in to comment.