forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add form builder tests * lint * add client tests * lint * add non-existent file test
- Loading branch information
1 parent
334ee6d
commit 9a1ecf5
Showing
5 changed files
with
125 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} |