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 testable json marshaller (sashabaranov#161)
- Loading branch information
1 parent
ba77a64
commit 53d195c
Showing
10 changed files
with
102 additions
and
18 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
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
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
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,15 @@ | ||
package openai | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type marshaller interface { | ||
marshal(value any) ([]byte, error) | ||
} | ||
|
||
type jsonMarshaller struct{} | ||
|
||
func (jm *jsonMarshaller) marshal(value any) ([]byte, error) { | ||
return json.Marshal(value) | ||
} |
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,71 @@ | ||
package openai //nolint:testpackage // testing private field | ||
|
||
import ( | ||
"github.com/sashabaranov/go-openai/internal/test" | ||
|
||
"context" | ||
"errors" | ||
"testing" | ||
) | ||
|
||
type failingMarshaller struct{} | ||
|
||
var errTestMarshallerFailed = errors.New("test marshaller failed") | ||
|
||
func (jm *failingMarshaller) marshal(value any) ([]byte, error) { | ||
return []byte{}, errTestMarshallerFailed | ||
} | ||
|
||
func TestClientReturnMarshallerErrors(t *testing.T) { | ||
var err error | ||
ts := test.NewTestServer().OpenAITestServer() | ||
ts.Start() | ||
defer ts.Close() | ||
|
||
config := DefaultConfig(test.GetTestToken()) | ||
config.BaseURL = ts.URL + "/v1" | ||
client := NewClientWithConfig(config) | ||
client.marshaller = &failingMarshaller{} | ||
|
||
ctx := context.Background() | ||
|
||
_, err = client.CreateCompletion(ctx, CompletionRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.CreateChatCompletion(ctx, ChatCompletionRequest{Model: GPT3Dot5Turbo}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.CreateChatCompletionStream(ctx, ChatCompletionRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.CreateFineTune(ctx, FineTuneRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.Moderations(ctx, ModerationRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.Edits(ctx, EditsRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.CreateEmbeddings(ctx, EmbeddingRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
|
||
_, err = client.CreateImage(ctx, ImageRequest{}) | ||
if !errors.Is(err, errTestMarshallerFailed) { | ||
t.Fatalf("Did not return error when marshaller failed: %v", err) | ||
} | ||
} |
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