From 05f48d9394b0fb21c4e2418aba8eea6ead9845c4 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 17:47:12 +0300 Subject: [PATCH] [#302] pkg/netmap: Convert nil `NodeInfo` and `NodeAttribute` to nil message Document that `NodeAttribute.ToV2` and `NodeInfo.ToV2` method return `nil` when called on `nil`. Document that `NodeAttributeFromV2` and `NodeInfoFromV2` functions return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/netmap/node_info.go | 8 ++++++ pkg/netmap/node_info_test.go | 53 ++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/pkg/netmap/node_info.go b/pkg/netmap/node_info.go index 17aa9f4a..935f9d2f 100644 --- a/pkg/netmap/node_info.go +++ b/pkg/netmap/node_info.go @@ -193,11 +193,15 @@ func NewNodeAttribute() *NodeAttribute { } // NodeAttributeFromV2 converts v2 node Attribute to NodeAttribute. +// +// Nil netmap.Attribute converts to nil. func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute { return (*NodeAttribute)(a) } // ToV2 converts NodeAttribute to v2 node Attribute. +// +// Nil NodeAttribute converts to nil. func (a *NodeAttribute) ToV2() *netmap.Attribute { return (*netmap.Attribute)(a) } @@ -276,11 +280,15 @@ func NewNodeInfo() *NodeInfo { } // NewNodeInfoFromV2 converts v2 NodeInfo to NodeInfo. +// +// Nil netmap.NodeInfo converts to nil. func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo { return (*NodeInfo)(i) } // ToV2 converts NodeInfo to v2 NodeInfo. +// +// Nil NodeInfo converts to nil. func (i *NodeInfo) ToV2() *netmap.NodeInfo { return (*netmap.NodeInfo)(i) } diff --git a/pkg/netmap/node_info_test.go b/pkg/netmap/node_info_test.go index 9b50a9b2..ea806566 100644 --- a/pkg/netmap/node_info_test.go +++ b/pkg/netmap/node_info_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/nspcc-dev/neofs-api-go/v2/netmap" + testv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap/test" "github.com/stretchr/testify/require" ) @@ -31,14 +32,27 @@ func TestNodeStateFromV2(t *testing.T) { } func TestNodeAttributeFromV2(t *testing.T) { - aV2 := new(netmap.Attribute) - aV2.SetKey("key") - aV2.SetValue("value") - aV2.SetParents([]string{"par1", "par2"}) + t.Run("from nil", func(t *testing.T) { + var x *netmap.Attribute - a := NewNodeAttributeFromV2(aV2) + require.Nil(t, NewNodeAttributeFromV2(x)) + }) - require.Equal(t, aV2, a.ToV2()) + t.Run("from non-nil", func(t *testing.T) { + aV2 := testv2.GenerateAttribute(false) + + a := NewNodeAttributeFromV2(aV2) + + require.Equal(t, aV2, a.ToV2()) + }) +} + +func TestNodeAttribute_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *NodeAttribute + + require.Nil(t, x.ToV2()) + }) } func TestNodeAttribute_Key(t *testing.T) { @@ -78,18 +92,27 @@ func testNodeAttribute() *NodeAttribute { } func TestNodeInfoFromV2(t *testing.T) { - iV2 := new(netmap.NodeInfo) - iV2.SetPublicKey([]byte{1, 2, 3}) - iV2.SetAddress("456") - iV2.SetState(netmap.Online) - iV2.SetAttributes([]*netmap.Attribute{ - testNodeAttribute().ToV2(), - testNodeAttribute().ToV2(), + t.Run("from nil", func(t *testing.T) { + var x *netmap.NodeInfo + + require.Nil(t, NewNodeInfoFromV2(x)) }) - i := NewNodeInfoFromV2(iV2) + t.Run("from non-nil", func(t *testing.T) { + iV2 := testv2.GenerateNodeInfo(false) - require.Equal(t, iV2, i.ToV2()) + i := NewNodeInfoFromV2(iV2) + + require.Equal(t, iV2, i.ToV2()) + }) +} + +func TestNodeInfo_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *NodeInfo + + require.Nil(t, x.ToV2()) + }) } func TestNodeInfo_PublicKey(t *testing.T) {