diff --git a/audio_test.go b/audio_test.go index a6dac2153..9d2abfc50 100644 --- a/audio_test.go +++ b/audio_test.go @@ -50,13 +50,13 @@ func TestAudio(t *testing.T) { ctx := context.Background() - dir, cleanup := createTestDirectory(t) + 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") - createTestFile(t, path) + test.CreateTestFile(t, path) req := AudioRequest{ FilePath: path, @@ -98,13 +98,13 @@ func TestAudioWithOptionalArgs(t *testing.T) { ctx := context.Background() - dir, cleanup := createTestDirectory(t) + 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") - createTestFile(t, path) + test.CreateTestFile(t, path) req := AudioRequest{ FilePath: path, @@ -119,27 +119,6 @@ func TestAudioWithOptionalArgs(t *testing.T) { } } -// createTestFile creates a fake file with "hello" as the content. -func createTestFile(t *testing.T, path string) { - file, err := os.Create(path) - checks.NoError(t, err, "failed to create file") - - if _, err = file.WriteString("hello"); err != nil { - t.Fatalf("failed to write to file %v", err) - } - 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) } -} - // handleAudioEndpoint Handles the completion endpoint by the test server. func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) { var err error @@ -190,10 +169,10 @@ func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) { } func TestAudioWithFailingFormBuilder(t *testing.T) { - dir, cleanup := createTestDirectory(t) + dir, cleanup := test.CreateTestDirectory(t) defer cleanup() path := filepath.Join(dir, "fake.mp3") - createTestFile(t, path) + test.CreateTestFile(t, path) req := AudioRequest{ FilePath: path, diff --git a/client_test.go b/client_test.go new file mode 100644 index 000000000..1c15985d6 --- /dev/null +++ b/client_test.go @@ -0,0 +1,22 @@ +package openai //nolint:testpackage // testing private field + +import ( + "testing" +) + +func TestClient(t *testing.T) { + const mockToken = "mock token" + client := NewClient(mockToken) + if client.config.authToken != mockToken { + t.Errorf("Client does not contain proper token") + } + + const mockOrg = "mock org" + client = NewOrgClient(mockToken, mockOrg) + if client.config.authToken != mockToken { + t.Errorf("Client does not contain proper token") + } + if client.config.OrgID != mockOrg { + t.Errorf("Client does not contain proper orgID") + } +} diff --git a/files_test.go b/files_test.go index 45b3900dc..bb06498c8 100644 --- a/files_test.go +++ b/files_test.go @@ -126,3 +126,17 @@ func TestFileUploadWithFailingFormBuilder(t *testing.T) { _, err = client.CreateFile(ctx, req) checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails") } + +func TestFileUploadWithNonExistentPath(t *testing.T) { + config := DefaultConfig("") + config.BaseURL = "" + client := NewClientWithConfig(config) + + ctx := context.Background() + req := FileRequest{ + FilePath: "some non existent file path/F616FD18-589E-44A8-BF0C-891EAE69C455", + } + + _, err := client.CreateFile(ctx, req) + checks.ErrorIs(t, err, os.ErrNotExist, "CreateFile should return error if file does not exist") +} diff --git a/form_builder_test.go b/form_builder_test.go new file mode 100644 index 000000000..78e2ec968 --- /dev/null +++ b/form_builder_test.go @@ -0,0 +1,54 @@ +package openai //nolint:testpackage // testing private field + +import ( + "github.com/sashabaranov/go-openai/internal/test" + "github.com/sashabaranov/go-openai/internal/test/checks" + + "bytes" + "errors" + "os" + "testing" +) + +type failingWriter struct { +} + +var errMockFailingWriterError = errors.New("mock writer failed") + +func (*failingWriter) Write([]byte) (int, error) { + return 0, errMockFailingWriterError +} + +func TestFormBuilderWithFailingWriter(t *testing.T) { + dir, cleanup := test.CreateTestDirectory(t) + defer cleanup() + + file, err := os.CreateTemp(dir, "") + if err != nil { + t.Errorf("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, "") + if err != nil { + t.Errorf("Error creating tmp file: %v", err) + } + file.Close() + defer os.Remove(file.Name()) + + body := &bytes.Buffer{} + builder := newFormBuilder(body) + err = builder.createFormFile("file", file) + checks.HasError(t, err, "formbuilder should return error if file is closed") + checks.ErrorIs(t, err, os.ErrClosed, "formbuilder should return error if file is closed") +} diff --git a/internal/test/helpers.go b/internal/test/helpers.go new file mode 100644 index 000000000..8461e5374 --- /dev/null +++ b/internal/test/helpers.go @@ -0,0 +1,29 @@ +package test + +import ( + "github.com/sashabaranov/go-openai/internal/test/checks" + + "os" + "testing" +) + +// CreateTestFile creates a fake file with "hello" as the content. +func CreateTestFile(t *testing.T, path string) { + file, err := os.Create(path) + checks.NoError(t, err, "failed to create file") + + if _, err = file.WriteString("hello"); err != nil { + t.Fatalf("failed to write to file %v", err) + } + 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) } +}