Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ linters:
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
- unconvert # Remove unnecessary type conversions
- unparam # Reports unused function parameters
- usetesting # Reports uses of functions with replacement inside the testing package
- wastedassign # wastedassign finds wasted assignment statements.
- whitespace # Tool for detection of leading and trailing whitespace
## you may want to enable
Expand Down
10 changes: 2 additions & 8 deletions audio_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ func TestAudio(t *testing.T) {

ctx := context.Background()

dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
path := filepath.Join(dir, "fake.mp3")
path := filepath.Join(t.TempDir(), "fake.mp3")
test.CreateTestFile(t, path)

req := openai.AudioRequest{
Expand Down Expand Up @@ -90,12 +87,9 @@ func TestAudioWithOptionalArgs(t *testing.T) {

ctx := context.Background()

dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
path := filepath.Join(dir, "fake.mp3")
path := filepath.Join(t.TempDir(), "fake.mp3")
test.CreateTestFile(t, path)

req := openai.AudioRequest{
Expand Down
8 changes: 2 additions & 6 deletions audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
)

func TestAudioWithFailingFormBuilder(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()
path := filepath.Join(dir, "fake.mp3")
path := filepath.Join(t.TempDir(), "fake.mp3")
test.CreateTestFile(t, path)

req := AudioRequest{
Expand Down Expand Up @@ -63,9 +61,7 @@ func TestAudioWithFailingFormBuilder(t *testing.T) {

func TestCreateFileField(t *testing.T) {
t.Run("createFileField failing file", func(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()
path := filepath.Join(dir, "fake.mp3")
path := filepath.Join(t.TempDir(), "fake.mp3")
test.CreateTestFile(t, path)

req := AudioRequest{
Expand Down
42 changes: 13 additions & 29 deletions image_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -86,24 +87,17 @@ func TestImageEdit(t *testing.T) {
defer teardown()
server.RegisterHandler("/v1/images/edits", handleEditImageEndpoint)

origin, err := os.Create("image.png")
origin, err := os.Create(filepath.Join(t.TempDir(), "image.png"))
if err != nil {
t.Error("open origin file error")
return
t.Fatalf("open origin file error: %v", err)
}
defer origin.Close()

mask, err := os.Create("mask.png")
mask, err := os.Create(filepath.Join(t.TempDir(), "mask.png"))
if err != nil {
t.Error("open mask file error")
return
t.Fatalf("open mask file error: %v", err)
}

defer func() {
mask.Close()
origin.Close()
os.Remove("mask.png")
os.Remove("image.png")
}()
defer mask.Close()

_, err = client.CreateEditImage(context.Background(), openai.ImageEditRequest{
Image: origin,
Expand All @@ -121,16 +115,11 @@ func TestImageEditWithoutMask(t *testing.T) {
defer teardown()
server.RegisterHandler("/v1/images/edits", handleEditImageEndpoint)

origin, err := os.Create("image.png")
origin, err := os.Create(filepath.Join(t.TempDir(), "image.png"))
if err != nil {
t.Error("open origin file error")
return
t.Fatalf("open origin file error: %v", err)
}

defer func() {
origin.Close()
os.Remove("image.png")
}()
defer origin.Close()

_, err = client.CreateEditImage(context.Background(), openai.ImageEditRequest{
Image: origin,
Expand Down Expand Up @@ -178,16 +167,11 @@ func TestImageVariation(t *testing.T) {
defer teardown()
server.RegisterHandler("/v1/images/variations", handleVariateImageEndpoint)

origin, err := os.Create("image.png")
origin, err := os.Create(filepath.Join(t.TempDir(), "image.png"))
if err != nil {
t.Error("open origin file error")
return
t.Fatalf("open origin file error: %v", err)
}

defer func() {
origin.Close()
os.Remove("image.png")
}()
defer origin.Close()

_, err = client.CreateVariImage(context.Background(), openai.ImageVariRequest{
Image: origin,
Expand Down
17 changes: 4 additions & 13 deletions internal/form_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package openai //nolint:testpackage // testing private field

import (
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"

"bytes"
Expand All @@ -20,31 +19,23 @@ func (*failingWriter) Write([]byte) (int, error) {
}

func TestFormBuilderWithFailingWriter(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()

file, err := os.CreateTemp(dir, "")
file, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
t.Fatalf("Error creating tmp file: %v", err)
}
defer file.Close()
defer os.Remove(file.Name())

builder := NewFormBuilder(&failingWriter{})
err = builder.CreateFormFile("file", file)
checks.ErrorIs(t, err, errMockFailingWriterError, "formbuilder should return error if writer fails")
}

func TestFormBuilderWithClosedFile(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()

file, err := os.CreateTemp(dir, "")
file, err := os.CreateTemp(t.TempDir(), "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
t.Fatalf("Error creating tmp file: %v", err)
}
file.Close()
defer os.Remove(file.Name())

body := &bytes.Buffer{}
builder := NewFormBuilder(body)
Expand Down
10 changes: 0 additions & 10 deletions internal/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ func CreateTestFile(t *testing.T, path string) {
file.Close()
}

// CreateTestDirectory creates a temporary folder which will be deleted when cleanup is called.
func CreateTestDirectory(t *testing.T) (path string, cleanup func()) {
t.Helper()

path, err := os.MkdirTemp(os.TempDir(), "")
checks.NoError(t, err)

return path, func() { os.RemoveAll(path) }
}

// TokenRoundTripper is a struct that implements the RoundTripper
// interface, specifically to handle the authentication token by adding a token
// to the request header. We need this because the API requires that each
Expand Down
2 changes: 1 addition & 1 deletion openai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func setupAzureTestServer() (client *openai.Client, server *test.ServerTest, tea
// This function approximates based on the rule of thumb stated by OpenAI:
// https://beta.openai.com/tokenizer
//
// TODO: implement an actual tokenizer for GPT-3 and Codex (once available)
// TODO: implement an actual tokenizer for GPT-3 and Codex (once available).
func numTokens(s string) int {
return int(float32(len(s)) / 4)
}
4 changes: 1 addition & 3 deletions speech_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ func TestSpeechIntegration(t *testing.T) {
defer teardown()

server.RegisterHandler("/v1/audio/speech", func(w http.ResponseWriter, r *http.Request) {
dir, cleanup := test.CreateTestDirectory(t)
path := filepath.Join(dir, "fake.mp3")
path := filepath.Join(t.TempDir(), "fake.mp3")
test.CreateTestFile(t, path)
defer cleanup()

// audio endpoints only accept POST requests
if r.Method != "POST" {
Expand Down