Add more tests (#241)
* add form builder tests * lint * add client tests * lint * add non-existent file test
This commit is contained in:
@@ -50,13 +50,13 @@ func TestAudio(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
dir, cleanup := createTestDirectory(t)
|
dir, cleanup := test.CreateTestDirectory(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
path := filepath.Join(dir, "fake.mp3")
|
path := filepath.Join(dir, "fake.mp3")
|
||||||
createTestFile(t, path)
|
test.CreateTestFile(t, path)
|
||||||
|
|
||||||
req := AudioRequest{
|
req := AudioRequest{
|
||||||
FilePath: path,
|
FilePath: path,
|
||||||
@@ -98,13 +98,13 @@ func TestAudioWithOptionalArgs(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
dir, cleanup := createTestDirectory(t)
|
dir, cleanup := test.CreateTestDirectory(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
path := filepath.Join(dir, "fake.mp3")
|
path := filepath.Join(dir, "fake.mp3")
|
||||||
createTestFile(t, path)
|
test.CreateTestFile(t, path)
|
||||||
|
|
||||||
req := AudioRequest{
|
req := AudioRequest{
|
||||||
FilePath: path,
|
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.
|
// handleAudioEndpoint Handles the completion endpoint by the test server.
|
||||||
func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
|
func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
@@ -190,10 +169,10 @@ func handleAudioEndpoint(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAudioWithFailingFormBuilder(t *testing.T) {
|
func TestAudioWithFailingFormBuilder(t *testing.T) {
|
||||||
dir, cleanup := createTestDirectory(t)
|
dir, cleanup := test.CreateTestDirectory(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
path := filepath.Join(dir, "fake.mp3")
|
path := filepath.Join(dir, "fake.mp3")
|
||||||
createTestFile(t, path)
|
test.CreateTestFile(t, path)
|
||||||
|
|
||||||
req := AudioRequest{
|
req := AudioRequest{
|
||||||
FilePath: path,
|
FilePath: path,
|
||||||
|
|||||||
22
client_test.go
Normal file
22
client_test.go
Normal file
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -126,3 +126,17 @@ func TestFileUploadWithFailingFormBuilder(t *testing.T) {
|
|||||||
_, err = client.CreateFile(ctx, req)
|
_, err = client.CreateFile(ctx, req)
|
||||||
checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails")
|
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")
|
||||||
|
}
|
||||||
|
|||||||
54
form_builder_test.go
Normal file
54
form_builder_test.go
Normal file
@@ -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")
|
||||||
|
}
|
||||||
29
internal/test/helpers.go
Normal file
29
internal/test/helpers.go
Normal file
@@ -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) }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user