Skip to content

Commit 9a1ecf5

Browse files
authored
Add more tests (#241)
* add form builder tests * lint * add client tests * lint * add non-existent file test
1 parent 334ee6d commit 9a1ecf5

File tree

5 files changed

+125
-27
lines changed

5 files changed

+125
-27
lines changed

audio_test.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ func TestAudio(t *testing.T) {
5050

5151
ctx := context.Background()
5252

53-
dir, cleanup := createTestDirectory(t)
53+
dir, cleanup := test.CreateTestDirectory(t)
5454
defer cleanup()
5555

5656
for _, tc := range testcases {
5757
t.Run(tc.name, func(t *testing.T) {
5858
path := filepath.Join(dir, "fake.mp3")
59-
createTestFile(t, path)
59+
test.CreateTestFile(t, path)
6060

6161
req := AudioRequest{
6262
FilePath: path,
@@ -98,13 +98,13 @@ func TestAudioWithOptionalArgs(t *testing.T) {
9898

9999
ctx := context.Background()
100100

101-
dir, cleanup := createTestDirectory(t)
101+
dir, cleanup := test.CreateTestDirectory(t)
102102
defer cleanup()
103103

104104
for _, tc := range testcases {
105105
t.Run(tc.name, func(t *testing.T) {
106106
path := filepath.Join(dir, "fake.mp3")
107-
createTestFile(t, path)
107+
test.CreateTestFile(t, path)
108108

109109
req := AudioRequest{
110110
FilePath: path,
@@ -119,27 +119,6 @@ func TestAudioWithOptionalArgs(t *testing.T) {
119119
}
120120
}
121121

122-
// createTestFile creates a fake file with "hello" as the content.
123-
func createTestFile(t *testing.T, path string) {
124-
file, err := os.Create(path)
125-
checks.NoError(t, err, "failed to create file")
126-
127-
if _, err = file.WriteString("hello"); err != nil {
128-
t.Fatalf("failed to write to file %v", err)
129-
}
130-
file.Close()
131-
}
132-
133-
// createTestDirectory creates a temporary folder which will be deleted when cleanup is called.
134-
func createTestDirectory(t *testing.T) (path string, cleanup func()) {
135-
t.Helper()
136-
137-
path, err := os.MkdirTemp(os.TempDir(), "")
138-
checks.NoError(t, err)
139-
140-
return path, func() { os.RemoveAll(path) }
141-
}
142-
143122
// handleAudioEndpoint Handles the completion endpoint by the test server.
144123
func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
145124
var err error
@@ -190,10 +169,10 @@ func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
190169
}
191170

192171
func TestAudioWithFailingFormBuilder(t *testing.T) {
193-
dir, cleanup := createTestDirectory(t)
172+
dir, cleanup := test.CreateTestDirectory(t)
194173
defer cleanup()
195174
path := filepath.Join(dir, "fake.mp3")
196-
createTestFile(t, path)
175+
test.CreateTestFile(t, path)
197176

198177
req := AudioRequest{
199178
FilePath: path,

client_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package openai //nolint:testpackage // testing private field
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestClient(t *testing.T) {
8+
const mockToken = "mock token"
9+
client := NewClient(mockToken)
10+
if client.config.authToken != mockToken {
11+
t.Errorf("Client does not contain proper token")
12+
}
13+
14+
const mockOrg = "mock org"
15+
client = NewOrgClient(mockToken, mockOrg)
16+
if client.config.authToken != mockToken {
17+
t.Errorf("Client does not contain proper token")
18+
}
19+
if client.config.OrgID != mockOrg {
20+
t.Errorf("Client does not contain proper orgID")
21+
}
22+
}

files_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,17 @@ func TestFileUploadWithFailingFormBuilder(t *testing.T) {
126126
_, err = client.CreateFile(ctx, req)
127127
checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails")
128128
}
129+
130+
func TestFileUploadWithNonExistentPath(t *testing.T) {
131+
config := DefaultConfig("")
132+
config.BaseURL = ""
133+
client := NewClientWithConfig(config)
134+
135+
ctx := context.Background()
136+
req := FileRequest{
137+
FilePath: "some non existent file path/F616FD18-589E-44A8-BF0C-891EAE69C455",
138+
}
139+
140+
_, err := client.CreateFile(ctx, req)
141+
checks.ErrorIs(t, err, os.ErrNotExist, "CreateFile should return error if file does not exist")
142+
}

form_builder_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package openai //nolint:testpackage // testing private field
2+
3+
import (
4+
"github.com/sashabaranov/go-openai/internal/test"
5+
"github.com/sashabaranov/go-openai/internal/test/checks"
6+
7+
"bytes"
8+
"errors"
9+
"os"
10+
"testing"
11+
)
12+
13+
type failingWriter struct {
14+
}
15+
16+
var errMockFailingWriterError = errors.New("mock writer failed")
17+
18+
func (*failingWriter) Write([]byte) (int, error) {
19+
return 0, errMockFailingWriterError
20+
}
21+
22+
func TestFormBuilderWithFailingWriter(t *testing.T) {
23+
dir, cleanup := test.CreateTestDirectory(t)
24+
defer cleanup()
25+
26+
file, err := os.CreateTemp(dir, "")
27+
if err != nil {
28+
t.Errorf("Error creating tmp file: %v", err)
29+
}
30+
defer file.Close()
31+
defer os.Remove(file.Name())
32+
33+
builder := newFormBuilder(&failingWriter{})
34+
err = builder.createFormFile("file", file)
35+
checks.ErrorIs(t, err, errMockFailingWriterError, "formbuilder should return error if writer fails")
36+
}
37+
38+
func TestFormBuilderWithClosedFile(t *testing.T) {
39+
dir, cleanup := test.CreateTestDirectory(t)
40+
defer cleanup()
41+
42+
file, err := os.CreateTemp(dir, "")
43+
if err != nil {
44+
t.Errorf("Error creating tmp file: %v", err)
45+
}
46+
file.Close()
47+
defer os.Remove(file.Name())
48+
49+
body := &bytes.Buffer{}
50+
builder := newFormBuilder(body)
51+
err = builder.createFormFile("file", file)
52+
checks.HasError(t, err, "formbuilder should return error if file is closed")
53+
checks.ErrorIs(t, err, os.ErrClosed, "formbuilder should return error if file is closed")
54+
}

internal/test/helpers.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test
2+
3+
import (
4+
"github.com/sashabaranov/go-openai/internal/test/checks"
5+
6+
"os"
7+
"testing"
8+
)
9+
10+
// CreateTestFile creates a fake file with "hello" as the content.
11+
func CreateTestFile(t *testing.T, path string) {
12+
file, err := os.Create(path)
13+
checks.NoError(t, err, "failed to create file")
14+
15+
if _, err = file.WriteString("hello"); err != nil {
16+
t.Fatalf("failed to write to file %v", err)
17+
}
18+
file.Close()
19+
}
20+
21+
// CreateTestDirectory creates a temporary folder which will be deleted when cleanup is called.
22+
func CreateTestDirectory(t *testing.T) (path string, cleanup func()) {
23+
t.Helper()
24+
25+
path, err := os.MkdirTemp(os.TempDir(), "")
26+
checks.NoError(t, err)
27+
28+
return path, func() { os.RemoveAll(path) }
29+
}

0 commit comments

Comments
 (0)