-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_files_test.go
185 lines (164 loc) · 3.99 KB
/
api_files_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package moonshot_test
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/northes/go-moonshot"
"github.com/northes/go-moonshot/test"
"github.com/stretchr/testify/require"
)
var (
fileID string
filePath string
fileContent []byte
)
func init() {
fileContent = test.GenerateTestContent()
var err error
filePath, err = test.GenerateTestFile(fileContent)
if err != nil {
panic(err)
}
}
func TestFilesUpload(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
resp, err := cli.Files().Upload(context.Background(), &moonshot.FilesUploadRequest{
Name: filepath.Base(filePath),
Path: filePath,
Purpose: moonshot.FilePurposeExtract,
})
if err != nil {
t.Fatal(err)
}
fileID = resp.ID
t.Logf("%+v", test.MarshalJsonToStringX(resp))
err = os.Remove(filePath)
if err != nil {
t.Fatal(err)
}
}
func TestFilesUploadBytes(t *testing.T) {
content := []byte("hello")
require := require.New(t)
// upload
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
uploadResp, err := cli.Files().UploadBytes(context.Background(), &moonshot.FilesUploadBytesRequest{
Name: "byteFile.txt",
Bytes: content,
Purpose: moonshot.FilePurposeExtract,
})
if err != nil {
t.Fatal(err)
}
require.Equal(uploadResp.Bytes, len(content))
// check content
contentResp, err := cli.Files().Content(context.Background(), uploadResp.ID)
if err != nil {
t.Fatal(err)
}
require.Equal(string(content), contentResp.Content)
// remove
deleteResp, err := cli.Files().Delete(context.Background(), uploadResp.ID)
if err != nil {
t.Fatal(err)
}
require.Equal(deleteResp.ID, uploadResp.ID)
require.Equal(deleteResp.Deleted, true)
}
func TestFilesList(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
resp, err := cli.Files().List(context.Background())
if err != nil {
t.Fatal(err)
}
t.Logf("file count: %d", len(resp.Data))
t.Logf("%+v", test.MarshalJsonToStringX(resp))
}
func TestFilesInfo(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
resp, err := cli.Files().Info(context.Background(), fileID)
if err != nil {
t.Fatal(err)
}
t.Logf("%+v", test.MarshalJsonToStringX(resp))
}
func TestFilesContent(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
resp, err := cli.Files().Content(context.Background(), fileID)
if err != nil {
t.Fatal(err)
}
require.Equal(t, []byte(resp.Content), fileContent, "file content not match")
t.Logf("%+v", test.MarshalJsonToStringX(resp))
}
func TestFilesDelete(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
resp, err := cli.Files().Delete(context.Background(), fileID)
if err != nil {
t.Fatal(err)
}
t.Logf("%+v", test.MarshalJsonToStringX(resp))
}
func TestFilesBatchDelete(t *testing.T) {
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
fileIdList := make([]string, 0)
for i := 0; i < 10; i++ {
fp, _ := test.GenerateTestFile(test.GenerateTestContent())
uploadResp, err := cli.Files().Upload(context.Background(), &moonshot.FilesUploadRequest{
Name: filepath.Base(fp),
Path: fp,
Purpose: moonshot.FilePurposeExtract,
})
if err != nil {
t.Logf("upload file err: %v", err)
continue
}
fileIdList = append(fileIdList, uploadResp.ID)
}
t.Logf("file id to delete: %v", fileIdList)
deleteAllResp, err := cli.Files().BatchDelete(context.Background(), &moonshot.FilesBatchDeleteRequest{
FileIDList: fileIdList,
})
if err != nil {
t.Fatal(err)
}
require.EqualValues(t, func(in []string) (ls []string) {
for _, v := range in {
ls = append(ls, v)
}
return
}(fileIdList), func(in []*moonshot.FilesDeleteResponse) (ls []string) {
for _, resp := range in {
ls = append(ls, resp.ID)
}
return
}(deleteAllResp.RespList), "must delete all files")
t.Logf("deleted Files ID List: %v", func(ls []*moonshot.FilesDeleteResponse) (l []string) {
for _, v := range ls {
l = append(l, v.ID)
}
return l
}(deleteAllResp.RespList))
}