-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs_manager_test.go
120 lines (91 loc) · 3.06 KB
/
gcs_manager_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package gcs
import (
"io"
"os"
"testing"
"cloud.google.com/go/storage"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func TestUploadFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
fileContent := []byte("test content")
file := os.NewFile(0, "test-file")
defer file.Close()
file.Write(fileContent)
file.Seek(0, io.SeekStart)
mockGCS.EXPECT().UploadFile("bucketName", "objectName", *file).Return(nil)
err := mockGCS.UploadFile("bucketName", "objectName", *file)
assert.NoError(t, err)
}
func TestDeleteFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().DeleteFile("bucketName", "objectName").Return(nil)
err := mockGCS.DeleteFile("bucketName", "objectName")
assert.NoError(t, err)
}
func TestListFiles(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
expectedFiles := []string{"file1", "file2"}
mockGCS.EXPECT().ListFiles("bucketName", "/").Return(expectedFiles, nil)
files, err := mockGCS.ListFiles("bucketName", "/")
assert.NoError(t, err)
assert.Equal(t, expectedFiles, files)
}
func TestMoveFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().MoveFile("bucketName", "srcObjectName", "dstObjectName").Return(nil)
err := mockGCS.MoveFile("bucketName", "srcObjectName", "dstObjectName")
assert.NoError(t, err)
}
func TestDeleteAllFilesInDirectory(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().DeleteAllFilesInDirectory("bucketName", "directory/").Return(nil)
err := mockGCS.DeleteAllFilesInDirectory("bucketName", "directory/")
assert.NoError(t, err)
}
func TestDownloadFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().DownloadFile("bucketName", "directory/file.ext", "/tmp/").Return(nil)
err := mockGCS.DownloadFile("bucketName", "directory/file.ext", "/tmp/")
assert.NoError(t, err)
}
func TestUploadDirectory(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
fileContent := []byte("test content")
file := os.NewFile(0, "test-file")
defer file.Close()
file.Write(fileContent)
file.Seek(0, io.SeekStart)
mockGCS.EXPECT().UploadDirectory("bucketName", "localDir", "prefix").Return(nil)
err := mockGCS.UploadDirectory("bucketName", "localDir", "prefix")
assert.NoError(t, err)
}
func TestRead(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockGCS := NewMockGCSManager(ctrl)
fileContent := []byte("test content")
file := os.NewFile(0, "test-file")
defer file.Close()
file.Write(fileContent)
file.Seek(0, io.SeekStart)
mockGCS.EXPECT().Read("bucketName", "objectName").Return(&storage.Reader{}, nil)
reader, err := mockGCS.Read("bucketName", "objectName")
assert.NoError(t, err)
assert.Equal(t, &storage.Reader{}, reader)
}