Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
h0rv committed Aug 10, 2024
1 parent c2367ef commit 3bba27c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,56 @@ func TestFinishReason(t *testing.T) {
}
}
}

func TestChatCompletionResponseFormat_JSONSchema_MarshalJSON(t *testing.T) {
tests := []struct {

Check failure on line 532 in chat_test.go

View workflow job for this annotation

GitHub Actions / Sanity check

File is not `goimports`-ed (goimports)
name string
input openai.ChatCompletionResponseFormatJSONSchema
expected string
wantErr bool
}{
{
name: "Empty Schema and SchemaRaw",
input: openai.ChatCompletionResponseFormatJSONSchema{
Name: "TestName",
SchemaRaw: nil,
Strict: false,
},
expected: `{"name":"TestName","strict":false}`,
wantErr: false,
},
{
name: "Non-empty SchemaRaw",
input: openai.ChatCompletionResponseFormatJSONSchema{
Name: "TestName",
SchemaRaw: func() *[]byte { b := []byte(`{"key":"value"}`); return &b }(),
Strict: true,
},
expected: `{"name":"TestName","schema":{"key":"value"},"strict":true}`,
wantErr: false,
},
{
name: "Invalid SchemaRaw JSON",
input: openai.ChatCompletionResponseFormatJSONSchema{
Name: "TestName",
SchemaRaw: func() *[]byte { b := []byte(`{key:value}`); return &b }(),
Strict: true,
},
expected: "",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.input.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(got) != tt.expected {
t.Errorf("MarshalJSON() got = %v, expected %v", string(got), tt.expected)
}
})
}
}

0 comments on commit 3bba27c

Please sign in to comment.