frostfs-s3-gw/api/layer/util_test.go
Angira Kekteeva d09a4e4e6c [#145] Fix util tests
Fixed contentType attribute difference in expected and actual results

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
2021-07-09 00:36:19 +03:00

158 lines
3.9 KiB
Go

package layer
import (
"net/http"
"strconv"
"testing"
"time"
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/stretchr/testify/require"
)
var (
defaultTestCreated = time.Now()
defaultTestPayload = []byte("test object payload")
defaultTestPayloadLength = int64(len(defaultTestPayload))
defaultTestContentType = http.DetectContentType(defaultTestPayload)
)
func newTestObject(oid *object.ID, bkt *BucketInfo, name string) *object.Object {
filename := object.NewAttribute()
filename.SetKey(object.AttributeFileName)
filename.SetValue(name)
created := object.NewAttribute()
created.SetKey(object.AttributeTimestamp)
created.SetValue(strconv.FormatInt(defaultTestCreated.Unix(), 10))
contentType := object.NewAttribute()
contentType.SetKey(object.AttributeContentType)
contentType.SetValue(defaultTestContentType)
raw := object.NewRaw()
raw.SetID(oid)
raw.SetOwnerID(bkt.Owner)
raw.SetContainerID(bkt.CID)
raw.SetPayload(defaultTestPayload)
raw.SetAttributes(filename, created, contentType)
raw.SetPayloadSize(uint64(defaultTestPayloadLength))
return raw.Object()
}
func newTestInfo(oid *object.ID, bkt *BucketInfo, name string, isDir bool) *ObjectInfo {
info := &ObjectInfo{
id: oid,
Name: name,
Bucket: bkt.Name,
Size: defaultTestPayloadLength,
ContentType: defaultTestContentType,
Created: time.Unix(defaultTestCreated.Unix(), 0),
Owner: bkt.Owner,
Headers: make(map[string]string),
}
if isDir {
info.isDir = true
info.Size = 0
info.ContentType = ""
info.Headers = nil
}
return info
}
func Test_objectInfoFromMeta(t *testing.T) {
uid := owner.NewID()
oid := object.NewID()
containerID := cid.New()
bkt := &BucketInfo{
Name: "test-container",
CID: containerID,
Owner: uid,
Created: time.Now(),
}
cases := []struct {
name string
prefix string
result *ObjectInfo
object *object.Object
delimiter string
}{
{
name: "small.jpg",
result: newTestInfo(oid, bkt, "small.jpg", false),
object: newTestObject(oid, bkt, "small.jpg"),
},
{
name: "small.jpg not matched prefix",
prefix: "big",
result: nil,
object: newTestObject(oid, bkt, "small.jpg"),
},
{
name: "small.jpg delimiter",
delimiter: "/",
result: newTestInfo(oid, bkt, "small.jpg", false),
object: newTestObject(oid, bkt, "small.jpg"),
},
{
name: "test/small.jpg",
result: newTestInfo(oid, bkt, "test/small.jpg", false),
object: newTestObject(oid, bkt, "test/small.jpg"),
},
{
name: "test/small.jpg with prefix and delimiter",
prefix: "test/",
delimiter: "/",
result: newTestInfo(oid, bkt, "test/small.jpg", false),
object: newTestObject(oid, bkt, "test/small.jpg"),
},
{
name: "a/b/small.jpg",
prefix: "a",
result: newTestInfo(oid, bkt, "a/b/small.jpg", false),
object: newTestObject(oid, bkt, "a/b/small.jpg"),
},
{
name: "a/b/small.jpg",
prefix: "a/",
delimiter: "/",
result: newTestInfo(oid, bkt, "a/b/", true),
object: newTestObject(oid, bkt, "a/b/small.jpg"),
},
{
name: "a/b/c/small.jpg",
prefix: "a/",
delimiter: "/",
result: newTestInfo(oid, bkt, "a/b/", true),
object: newTestObject(oid, bkt, "a/b/c/small.jpg"),
},
{
name: "a/b/c/small.jpg",
prefix: "a/b/c/s",
delimiter: "/",
result: newTestInfo(oid, bkt, "a/b/c/small.jpg", false),
object: newTestObject(oid, bkt, "a/b/c/small.jpg"),
},
{
name: "a/b/c/big.jpg",
prefix: "a/b/",
delimiter: "/",
result: newTestInfo(oid, bkt, "a/b/c/", true),
object: newTestObject(oid, bkt, "a/b/c/big.jpg"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
info := objectInfoFromMeta(bkt, tc.object, tc.prefix, tc.delimiter)
require.Equal(t, tc.result, info)
})
}
}