forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engines_test.go
47 lines (41 loc) · 1.47 KB
/
engines_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
package openai_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/casemark/go-openai"
"github.com/casemark/go-openai/internal/test/checks"
)
// TestGetEngine Tests the retrieve engine endpoint of the API using the mocked server.
func TestGetEngine(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/engines/text-davinci-003", func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.Engine{})
fmt.Fprintln(w, string(resBytes))
})
_, err := client.GetEngine(context.Background(), "text-davinci-003")
checks.NoError(t, err, "GetEngine error")
}
// TestListEngines Tests the list engines endpoint of the API using the mocked server.
func TestListEngines(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(openai.EnginesList{})
fmt.Fprintln(w, string(resBytes))
})
_, err := client.ListEngines(context.Background())
checks.NoError(t, err, "ListEngines error")
}
func TestListEnginesReturnError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusTeapot)
})
_, err := client.ListEngines(context.Background())
checks.HasError(t, err, "ListEngines did not fail")
}