add more tests (#140)

* test models endpoint

* simplify

* add fine tune tests
This commit is contained in:
sashabaranov
2023-03-09 23:56:23 +04:00
committed by GitHub
parent 11052c6106
commit 9428f6cc3d
2 changed files with 140 additions and 0 deletions

39
models_test.go Normal file
View File

@@ -0,0 +1,39 @@
package openai_test
import (
. "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
)
// TestListModels Tests the models endpoint of the API using the mocked server.
func TestListModels(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/v1/models", handleModelsEndpoint)
// create the test server
var err error
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()
config := DefaultConfig(test.GetTestToken())
config.BaseURL = ts.URL + "/v1"
client := NewClientWithConfig(config)
ctx := context.Background()
_, err = client.ListModels(ctx)
if err != nil {
t.Fatalf("ListModels error: %v", err)
}
}
// handleModelsEndpoint Handles the models endpoint by the test server.
func handleModelsEndpoint(w http.ResponseWriter, r *http.Request) {
resBytes, _ := json.Marshal(ModelsList{})
fmt.Fprintln(w, string(resBytes))
}