From 5ff7b5ba6b88c26fe48b77101177afa8ba05e09c Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 11 Nov 2020 17:21:55 +0300 Subject: [PATCH] [#193] pkg/container: Increase test coverage Signed-off-by: Alex Vanin --- pkg/container/attribute_test.go | 84 +++++++++++++++++++++++++++++++++ pkg/container/container_test.go | 68 ++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 pkg/container/attribute_test.go create mode 100644 pkg/container/container_test.go diff --git a/pkg/container/attribute_test.go b/pkg/container/attribute_test.go new file mode 100644 index 0000000..a7a700d --- /dev/null +++ b/pkg/container/attribute_test.go @@ -0,0 +1,84 @@ +package container_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/stretchr/testify/require" +) + +func TestAttribute(t *testing.T) { + const ( + key = "key" + value = "value" + ) + + attr := container.NewAttribute() + attr.SetKey(key) + attr.SetValue(value) + + require.Equal(t, key, attr.Key()) + require.Equal(t, value, attr.Value()) + + t.Run("test v2", func(t *testing.T) { + const ( + newKey = "newKey" + newValue = "newValue" + ) + + v2 := attr.ToV2() + require.Equal(t, key, v2.GetKey()) + require.Equal(t, value, v2.GetValue()) + + v2.SetKey(newKey) + v2.SetValue(newValue) + + newAttr := container.NewAttributeFromV2(v2) + + require.Equal(t, newKey, newAttr.Key()) + require.Equal(t, newValue, newAttr.Value()) + }) +} + +func TestAttributes(t *testing.T) { + var ( + keys = []string{"key1", "key2", "key3"} + vals = []string{"val1", "val2", "val3"} + ) + + attrs := make(container.Attributes, 0, len(keys)) + + for i := range keys { + attr := container.NewAttribute() + attr.SetKey(keys[i]) + attr.SetValue(vals[i]) + + attrs = append(attrs, attr) + } + + t.Run("test v2", func(t *testing.T) { + const postfix = "x" + + v2 := attrs.ToV2() + require.Len(t, v2, len(keys)) + + for i := range v2 { + k := v2[i].GetKey() + v := v2[i].GetValue() + + require.Equal(t, keys[i], k) + require.Equal(t, vals[i], v) + + v2[i].SetKey(k + postfix) + v2[i].SetValue(v + postfix) + } + + newAttrs := container.NewAttributesFromV2(v2) + require.Len(t, newAttrs, len(keys)) + + for i := range newAttrs { + require.Equal(t, keys[i]+postfix, newAttrs[i].Key()) + require.Equal(t, vals[i]+postfix, newAttrs[i].Value()) + } + }) +} diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go new file mode 100644 index 0000000..ec5e273 --- /dev/null +++ b/pkg/container/container_test.go @@ -0,0 +1,68 @@ +package container_test + +import ( + "strconv" + "testing" + + "github.com/google/uuid" + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/pkg/acl" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-crypto/test" + "github.com/stretchr/testify/require" +) + +func TestNewContainer(t *testing.T) { + c := container.New() + + nonce, err := uuid.New().MarshalBinary() + require.NoError(t, err) + + wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(1).PublicKey) + require.NoError(t, err) + + ownerID := owner.NewIDFromNeo3Wallet(wallet) + policy := generatePlacementPolicy() + + c.SetBasicACL(acl.PublicBasicRule) + c.SetAttributes(generateAttributes(5)) + c.SetPlacementPolicy(policy) + c.SetNonce(nonce) + c.SetOwnerID(ownerID) + c.SetVersion(pkg.SDKVersion()) + + v2 := c.ToV2() + newContainer := container.NewContainerFromV2(v2) + + require.EqualValues(t, newContainer.PlacementPolicy(), policy) + require.EqualValues(t, newContainer.Attributes(), generateAttributes(5)) + require.EqualValues(t, newContainer.BasicACL(), acl.PublicBasicRule) + require.EqualValues(t, newContainer.Nonce(), nonce) + require.EqualValues(t, newContainer.OwnerID(), ownerID) + require.EqualValues(t, newContainer.Version(), pkg.SDKVersion()) +} + +func generateAttributes(n int) container.Attributes { + attrs := make(container.Attributes, 0, n) + + for i := 0; i < n; i++ { + strN := strconv.Itoa(n) + + attr := container.NewAttribute() + attr.SetKey("key" + strN) + attr.SetValue("val" + strN) + + attrs = append(attrs, attr) + } + + return attrs +} + +func generatePlacementPolicy() *netmap.PlacementPolicy { + p := new(netmap.PlacementPolicy) + p.SetContainerBackupFactor(10) + + return p +}