forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_api_test.go
195 lines (175 loc) · 5.31 KB
/
files_api_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
186
187
188
189
190
191
192
193
194
195
package openai_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"testing"
"time"
"github.com/rekki/go-openai"
"github.com/rekki/go-openai/internal/test/checks"
)
func TestFileBytesUpload(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files", handleCreateFile)
req := openai.FileBytesRequest{
Name: "foo",
Bytes: []byte("foo"),
Purpose: openai.PurposeFineTune,
}
_, err := client.CreateFileBytes(context.Background(), req)
checks.NoError(t, err, "CreateFile error")
}
func TestFileUpload(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files", handleCreateFile)
req := openai.FileRequest{
FileName: "test.go",
FilePath: "client.go",
Purpose: "fine-tune",
}
_, err := client.CreateFile(context.Background(), req)
checks.NoError(t, err, "CreateFile error")
}
// handleCreateFile Handles the images endpoint by the test server.
func handleCreateFile(w http.ResponseWriter, r *http.Request) {
var err error
var resBytes []byte
// edits only accepts POST requests
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
err = r.ParseMultipartForm(1024 * 1024 * 1024)
if err != nil {
http.Error(w, "file is more than 1GB", http.StatusInternalServerError)
return
}
values := r.Form
var purpose string
for key, value := range values {
if key == "purpose" {
purpose = value[0]
}
}
file, header, err := r.FormFile("file")
if err != nil {
return
}
defer file.Close()
fileReq := openai.File{
Bytes: int(header.Size),
ID: strconv.Itoa(int(time.Now().Unix())),
FileName: header.Filename,
Purpose: purpose,
CreatedAt: time.Now().Unix(),
Object: "test-objecct",
}
resBytes, _ = json.Marshal(fileReq)
fmt.Fprint(w, string(resBytes))
}
func TestDeleteFile(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef", func(http.ResponseWriter, *http.Request) {})
err := client.DeleteFile(context.Background(), "deadbeef")
checks.NoError(t, err, "DeleteFile error")
}
func TestListFile(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files", func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.FilesList{})
fmt.Fprintln(w, string(resBytes))
})
_, err := client.ListFiles(context.Background())
checks.NoError(t, err, "ListFiles error")
}
func TestGetFile(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef", func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.File{})
fmt.Fprintln(w, string(resBytes))
})
_, err := client.GetFile(context.Background(), "deadbeef")
checks.NoError(t, err, "GetFile error")
}
func TestGetFileContent(t *testing.T) {
wantRespJsonl := `{"prompt": "foo", "completion": "foo"}
{"prompt": "bar", "completion": "bar"}
{"prompt": "baz", "completion": "baz"}
`
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, r *http.Request) {
// edits only accepts GET requests
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
fmt.Fprint(w, wantRespJsonl)
})
content, err := client.GetFileContent(context.Background(), "deadbeef")
checks.NoError(t, err, "GetFileContent error")
defer content.Close()
actual, _ := io.ReadAll(content)
if string(actual) != wantRespJsonl {
t.Errorf("Expected %s, got %s", wantRespJsonl, string(actual))
}
}
func TestGetFileContentReturnError(t *testing.T) {
wantMessage := "To help mitigate abuse, downloading of fine-tune training files is disabled for free accounts."
wantType := "invalid_request_error"
wantErrorResp := `{
"error": {
"message": "` + wantMessage + `",
"type": "` + wantType + `",
"param": null,
"code": null
}
}`
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef/content", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, wantErrorResp)
})
_, err := client.GetFileContent(context.Background(), "deadbeef")
if err == nil {
t.Fatal("Did not return error")
}
apiErr := &openai.APIError{}
if !errors.As(err, &apiErr) {
t.Fatalf("Did not return APIError: %+v\n", apiErr)
}
if apiErr.Message != wantMessage {
t.Fatalf("Expected %s Message, got = %s\n", wantMessage, apiErr.Message)
return
}
if apiErr.Type != wantType {
t.Fatalf("Expected %s Type, got = %s\n", wantType, apiErr.Type)
return
}
}
func TestGetFileContentReturnTimeoutError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/files/deadbeef/content", func(http.ResponseWriter, *http.Request) {
time.Sleep(10 * time.Nanosecond)
})
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Nanosecond)
defer cancel()
_, err := client.GetFileContent(ctx, "deadbeef")
if err == nil {
t.Fatal("Did not return error")
}
if !os.IsTimeout(err) {
t.Fatal("Did not return timeout error")
}
}