Repository: sashabaranov/go-openai
Pull request #171 (2023-03-16 14:52:17)

Add tests


Patch:
diff --git a/chat_stream.go b/chat_stream.go
index 907b7a6..26e964c 100644
--- a/chat_stream.go
+++ b/chat_stream.go
@@ -78,10 +78,6 @@ func (stream *ChatCompletionStream) Close() {
 	stream.response.Body.Close()
 }
 
-func (stream *ChatCompletionStream) GetResponse() *http.Response {
-	return stream.response
-}
-
 // CreateChatCompletionStream — API call to create a chat completion w/ streaming
 // support. It sets whether to stream back partial progress. If set, tokens will be
 // sent as data-only server-sent events as they become available, with the
diff --git a/models.go b/models.go
index d6f0a23..2be91aa 100644
--- a/models.go
+++ b/models.go
@@ -40,7 +40,7 @@ type ModelsList struct {
 // ListModels Lists the currently available models,
 // and provides basic information about each model such as the model id and parent.
 func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error) {
-	req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.fullURL("/models"), nil)
+	req, err := c.requestBuilder.build(ctx, http.MethodGet, c.fullURL("/models"), nil)
 	if err != nil {
 		return
 	}


Test patch:
diff --git a/api_test.go b/api_test.go
index e8257db..202ec94 100644
--- a/api_test.go
+++ b/api_test.go
@@ -141,6 +141,9 @@ func TestAPIError(t *testing.T) {
 	if *apiErr.Code != "invalid_api_key" {
 		t.Fatalf("Unexpected API error code: %s", *apiErr.Code)
 	}
+	if apiErr.Error() == "" {
+		t.Fatal("Empty error message occured")
+	}
 }
 
 func TestRequestError(t *testing.T) {
@@ -163,6 +166,10 @@ func TestRequestError(t *testing.T) {
 	if reqErr.StatusCode != 418 {
 		t.Fatalf("Unexpected request error status code: %d", reqErr.StatusCode)
 	}
+
+	if reqErr.Unwrap() == nil {
+		t.Fatalf("Empty request error occured")
+	}
 }
 
 // numTokens Returns the number of GPT-3 encoded tokens in the given text.
diff --git a/chat_stream_test.go b/chat_stream_test.go
index b385e7b..e3da2da 100644
--- a/chat_stream_test.go
+++ b/chat_stream_test.go
@@ -116,6 +116,11 @@ func TestCreateChatCompletionStream(t *testing.T) {
 	if !errors.Is(streamErr, io.EOF) {
 		t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr)
 	}
+
+	_, streamErr = stream.Recv()
+	if !errors.Is(streamErr, io.EOF) {
+		t.Errorf("stream.Recv() did not return EOF when the stream is finished: %v", streamErr)
+	}
 }
 
 // Helper funcs.
diff --git a/request_builder_test.go b/request_builder_test.go
index c06112c..533977a 100644
--- a/request_builder_test.go
+++ b/request_builder_test.go
@@ -140,4 +140,9 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) {
 	if !errors.Is(err, errTestRequestBuilderFailed) {
 		t.Fatalf("Did not return error when request builder failed: %v", err)
 	}
+
+	_, err = client.ListModels(ctx)
+	if !errors.Is(err, errTestRequestBuilderFailed) {
+		t.Fatalf("Did not return error when request builder failed: %v", err)
+	}
 }
diff --git a/stream_test.go b/stream_test.go
index 87ef740..8f89e6b 100644
--- a/stream_test.go
+++ b/stream_test.go
@@ -93,6 +93,11 @@ func TestCreateCompletionStream(t *testing.T) {
 	if !errors.Is(streamErr, io.EOF) {
 		t.Errorf("stream.Recv() did not return EOF in the end: %v", streamErr)
 	}
+
+	_, streamErr = stream.Recv()
+	if !errors.Is(streamErr, io.EOF) {
+		t.Errorf("stream.Recv() did not return EOF when the stream is finished: %v", streamErr)
+	}
 }
 
 // A "tokenRoundTripper" is a struct that implements the RoundTripper