neoneo-go/pkg/services/oracle/neofs/neofs_test.go

76 lines
1.9 KiB
Go
Raw Normal View History

2020-10-16 09:51:43 +00:00
package neofs
import (
"errors"
"net/url"
"testing"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
2020-10-16 09:51:43 +00:00
"github.com/stretchr/testify/require"
)
func TestParseRange(t *testing.T) {
t.Run("valid", func(t *testing.T) {
r, err := parseRange("13|87")
require.NoError(t, err)
require.Equal(t, uint64(13), r.GetOffset())
require.Equal(t, uint64(87), r.GetLength())
})
t.Run("missing offset", func(t *testing.T) {
_, err := parseRange("|87")
require.Error(t, err)
})
t.Run("missing length", func(t *testing.T) {
_, err := parseRange("13|")
require.Error(t, err)
})
t.Run("missing separator", func(t *testing.T) {
_, err := parseRange("1387")
require.Error(t, err)
})
t.Run("invalid number", func(t *testing.T) {
_, err := parseRange("ab|87")
require.Error(t, err)
})
}
func TestParseNeoFSURL(t *testing.T) {
cStr := "C3swfg8MiMJ9bXbeFG6dWJTCoHp9hAEZkHezvbSwK1Cc"
oStr := "3nQH1L8u3eM9jt2mZCs6MyjzdjerdSzBkXCYYj4M4Znk"
var objectAddr oid.Address
require.NoError(t, objectAddr.DecodeString(cStr+"/"+oStr))
2020-10-16 09:51:43 +00:00
validPrefix := "neofs:" + cStr + "/" + oStr
2020-10-16 09:51:43 +00:00
testCases := []struct {
url string
params []string
err error
}{
{validPrefix, nil, nil},
{validPrefix + "/", []string{""}, nil},
{validPrefix + "/range/1|2", []string{"range", "1|2"}, nil},
{"neoffs:" + cStr + "/" + oStr, nil, ErrInvalidScheme},
{"neofs:" + cStr, nil, ErrMissingObject},
{"neofs:" + cStr + "ooo/" + oStr, nil, ErrInvalidContainer},
{"neofs:" + cStr + "/ooo" + oStr, nil, ErrInvalidObject},
2020-10-16 09:51:43 +00:00
}
for _, tc := range testCases {
t.Run(tc.url, func(t *testing.T) {
u, err := url.Parse(tc.url)
require.NoError(t, err)
oa, ps, err := parseNeoFSURL(u)
if tc.err != nil {
require.True(t, errors.Is(err, tc.err), "got: %#v", err)
return
}
require.NoError(t, err)
require.Equal(t, objectAddr, *oa)
2020-10-16 09:51:43 +00:00
require.Equal(t, len(tc.params), len(ps))
if len(ps) != 0 {
require.Equal(t, tc.params, ps)
}
})
}
}