frostfs-api-go/pkg/reputation/peer_test.go
Pavel Karpy cce7ecbc00 [#302] pkg/reputation: Document default values set in NewPeerID
Document field values of instance constructed via `NewPeerID`.
Assert the values in corresponding unit test.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
2021-06-09 17:23:35 +03:00

88 lines
1.7 KiB
Go

package reputation_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/pkg/reputation"
reputationtest "github.com/nspcc-dev/neofs-api-go/pkg/reputation/test"
reputationV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation"
"github.com/stretchr/testify/require"
)
func TestPeerID_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *reputation.PeerID
require.Nil(t, x.ToV2())
})
t.Run("nil", func(t *testing.T) {
peerID := reputationtest.GeneratePeerID()
require.Equal(t, peerID, reputation.PeerIDFromV2(peerID.ToV2()))
})
}
func TestPeerID_String(t *testing.T) {
t.Run("Parse/String", func(t *testing.T) {
id := reputationtest.GeneratePeerID()
strID := id.String()
id2 := reputation.NewPeerID()
err := id2.Parse(strID)
require.NoError(t, err)
require.Equal(t, id, id2)
})
t.Run("nil", func(t *testing.T) {
id := reputation.NewPeerID()
require.Empty(t, id.String())
})
}
func TestPeerIDEncoding(t *testing.T) {
id := reputationtest.GeneratePeerID()
t.Run("binary", func(t *testing.T) {
data, err := id.Marshal()
require.NoError(t, err)
id2 := reputation.NewPeerID()
require.NoError(t, id2.Unmarshal(data))
require.Equal(t, id, id2)
})
t.Run("json", func(t *testing.T) {
data, err := id.MarshalJSON()
require.NoError(t, err)
id2 := reputation.NewPeerID()
require.NoError(t, id2.UnmarshalJSON(data))
require.Equal(t, id, id2)
})
}
func TestPeerIDFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *reputationV2.PeerID
require.Nil(t, reputation.PeerIDFromV2(x))
})
}
func TestNewPeerID(t *testing.T) {
t.Run("default values", func(t *testing.T) {
id := reputation.NewPeerID()
// convert to v2 message
idV2 := id.ToV2()
require.Nil(t, idV2.GetPublicKey())
})
}