- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmodels_test.go
94 lines (81 loc) · 3.05 KB
/
models_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
package openai_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"
"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test/checks"
)
consttestFineTuneModelID="fine-tune-model-id"
// TestListModels Tests the list models endpoint of the API using the mocked server.
funcTestListModels(t*testing.T) {
client, server, teardown:=setupOpenAITestServer()
deferteardown()
server.RegisterHandler("/v1/models", handleListModelsEndpoint)
_, err:=client.ListModels(context.Background())
checks.NoError(t, err, "ListModels error")
}
funcTestAzureListModels(t*testing.T) {
client, server, teardown:=setupAzureTestServer()
deferteardown()
server.RegisterHandler("/openai/models", handleListModelsEndpoint)
_, err:=client.ListModels(context.Background())
checks.NoError(t, err, "ListModels error")
}
// handleListModelsEndpoint Handles the list models endpoint by the test server.
funchandleListModelsEndpoint(w http.ResponseWriter, _*http.Request) {
resBytes, _:=json.Marshal(openai.ModelsList{})
fmt.Fprintln(w, string(resBytes))
}
// TestGetModel Tests the retrieve model endpoint of the API using the mocked server.
funcTestGetModel(t*testing.T) {
client, server, teardown:=setupOpenAITestServer()
deferteardown()
server.RegisterHandler("/v1/models/text-davinci-003", handleGetModelEndpoint)
_, err:=client.GetModel(context.Background(), "text-davinci-003")
checks.NoError(t, err, "GetModel error")
}
funcTestAzureGetModel(t*testing.T) {
client, server, teardown:=setupAzureTestServer()
deferteardown()
server.RegisterHandler("/openai/models/text-davinci-003", handleGetModelEndpoint)
_, err:=client.GetModel(context.Background(), "text-davinci-003")
checks.NoError(t, err, "GetModel error")
}
// handleGetModelsEndpoint Handles the get model endpoint by the test server.
funchandleGetModelEndpoint(w http.ResponseWriter, _*http.Request) {
resBytes, _:=json.Marshal(openai.Model{})
fmt.Fprintln(w, string(resBytes))
}
funcTestGetModelReturnTimeoutError(t*testing.T) {
client, server, teardown:=setupOpenAITestServer()
deferteardown()
server.RegisterHandler("/v1/models/text-davinci-003", func(http.ResponseWriter, *http.Request) {
time.Sleep(10*time.Nanosecond)
})
ctx:=context.Background()
ctx, cancel:=context.WithTimeout(ctx, time.Nanosecond)
defercancel()
_, err:=client.GetModel(ctx, "text-davinci-003")
iferr==nil {
t.Fatal("Did not return error")
}
if!os.IsTimeout(err) {
t.Fatal("Did not return timeout error")
}
}
funcTestDeleteFineTuneModel(t*testing.T) {
client, server, teardown:=setupOpenAITestServer()
deferteardown()
server.RegisterHandler("/v1/models/"+testFineTuneModelID, handleDeleteFineTuneModelEndpoint)
_, err:=client.DeleteFineTuneModel(context.Background(), testFineTuneModelID)
checks.NoError(t, err, "DeleteFineTuneModel error")
}
funchandleDeleteFineTuneModelEndpoint(w http.ResponseWriter, _*http.Request) {
resBytes, _:=json.Marshal(openai.FineTuneModelDeleteResponse{})
fmt.Fprintln(w, string(resBytes))
}