-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
test: enhance error accumulator and form builder tests, add marshaller tests #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
76ae485
test: enhance error accumulator and form builder tests, add marshalle…
Whitea029 d0cfb0a
test: fix some issue form golangci-lint
Whitea029 ebfffde
test: gofmt form builder test
Whitea029 dc2344d
resolve merge conflicts
Whitea029 d1dd4d2
fix
Whitea029 4625965
fix
Whitea029 9dc5736
fix lint
Whitea029 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,41 +1,32 @@ | ||
| package openai_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| utils "github.com/sashabaranov/go-openai/internal" | ||
| "github.com/sashabaranov/go-openai/internal/test" | ||
| openai "github.com/sashabaranov/go-openai/internal" | ||
| "github.com/sashabaranov/go-openai/internal/test/checks" | ||
| ) | ||
|
|
||
| func TestErrorAccumulatorBytes(t *testing.T) { | ||
| accumulator := &utils.DefaultErrorAccumulator{ | ||
| Buffer: &bytes.Buffer{}, | ||
| func TestDefaultErrorAccumulator_WriteMultiple(t *testing.T) { | ||
| ea, ok := openai.NewErrorAccumulator().(*openai.DefaultErrorAccumulator) | ||
| if !ok { | ||
| t.Fatal("type assertion to *DefaultErrorAccumulator failed") | ||
| } | ||
| checks.NoError(t, ea.Write([]byte("{\"error\": \"test1\"}"))) | ||
| checks.NoError(t, ea.Write([]byte("{\"error\": \"test2\"}"))) | ||
|
|
||
| errBytes := accumulator.Bytes() | ||
| if len(errBytes) != 0 { | ||
| t.Fatalf("Did not return nil with empty bytes: %s", string(errBytes)) | ||
| } | ||
|
|
||
| err := accumulator.Write([]byte("{}")) | ||
| if err != nil { | ||
| t.Fatalf("%+v", err) | ||
| } | ||
|
|
||
| errBytes = accumulator.Bytes() | ||
| if len(errBytes) == 0 { | ||
| t.Fatalf("Did not return error bytes when has error: %s", string(errBytes)) | ||
| expected := "{\"error\": \"test1\"}{\"error\": \"test2\"}" | ||
| if string(ea.Bytes()) != expected { | ||
| t.Fatalf("Expected %q, got %q", expected, ea.Bytes()) | ||
| } | ||
| } | ||
|
|
||
| func TestErrorByteWriteErrors(t *testing.T) { | ||
| accumulator := &utils.DefaultErrorAccumulator{ | ||
| Buffer: &test.FailingErrorBuffer{}, | ||
| func TestDefaultErrorAccumulator_EmptyBuffer(t *testing.T) { | ||
| ea, ok := openai.NewErrorAccumulator().(*openai.DefaultErrorAccumulator) | ||
| if !ok { | ||
| t.Fatal("type assertion to *DefaultErrorAccumulator failed") | ||
| } | ||
| err := accumulator.Write([]byte("{")) | ||
| if !errors.Is(err, test.ErrTestErrorAccumulatorWriteFailed) { | ||
| t.Fatalf("Did not return error when write failed: %v", err) | ||
| if len(ea.Bytes()) != 0 { | ||
| t.Fatal("Buffer should be empty initially") | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| package openai_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| openai "github.com/sashabaranov/go-openai/internal" | ||
| "github.com/sashabaranov/go-openai/internal/test/checks" | ||
| ) | ||
|
|
||
| func TestJSONMarshaller_Normal(t *testing.T) { | ||
| jm := &openai.JSONMarshaller{} | ||
| data := map[string]string{"key": "value"} | ||
|
|
||
| b, err := jm.Marshal(data) | ||
| checks.NoError(t, err) | ||
| if len(b) == 0 { | ||
| t.Fatal("should return non-empty bytes") | ||
| } | ||
| } | ||
|
|
||
| func TestJSONMarshaller_InvalidInput(t *testing.T) { | ||
| jm := &openai.JSONMarshaller{} | ||
| _, err := jm.Marshal(make(chan int)) | ||
| checks.HasError(t, err, "should return error for unsupported type") | ||
| } | ||
|
|
||
| func TestJSONMarshaller_EmptyValue(t *testing.T) { | ||
| jm := &openai.JSONMarshaller{} | ||
| b, err := jm.Marshal(nil) | ||
| checks.NoError(t, err) | ||
| if string(b) != "null" { | ||
| t.Fatalf("unexpected marshaled value: %s", string(b)) | ||
| } | ||
| } |
This file contains hidden or 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,37 @@ | ||
| package openai_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| openai "github.com/sashabaranov/go-openai/internal" | ||
| "github.com/sashabaranov/go-openai/internal/test/checks" | ||
| ) | ||
|
|
||
| func TestJSONUnmarshaler_Normal(t *testing.T) { | ||
| jm := &openai.JSONUnmarshaler{} | ||
| data := []byte(`{"key":"value"}`) | ||
| var v map[string]string | ||
|
|
||
| err := jm.Unmarshal(data, &v) | ||
| checks.NoError(t, err) | ||
| if v["key"] != "value" { | ||
| t.Fatal("unmarshal result mismatch") | ||
| } | ||
| } | ||
|
|
||
| func TestJSONUnmarshaler_InvalidJSON(t *testing.T) { | ||
| jm := &openai.JSONUnmarshaler{} | ||
| data := []byte(`{invalid}`) | ||
| var v map[string]interface{} | ||
|
|
||
| err := jm.Unmarshal(data, &v) | ||
| checks.HasError(t, err, "should return error for invalid JSON") | ||
| } | ||
|
|
||
| func TestJSONUnmarshaler_EmptyInput(t *testing.T) { | ||
| jm := &openai.JSONUnmarshaler{} | ||
| var v interface{} | ||
|
|
||
| err := jm.Unmarshal(nil, &v) | ||
| checks.HasError(t, err, "should return error for nil input") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.