prysm-pulse/beacon-chain/rpc/apimiddleware/structs_marshalling_test.go
Radosław Kapka d020e1b2d9
Correctly display epoch participation in GetStateV2 (#9598)
* Correctly display epoch participation in `GetStateV2`

* fix file names

* handle null case

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-09-16 02:51:51 +00:00

40 lines
1.2 KiB
Go

package apimiddleware
import (
"encoding/base64"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
func TestUnmarshalEpochParticipation(t *testing.T) {
t.Run("ok", func(t *testing.T) {
b := []byte{3, 3, 0}
b64 := []byte("\"" + base64.StdEncoding.EncodeToString(b) + "\"")
ep := EpochParticipation{}
require.NoError(t, ep.UnmarshalJSON(b64))
require.Equal(t, 3, len(ep))
assert.Equal(t, "3", ep[0])
assert.Equal(t, "3", ep[1])
assert.Equal(t, "0", ep[2])
})
t.Run("incorrect value", func(t *testing.T) {
ep := EpochParticipation{}
err := ep.UnmarshalJSON([]byte(":illegal:"))
require.NotNil(t, err)
assert.ErrorContains(t, "could not decode epoch participation base64 value", err)
})
t.Run("length too small", func(t *testing.T) {
ep := EpochParticipation{}
err := ep.UnmarshalJSON([]byte("x"))
require.NotNil(t, err)
assert.ErrorContains(t, "epoch participation length must be at least 2", err)
})
t.Run("null value", func(t *testing.T) {
ep := EpochParticipation{}
require.NoError(t, ep.UnmarshalJSON([]byte("null")))
assert.DeepEqual(t, EpochParticipation([]string{}), ep)
})
}