-
Notifications
You must be signed in to change notification settings - Fork 38
/
uploader_test.go
98 lines (90 loc) · 2.86 KB
/
uploader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package afs
import (
"bytes"
"context"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/viant/afs/asset"
"github.com/viant/afs/file"
"io"
"os"
"path"
"strings"
"testing"
)
func TestService_Uploader(t *testing.T) {
baseDir := os.TempDir()
var useCases = []struct {
description string
destURL string
assets []*asset.Resource
}{
{
description: "batch asset upload",
destURL: path.Join(baseDir, "service_uploader_01/dst"),
assets: []*asset.Resource{
asset.NewFile("asset1.txt", []byte("test 1"), 0644),
asset.NewFile("asset2.txt", []byte("test 1"), 0644),
},
},
{
description: "batch multi level asset upload",
destURL: path.Join(baseDir, "service_uploader_02/dst"),
assets: []*asset.Resource{
asset.NewFile("asset1.txt", []byte("test 1"), 0644),
asset.NewFile("asset2.txt", []byte("test 2"), 0644),
asset.NewDir("folder1", file.DefaultDirOsMode),
asset.NewFile("folder1/asset1.txt", []byte("test 1"), 0644),
asset.NewFile("folder1/asset2.txt", []byte("test 2"), 0644),
asset.NewDir("folder1/s1", file.DefaultDirOsMode),
asset.NewFile("folder1/s1/asset1.txt", []byte("test 1"), 0644),
asset.NewDir("folder1/ss", file.DefaultDirOsMode),
asset.NewFile("folder1/ss/asset2.txt", []byte("test 2"), 0644),
asset.NewFile("asset3.txt", []byte("test 2"), 0644),
},
},
}
ctx := context.Background()
for _, useCase := range useCases {
service := New()
destManager, err := Manager(useCase.destURL)
if !assert.Nil(t, err, useCase.description) {
continue
}
_ = asset.Cleanup(destManager, useCase.destURL)
assert.Nil(t, err, useCase.description)
upload, closer, err := service.Uploader(ctx, useCase.destURL)
if !assert.Nil(t, err, useCase.description) {
continue
}
for _, asset := range useCase.assets {
relative := ""
var reader io.Reader
if strings.Contains(asset.Name, "/") {
relative, _ = path.Split(asset.Name)
}
if !asset.Dir {
reader = bytes.NewReader(asset.Data)
}
err = upload(ctx, relative, asset.Info(), reader)
assert.Nil(t, err, useCase.description+" "+asset.Name)
}
_ = closer.Close()
actuals, err := asset.Load(destManager, useCase.destURL)
if !assert.Nil(t, err) {
continue
}
for _, expect := range useCase.assets {
actual, ok := actuals[expect.Name]
if !assert.True(t, ok, useCase.description+": "+expect.Name+fmt.Sprintf(" - actuals: %v", actuals)) {
continue
}
assert.EqualValues(t, expect.Name, actual.Name, useCase.description+" "+expect.Name)
assert.EqualValues(t, expect.Mode, actual.Mode, useCase.description+" "+expect.Name)
assert.EqualValues(t, expect.Dir, actual.Dir, useCase.description+" "+expect.Name)
assert.EqualValues(t, expect.Data, actual.Data, useCase.description+" "+expect.Name)
}
_ = asset.Cleanup(destManager, useCase.destURL)
_ = service.Close(useCase.destURL)
}
}