2020-09-22 06:51:47 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/container"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-10-01 12:12:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/search/query"
|
2020-09-22 06:51:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testID(t *testing.T) *objectSDK.ID {
|
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id := objectSDK.NewID()
|
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCID(t *testing.T) *container.ID {
|
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
|
|
|
|
_, err := rand.Read(cs[:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id := container.NewID()
|
|
|
|
id.SetSHA256(cs)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOwnerID(t *testing.T) *owner.ID {
|
|
|
|
w := new(owner.NEO3Wallet)
|
|
|
|
|
|
|
|
_, err := rand.Read(w.Bytes())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id := owner.NewID()
|
|
|
|
id.SetNeo3Wallet(w)
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
func matchesQuery(q query.Query, obj *object.Object) (res bool) {
|
|
|
|
q.Match(obj, func(id *objectSDK.ID) {
|
|
|
|
res = id.Equal(obj.GetID())
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:51:47 +00:00
|
|
|
func TestQ_Match(t *testing.T) {
|
|
|
|
t.Run("container identifier equal", func(t *testing.T) {
|
|
|
|
obj := object.NewRaw()
|
|
|
|
|
|
|
|
id := testCID(t)
|
|
|
|
obj.SetContainerID(id)
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
fs := objectSDK.SearchFilters{}
|
2020-10-29 14:57:07 +00:00
|
|
|
fs.AddObjectContainerIDFilter(objectSDK.MatchStringEqual, id)
|
2020-10-01 12:12:39 +00:00
|
|
|
|
|
|
|
q := New(fs)
|
2020-09-22 06:51:47 +00:00
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
require.True(t, matchesQuery(q, obj.Object()))
|
2020-09-22 06:51:47 +00:00
|
|
|
|
|
|
|
obj.SetContainerID(testCID(t))
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
require.False(t, matchesQuery(q, obj.Object()))
|
2020-09-22 06:51:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("owner identifier equal", func(t *testing.T) {
|
|
|
|
obj := object.NewRaw()
|
|
|
|
|
|
|
|
id := testOwnerID(t)
|
|
|
|
obj.SetOwnerID(id)
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
fs := objectSDK.SearchFilters{}
|
2020-10-29 14:57:07 +00:00
|
|
|
fs.AddObjectOwnerIDFilter(objectSDK.MatchStringEqual, id)
|
2020-09-22 06:51:47 +00:00
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
q := New(fs)
|
|
|
|
|
|
|
|
require.True(t, matchesQuery(q, obj.Object()))
|
2020-09-22 06:51:47 +00:00
|
|
|
|
|
|
|
obj.SetOwnerID(testOwnerID(t))
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
require.False(t, matchesQuery(q, obj.Object()))
|
2020-09-22 06:51:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("attribute equal", func(t *testing.T) {
|
|
|
|
obj := object.NewRaw()
|
|
|
|
|
|
|
|
k, v := "key", "val"
|
|
|
|
a := new(objectSDK.Attribute)
|
|
|
|
a.SetKey(k)
|
|
|
|
a.SetValue(v)
|
|
|
|
|
|
|
|
obj.SetAttributes(a)
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
fs := objectSDK.SearchFilters{}
|
|
|
|
fs.AddFilter(k, v, objectSDK.MatchStringEqual)
|
|
|
|
|
|
|
|
q := New(fs)
|
2020-09-22 06:51:47 +00:00
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
require.True(t, matchesQuery(q, obj.Object()))
|
2020-09-22 06:51:47 +00:00
|
|
|
|
|
|
|
a.SetKey(k + "1")
|
|
|
|
|
2020-10-01 12:12:39 +00:00
|
|
|
require.False(t, matchesQuery(q, obj.Object()))
|
2020-09-22 06:51:47 +00:00
|
|
|
})
|
|
|
|
}
|