2023-10-23 22:47:18 +00:00
|
|
|
package fs_test
|
2022-05-24 10:00:00 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-23 22:47:18 +00:00
|
|
|
"context"
|
2022-05-24 14:46:07 +00:00
|
|
|
"fmt"
|
2022-05-24 10:00:00 +00:00
|
|
|
"testing"
|
2023-10-23 22:47:18 +00:00
|
|
|
"time"
|
2022-05-24 10:00:00 +00:00
|
|
|
|
2023-10-23 22:47:18 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/object"
|
|
|
|
"github.com/rclone/rclone/fstest/mockfs"
|
2022-05-24 10:00:00 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-10-23 22:47:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-05-24 10:00:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetadataSet(t *testing.T) {
|
2023-10-23 22:47:18 +00:00
|
|
|
var m fs.Metadata
|
2022-05-24 10:00:00 +00:00
|
|
|
assert.Nil(t, m)
|
|
|
|
m.Set("key", "value")
|
|
|
|
assert.NotNil(t, m)
|
|
|
|
assert.Equal(t, "value", m["key"])
|
|
|
|
m.Set("key", "value2")
|
|
|
|
assert.Equal(t, "value2", m["key"])
|
|
|
|
}
|
2022-05-24 14:46:07 +00:00
|
|
|
|
|
|
|
func TestMetadataMerge(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
2023-10-23 22:47:18 +00:00
|
|
|
in fs.Metadata
|
|
|
|
merge fs.Metadata
|
|
|
|
want fs.Metadata
|
2022-05-24 14:46:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-10-23 22:47:18 +00:00
|
|
|
in: fs.Metadata{},
|
|
|
|
merge: fs.Metadata{},
|
|
|
|
want: fs.Metadata{},
|
2022-05-24 14:46:07 +00:00
|
|
|
}, {
|
|
|
|
in: nil,
|
|
|
|
merge: nil,
|
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
in: nil,
|
2023-10-23 22:47:18 +00:00
|
|
|
merge: fs.Metadata{},
|
2022-05-24 14:46:07 +00:00
|
|
|
want: nil,
|
|
|
|
}, {
|
|
|
|
in: nil,
|
2023-10-23 22:47:18 +00:00
|
|
|
merge: fs.Metadata{"a": "1", "b": "2"},
|
|
|
|
want: fs.Metadata{"a": "1", "b": "2"},
|
2022-05-24 14:46:07 +00:00
|
|
|
}, {
|
2023-10-23 22:47:18 +00:00
|
|
|
in: fs.Metadata{"a": "1", "b": "2"},
|
2022-05-24 14:46:07 +00:00
|
|
|
merge: nil,
|
2023-10-23 22:47:18 +00:00
|
|
|
want: fs.Metadata{"a": "1", "b": "2"},
|
2022-05-24 14:46:07 +00:00
|
|
|
}, {
|
2023-10-23 22:47:18 +00:00
|
|
|
in: fs.Metadata{"a": "1", "b": "2"},
|
|
|
|
merge: fs.Metadata{"b": "B", "c": "3"},
|
|
|
|
want: fs.Metadata{"a": "1", "b": "B", "c": "3"},
|
2022-05-24 14:46:07 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
what := fmt.Sprintf("in=%v, merge=%v", test.in, test.merge)
|
|
|
|
test.in.Merge(test.merge)
|
|
|
|
assert.Equal(t, test.want, test.in, what)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataMergeOptions(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
2023-10-23 22:47:18 +00:00
|
|
|
in fs.Metadata
|
|
|
|
opts []fs.OpenOption
|
|
|
|
want fs.Metadata
|
2022-05-24 14:46:07 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-10-23 22:47:18 +00:00
|
|
|
opts: []fs.OpenOption{},
|
2022-05-24 14:46:07 +00:00
|
|
|
want: nil,
|
|
|
|
}, {
|
2023-10-23 22:47:18 +00:00
|
|
|
opts: []fs.OpenOption{&fs.HTTPOption{}},
|
2022-05-24 14:46:07 +00:00
|
|
|
want: nil,
|
|
|
|
}, {
|
2023-10-23 22:47:18 +00:00
|
|
|
opts: []fs.OpenOption{fs.MetadataOption{"a": "1", "b": "2"}},
|
|
|
|
want: fs.Metadata{"a": "1", "b": "2"},
|
2022-05-24 14:46:07 +00:00
|
|
|
}, {
|
2023-10-23 22:47:18 +00:00
|
|
|
opts: []fs.OpenOption{
|
|
|
|
&fs.HTTPOption{},
|
|
|
|
fs.MetadataOption{"a": "1", "b": "2"},
|
|
|
|
fs.MetadataOption{"b": "B", "c": "3"},
|
|
|
|
&fs.HTTPOption{},
|
2022-05-24 14:46:07 +00:00
|
|
|
},
|
2023-10-23 22:47:18 +00:00
|
|
|
want: fs.Metadata{"a": "1", "b": "B", "c": "3"},
|
2022-05-24 14:46:07 +00:00
|
|
|
}, {
|
2023-10-23 22:47:18 +00:00
|
|
|
in: fs.Metadata{"a": "first", "z": "OK"},
|
|
|
|
opts: []fs.OpenOption{
|
|
|
|
&fs.HTTPOption{},
|
|
|
|
fs.MetadataOption{"a": "1", "b": "2"},
|
|
|
|
fs.MetadataOption{"b": "B", "c": "3"},
|
|
|
|
&fs.HTTPOption{},
|
2022-05-24 14:46:07 +00:00
|
|
|
},
|
2023-10-23 22:47:18 +00:00
|
|
|
want: fs.Metadata{"a": "1", "b": "B", "c": "3", "z": "OK"},
|
2022-05-24 14:46:07 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
what := fmt.Sprintf("in=%v, opts=%v", test.in, test.opts)
|
|
|
|
test.in.MergeOptions(test.opts)
|
|
|
|
assert.Equal(t, test.want, test.in, what)
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 22:47:18 +00:00
|
|
|
|
|
|
|
func TestMetadataMapper(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, ci := fs.AddConfig(ctx)
|
|
|
|
ci.Metadata = true
|
|
|
|
require.NoError(t, ci.MetadataMapper.Set("go run metadata_mapper_code.go"))
|
|
|
|
now := time.Date(2001, 2, 3, 4, 5, 6, 7, time.UTC)
|
|
|
|
f, err := mockfs.NewFs(ctx, "dstFs", "dstFsRoot", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("Normal", func(t *testing.T) {
|
|
|
|
o := object.NewMemoryObject("file.txt", now, []byte("hello")).WithMetadata(fs.Metadata{
|
|
|
|
"key1": "potato",
|
|
|
|
"key2": "sausage",
|
|
|
|
"key3": "gravy",
|
|
|
|
})
|
|
|
|
metadata, err := fs.GetMetadataOptions(ctx, f, o, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, fs.Metadata{
|
|
|
|
"key0": "cabbage",
|
|
|
|
"key1": "two potato",
|
|
|
|
"key2": "sausage",
|
|
|
|
}, metadata)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Error", func(t *testing.T) {
|
|
|
|
o := object.NewMemoryObject("file.txt", now, []byte("hello")).WithMetadata(fs.Metadata{
|
|
|
|
"error": "Red Alert",
|
|
|
|
})
|
|
|
|
metadata, err := fs.GetMetadataOptions(ctx, f, o, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.ErrorContains(t, err, "Red Alert")
|
|
|
|
require.Nil(t, metadata)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Merge", func(t *testing.T) {
|
|
|
|
o := object.NewMemoryObject("file.txt", now, []byte("hello")).WithMetadata(fs.Metadata{
|
|
|
|
"key1": "potato",
|
|
|
|
"key2": "sausage",
|
|
|
|
"key3": "gravy",
|
|
|
|
})
|
|
|
|
metadata, err := fs.GetMetadataOptions(ctx, f, o, []fs.OpenOption{fs.MetadataOption(fs.Metadata{
|
|
|
|
"option": "optionValue",
|
|
|
|
"key1": "new potato",
|
|
|
|
"key2": "salami",
|
|
|
|
})})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, fs.Metadata{
|
|
|
|
"key0": "cabbage",
|
|
|
|
"key1": "two new potato",
|
|
|
|
"key2": "salami",
|
|
|
|
"option": "optionValue",
|
|
|
|
}, metadata)
|
|
|
|
})
|
|
|
|
}
|