@@ -2,6 +2,7 @@ package openai_test
22
33import (
44 . "github.com/sashabaranov/go-openai"
5+ "github.com/sashabaranov/go-openai/internal/test/checks"
56
67 "context"
78 "errors"
@@ -20,25 +21,17 @@ func TestAPI(t *testing.T) {
2021 c := NewClient (apiToken )
2122 ctx := context .Background ()
2223 _ , err = c .ListEngines (ctx )
23- if err != nil {
24- t .Fatalf ("ListEngines error: %v" , err )
25- }
24+ checks .NoError (t , err , "ListEngines error" )
2625
2726 _ , err = c .GetEngine (ctx , "davinci" )
28- if err != nil {
29- t .Fatalf ("GetEngine error: %v" , err )
30- }
27+ checks .NoError (t , err , "GetEngine error" )
3128
3229 fileRes , err := c .ListFiles (ctx )
33- if err != nil {
34- t .Fatalf ("ListFiles error: %v" , err )
35- }
30+ checks .NoError (t , err , "ListFiles error" )
3631
3732 if len (fileRes .Files ) > 0 {
3833 _ , err = c .GetFile (ctx , fileRes .Files [0 ].ID )
39- if err != nil {
40- t .Fatalf ("GetFile error: %v" , err )
41- }
34+ checks .NoError (t , err , "GetFile error" )
4235 } // else skip
4336
4437 embeddingReq := EmbeddingRequest {
@@ -49,9 +42,7 @@ func TestAPI(t *testing.T) {
4942 Model : AdaSearchQuery ,
5043 }
5144 _ , err = c .CreateEmbeddings (ctx , embeddingReq )
52- if err != nil {
53- t .Fatalf ("Embedding error: %v" , err )
54- }
45+ checks .NoError (t , err , "Embedding error" )
5546
5647 _ , err = c .CreateChatCompletion (
5748 ctx ,
@@ -66,9 +57,7 @@ func TestAPI(t *testing.T) {
6657 },
6758 )
6859
69- if err != nil {
70- t .Errorf ("CreateChatCompletion (without name) returned error: %v" , err )
71- }
60+ checks .NoError (t , err , "CreateChatCompletion (without name) returned error" )
7261
7362 _ , err = c .CreateChatCompletion (
7463 ctx ,
@@ -83,20 +72,15 @@ func TestAPI(t *testing.T) {
8372 },
8473 },
8574 )
86-
87- if err != nil {
88- t .Errorf ("CreateChatCompletion (with name) returned error: %v" , err )
89- }
75+ checks .NoError (t , err , "CreateChatCompletion (with name) returned error" )
9076
9177 stream , err := c .CreateCompletionStream (ctx , CompletionRequest {
9278 Prompt : "Ex falso quodlibet" ,
9379 Model : GPT3Ada ,
9480 MaxTokens : 5 ,
9581 Stream : true ,
9682 })
97- if err != nil {
98- t .Errorf ("CreateCompletionStream returned error: %v" , err )
99- }
83+ checks .NoError (t , err , "CreateCompletionStream returned error" )
10084 defer stream .Close ()
10185
10286 counter := 0
@@ -126,9 +110,7 @@ func TestAPIError(t *testing.T) {
126110 c := NewClient (apiToken + "_invalid" )
127111 ctx := context .Background ()
128112 _ , err = c .ListEngines (ctx )
129- if err == nil {
130- t .Fatal ("ListEngines did not fail" )
131- }
113+ checks .NoError (t , err , "ListEngines did not fail" )
132114
133115 var apiErr * APIError
134116 if ! errors .As (err , & apiErr ) {
@@ -154,9 +136,7 @@ func TestRequestError(t *testing.T) {
154136 c := NewClientWithConfig (config )
155137 ctx := context .Background ()
156138 _ , err = c .ListEngines (ctx )
157- if err == nil {
158- t .Fatal ("ListEngines request did not fail" )
159- }
139+ checks .HasError (t , err , "ListEngines did not fail" )
160140
161141 var reqErr * RequestError
162142 if ! errors .As (err , & reqErr ) {
0 commit comments