forked from TrueCloudLab/frostfs-api-go
941b513eb3
Document that `Attribute.ToV2` method return `nil` when called on `nil`. Document that `NewAttributeFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package object
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAttribute(t *testing.T) {
|
|
key, val := "some key", "some value"
|
|
|
|
a := NewAttribute()
|
|
a.SetKey(key)
|
|
a.SetValue(val)
|
|
|
|
require.Equal(t, key, a.Key())
|
|
require.Equal(t, val, a.Value())
|
|
|
|
aV2 := a.ToV2()
|
|
|
|
require.Equal(t, key, aV2.GetKey())
|
|
require.Equal(t, val, aV2.GetValue())
|
|
}
|
|
|
|
func TestAttributeEncoding(t *testing.T) {
|
|
a := NewAttribute()
|
|
a.SetKey("key")
|
|
a.SetValue("value")
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
data, err := a.Marshal()
|
|
require.NoError(t, err)
|
|
|
|
a2 := NewAttribute()
|
|
require.NoError(t, a2.Unmarshal(data))
|
|
|
|
require.Equal(t, a, a2)
|
|
})
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
data, err := a.MarshalJSON()
|
|
require.NoError(t, err)
|
|
|
|
a2 := NewAttribute()
|
|
require.NoError(t, a2.UnmarshalJSON(data))
|
|
|
|
require.Equal(t, a, a2)
|
|
})
|
|
}
|
|
|
|
func TestNewAttributeFromV2(t *testing.T) {
|
|
t.Run("from nil", func(t *testing.T) {
|
|
var x *object.Attribute
|
|
|
|
require.Nil(t, NewAttributeFromV2(x))
|
|
})
|
|
}
|
|
|
|
func TestAttribute_ToV2(t *testing.T) {
|
|
t.Run("nil", func(t *testing.T) {
|
|
var x *Attribute
|
|
|
|
require.Nil(t, x.ToV2())
|
|
})
|
|
}
|