From 3bba27cd939f314825db7e6fc686811de27af9c4 Mon Sep 17 00:00:00 2001 From: Robby Date: Sat, 10 Aug 2024 18:14:46 -0400 Subject: [PATCH] add unit tests --- chat_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/chat_test.go b/chat_test.go index 520bf5ca4..21cdb2bce 100644 --- a/chat_test.go +++ b/chat_test.go @@ -527,3 +527,56 @@ func TestFinishReason(t *testing.T) { } } } + +func TestChatCompletionResponseFormat_JSONSchema_MarshalJSON(t *testing.T) { + tests := []struct { + 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) + } + }) + } +}