Skip to content

Commit

Permalink
Add more tests (sashabaranov#241)
Browse files Browse the repository at this point in the history
* add form builder tests

* lint

* add client tests

* lint

* add non-existent file test
  • Loading branch information
sashabaranov authored Apr 9, 2023
1 parent 334ee6d commit 9a1ecf5
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 27 deletions.
33 changes: 6 additions & 27 deletions audio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
14 changes: 14 additions & 0 deletions files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
54 changes: 54 additions & 0 deletions form_builder_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions internal/test/helpers.go
Original file line number Diff line number Diff line change
@@ -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) }
}

0 comments on commit 9a1ecf5

Please sign in to comment.