-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unti tests for the right bound determination
- Loading branch information
1 parent
a31f8bb
commit 332fc68
Showing
3 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |