feat: Add unit test for rolldpos protocol
* feat: Add unit test for rolldpos protocol
Issue
Closes
* Update epoch_test.go
* Update epoch_test.go
* Update epoch_test.go
@@ -2,10 +2,10 @@ package rolldpos
import (
"context"
+ "errors"
+ "github.com/stretchr/testify/require"
"strconv"
"testing"
-
- "github.com/stretchr/testify/require"
)
func TestEnableDardanellesSubEpoch(t *testing.T) {
@@ -238,3 +238,63 @@ func TestGetSubEpochNum(t *testing.T) {
require.Equal(expectedSubEpochNums[i], subEpochNum)
}
}
+
+func productivity(epochStartHeight uint64, epochEndHeight uint64) (map[string]uint64, error) {
+ if epochStartHeight == 0 || epochEndHeight == 0 {
+ return nil, errors.New("productivity error")
+ }
+ return map[string]uint64{"ret": 0}, nil
+}
+
+func TestProductivityByEpoch(t *testing.T) {
+ require := require.New(t)
+ p := NewProtocol(23, 4, 3)
+
+ t.Run("normal call", func(t *testing.T) {
+ epochNum := uint64(1)
+ tipHeight := uint64(1)
+ expectedHeights := uint64(1)
+ expectedProduces := map[string]uint64{"ret": 0}
+ retHeight, retProduce, retError := p.ProductivityByEpoch(epochNum, tipHeight, productivity)
+ require.Equal(retHeight, expectedHeights)
+ require.Equal(retProduce, expectedProduces)
+ require.NoError(retError)
+ })
+
+ t.Run("tipHeight param error", func(t *testing.T) {
+ epochNum := uint64(0)
+ tipHeight := uint64(0)
+ expectedHeights := uint64(0)
+ expectedProduces := map[string]uint64{}
+ retHeight, retProduce, retError := p.ProductivityByEpoch(epochNum, tipHeight, productivity)
+ require.Equal(retHeight, expectedHeights)
+ require.Equal(retProduce, expectedProduces)
+ require.NoError(retError)
+ })
+
+ t.Run("epochNum param error", func(t *testing.T) {
+ epochNum := uint64(2)
+ tipHeight := uint64(12)
+ expectedHeights := uint64(0)
+ var expectedProduces = map[string]uint64{}
+ expectedProduces = nil
+ expectedErrors := errors.New("epoch number 2 is larger than current epoch number 1")
+ retHeight, retProduce, retError := p.ProductivityByEpoch(epochNum, tipHeight, productivity)
+ require.Equal(retHeight, expectedHeights)
+ require.Equal(retProduce, expectedProduces)
+ require.EqualError(retError, expectedErrors.Error())
+ })
+
+ t.Run("productivity param error", func(t *testing.T) {
+ epochNum := uint64(0)
+ tipHeight := uint64(1)
+ expectedHeights := uint64(1)
+ var expectedProduces = map[string]uint64{}
+ expectedProduces = nil
+ expectedErrors := errors.New("productivity error")
+ retHeight, retProduce, retError := p.ProductivityByEpoch(epochNum, tipHeight, productivity)
+ require.Equal(retHeight, expectedHeights)
+ require.Equal(retProduce, expectedProduces)
+ require.EqualError(retError, expectedErrors.Error())
+ })
+}
