2020-09-17 09:20:35 +00:00
|
|
|
package object_test
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:31:23 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2020-09-17 09:20:35 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
eqV2Matches = map[object.SearchMatchType]v2object.MatchType{
|
2021-02-01 17:57:18 +00:00
|
|
|
object.MatchUnknown: v2object.MatchUnknown,
|
|
|
|
object.MatchStringEqual: v2object.MatchStringEqual,
|
|
|
|
object.MatchStringNotEqual: v2object.MatchStringNotEqual,
|
|
|
|
object.MatchNotPresent: v2object.MatchNotPresent,
|
2020-09-17 09:20:35 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMatch(t *testing.T) {
|
|
|
|
t.Run("known matches", func(t *testing.T) {
|
2021-02-01 17:54:33 +00:00
|
|
|
for matchType, matchTypeV2 := range eqV2Matches {
|
|
|
|
require.Equal(t, matchTypeV2, matchType.ToV2())
|
|
|
|
require.Equal(t, object.SearchMatchFromV2(matchTypeV2), matchType)
|
2020-09-17 09:20:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown matches", func(t *testing.T) {
|
2021-02-01 17:54:33 +00:00
|
|
|
var unknownMatchType object.SearchMatchType
|
|
|
|
|
|
|
|
for matchType := range eqV2Matches {
|
|
|
|
unknownMatchType += matchType
|
|
|
|
}
|
|
|
|
|
|
|
|
unknownMatchType++
|
|
|
|
|
|
|
|
require.Equal(t, unknownMatchType.ToV2(), v2object.MatchUnknown)
|
|
|
|
|
|
|
|
var unknownMatchTypeV2 v2object.MatchType
|
|
|
|
|
|
|
|
for _, matchTypeV2 := range eqV2Matches {
|
|
|
|
unknownMatchTypeV2 += matchTypeV2
|
|
|
|
}
|
|
|
|
|
|
|
|
unknownMatchTypeV2++
|
|
|
|
|
|
|
|
require.Equal(t, object.SearchMatchFromV2(unknownMatchTypeV2), object.MatchUnknown)
|
2020-09-17 09:20:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter(t *testing.T) {
|
|
|
|
inputs := [][]string{
|
|
|
|
{"user-header", "user-value"},
|
|
|
|
}
|
|
|
|
|
|
|
|
filters := object.NewSearchFilters()
|
|
|
|
for i := range inputs {
|
|
|
|
filters.AddFilter(inputs[i][0], inputs[i][1], object.MatchStringEqual)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, filters, len(inputs))
|
|
|
|
for i := range inputs {
|
|
|
|
require.Equal(t, inputs[i][0], filters[i].Header())
|
|
|
|
require.Equal(t, inputs[i][1], filters[i].Value())
|
|
|
|
require.Equal(t, object.MatchStringEqual, filters[i].Operation())
|
|
|
|
}
|
|
|
|
|
|
|
|
v2 := filters.ToV2()
|
|
|
|
newFilters := object.NewSearchFiltersFromV2(v2)
|
|
|
|
require.Equal(t, filters, newFilters)
|
|
|
|
}
|
2020-09-30 15:54:07 +00:00
|
|
|
|
|
|
|
func TestSearchFilters_AddRootFilter(t *testing.T) {
|
|
|
|
fs := new(object.SearchFilters)
|
|
|
|
|
|
|
|
fs.AddRootFilter()
|
|
|
|
|
|
|
|
require.Len(t, *fs, 1)
|
|
|
|
|
|
|
|
f := (*fs)[0]
|
|
|
|
|
2020-11-09 12:41:40 +00:00
|
|
|
require.Equal(t, object.MatchUnknown, f.Operation())
|
2020-10-27 18:11:58 +00:00
|
|
|
require.Equal(t, v2object.FilterPropertyRoot, f.Header())
|
2020-11-09 12:41:40 +00:00
|
|
|
require.Equal(t, "", f.Value())
|
2020-09-30 15:54:07 +00:00
|
|
|
}
|
2020-09-30 15:57:39 +00:00
|
|
|
|
2020-11-09 12:29:26 +00:00
|
|
|
func TestSearchFilters_AddPhyFilter(t *testing.T) {
|
2020-09-30 15:57:39 +00:00
|
|
|
fs := new(object.SearchFilters)
|
|
|
|
|
2020-11-09 12:29:26 +00:00
|
|
|
fs.AddPhyFilter()
|
2020-09-30 15:57:39 +00:00
|
|
|
|
|
|
|
require.Len(t, *fs, 1)
|
|
|
|
|
|
|
|
f := (*fs)[0]
|
|
|
|
|
2020-11-09 12:41:40 +00:00
|
|
|
require.Equal(t, object.MatchUnknown, f.Operation())
|
2020-11-09 12:29:26 +00:00
|
|
|
require.Equal(t, v2object.FilterPropertyPhy, f.Header())
|
2020-11-09 12:41:40 +00:00
|
|
|
require.Equal(t, "", f.Value())
|
2020-09-30 15:57:39 +00:00
|
|
|
}
|
2020-11-05 16:31:23 +00:00
|
|
|
|
|
|
|
func testOID() *object.ID {
|
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
|
|
|
|
rand.Read(cs[:])
|
|
|
|
|
|
|
|
id := object.NewID()
|
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearchFilters_AddParentIDFilter(t *testing.T) {
|
|
|
|
par := testOID()
|
|
|
|
|
|
|
|
fs := object.SearchFilters{}
|
|
|
|
fs.AddParentIDFilter(object.MatchStringEqual, par)
|
|
|
|
|
|
|
|
fsV2 := fs.ToV2()
|
|
|
|
|
|
|
|
require.Len(t, fsV2, 1)
|
|
|
|
|
|
|
|
require.Equal(t, v2object.FilterHeaderParent, fsV2[0].GetKey())
|
|
|
|
require.Equal(t, par.String(), fsV2[0].GetValue())
|
|
|
|
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
|
|
|
|
}
|
2020-11-20 06:55:15 +00:00
|
|
|
|
|
|
|
func TestSearchFilters_AddObjectIDFilter(t *testing.T) {
|
|
|
|
id := testOID()
|
|
|
|
|
|
|
|
fs := new(object.SearchFilters)
|
|
|
|
fs.AddObjectIDFilter(object.MatchStringEqual, id)
|
|
|
|
|
|
|
|
t.Run("v2", func(t *testing.T) {
|
|
|
|
fsV2 := fs.ToV2()
|
|
|
|
|
|
|
|
require.Len(t, fsV2, 1)
|
|
|
|
|
|
|
|
require.Equal(t, v2object.FilterHeaderObjectID, fsV2[0].GetKey())
|
|
|
|
require.Equal(t, id.String(), fsV2[0].GetValue())
|
|
|
|
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
|
|
|
|
})
|
|
|
|
}
|
2020-11-25 16:32:52 +00:00
|
|
|
|
|
|
|
func TestSearchFilters_AddSplitIDFilter(t *testing.T) {
|
|
|
|
id := object.NewSplitID()
|
|
|
|
|
|
|
|
fs := new(object.SearchFilters)
|
|
|
|
fs.AddSplitIDFilter(object.MatchStringEqual, id)
|
|
|
|
|
|
|
|
t.Run("v2", func(t *testing.T) {
|
|
|
|
fsV2 := fs.ToV2()
|
|
|
|
|
|
|
|
require.Len(t, fsV2, 1)
|
|
|
|
|
|
|
|
require.Equal(t, v2object.FilterHeaderSplitID, fsV2[0].GetKey())
|
|
|
|
require.Equal(t, id.String(), fsV2[0].GetValue())
|
|
|
|
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
|
|
|
|
})
|
|
|
|
}
|
2020-12-17 15:48:09 +00:00
|
|
|
|
|
|
|
func TestSearchFilters_AddTypeFilter(t *testing.T) {
|
|
|
|
typ := object.TypeTombstone
|
|
|
|
|
|
|
|
fs := new(object.SearchFilters)
|
|
|
|
fs.AddTypeFilter(object.MatchStringEqual, typ)
|
|
|
|
|
|
|
|
t.Run("v2", func(t *testing.T) {
|
|
|
|
fsV2 := fs.ToV2()
|
|
|
|
|
|
|
|
require.Len(t, fsV2, 1)
|
|
|
|
|
|
|
|
require.Equal(t, v2object.FilterHeaderObjectType, fsV2[0].GetKey())
|
|
|
|
require.Equal(t, typ.String(), fsV2[0].GetValue())
|
|
|
|
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
|
|
|
|
})
|
|
|
|
}
|
2021-02-09 22:32:13 +00:00
|
|
|
|
|
|
|
func TestSearchFiltersEncoding(t *testing.T) {
|
|
|
|
fs := object.NewSearchFilters()
|
|
|
|
fs.AddFilter("key 1", "value 2", object.MatchStringEqual)
|
|
|
|
fs.AddFilter("key 2", "value 2", object.MatchStringNotEqual)
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
data, err := fs.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fs2 := object.NewSearchFilters()
|
|
|
|
require.NoError(t, fs2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, fs, fs2)
|
|
|
|
})
|
|
|
|
}
|
2021-06-18 12:27:01 +00:00
|
|
|
|
|
|
|
func TestSearchMatchType_String(t *testing.T) {
|
|
|
|
toPtr := func(v object.SearchMatchType) *object.SearchMatchType {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
|
|
|
testEnumStrings(t, new(object.SearchMatchType), []enumStringItem{
|
|
|
|
{val: toPtr(object.MatchStringEqual), str: "STRING_EQUAL"},
|
|
|
|
{val: toPtr(object.MatchStringNotEqual), str: "STRING_NOT_EQUAL"},
|
|
|
|
{val: toPtr(object.MatchNotPresent), str: "NOT_PRESENT"},
|
|
|
|
{val: toPtr(object.MatchUnknown), str: "MATCH_TYPE_UNSPECIFIED"},
|
|
|
|
})
|
|
|
|
}
|