@@ -2,6 +2,7 @@ package openai_test
2
2
3
3
import (
4
4
. "github.com/sashabaranov/go-openai"
5
+ "github.com/sashabaranov/go-openai/internal/test/checks"
5
6
6
7
"context"
7
8
"errors"
@@ -20,25 +21,17 @@ func TestAPI(t *testing.T) {
20
21
c := NewClient (apiToken )
21
22
ctx := context .Background ()
22
23
_ , err = c .ListEngines (ctx )
23
- if err != nil {
24
- t .Fatalf ("ListEngines error: %v" , err )
25
- }
24
+ checks .NoError (t , err , "ListEngines error" )
26
25
27
26
_ , err = c .GetEngine (ctx , "davinci" )
28
- if err != nil {
29
- t .Fatalf ("GetEngine error: %v" , err )
30
- }
27
+ checks .NoError (t , err , "GetEngine error" )
31
28
32
29
fileRes , err := c .ListFiles (ctx )
33
- if err != nil {
34
- t .Fatalf ("ListFiles error: %v" , err )
35
- }
30
+ checks .NoError (t , err , "ListFiles error" )
36
31
37
32
if len (fileRes .Files ) > 0 {
38
33
_ , 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" )
42
35
} // else skip
43
36
44
37
embeddingReq := EmbeddingRequest {
@@ -49,9 +42,7 @@ func TestAPI(t *testing.T) {
49
42
Model : AdaSearchQuery ,
50
43
}
51
44
_ , err = c .CreateEmbeddings (ctx , embeddingReq )
52
- if err != nil {
53
- t .Fatalf ("Embedding error: %v" , err )
54
- }
45
+ checks .NoError (t , err , "Embedding error" )
55
46
56
47
_ , err = c .CreateChatCompletion (
57
48
ctx ,
@@ -66,9 +57,7 @@ func TestAPI(t *testing.T) {
66
57
},
67
58
)
68
59
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" )
72
61
73
62
_ , err = c .CreateChatCompletion (
74
63
ctx ,
@@ -83,20 +72,15 @@ func TestAPI(t *testing.T) {
83
72
},
84
73
},
85
74
)
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" )
90
76
91
77
stream , err := c .CreateCompletionStream (ctx , CompletionRequest {
92
78
Prompt : "Ex falso quodlibet" ,
93
79
Model : GPT3Ada ,
94
80
MaxTokens : 5 ,
95
81
Stream : true ,
96
82
})
97
- if err != nil {
98
- t .Errorf ("CreateCompletionStream returned error: %v" , err )
99
- }
83
+ checks .NoError (t , err , "CreateCompletionStream returned error" )
100
84
defer stream .Close ()
101
85
102
86
counter := 0
@@ -126,9 +110,7 @@ func TestAPIError(t *testing.T) {
126
110
c := NewClient (apiToken + "_invalid" )
127
111
ctx := context .Background ()
128
112
_ , 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" )
132
114
133
115
var apiErr * APIError
134
116
if ! errors .As (err , & apiErr ) {
@@ -154,9 +136,7 @@ func TestRequestError(t *testing.T) {
154
136
c := NewClientWithConfig (config )
155
137
ctx := context .Background ()
156
138
_ , 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" )
160
140
161
141
var reqErr * RequestError
162
142
if ! errors .As (err , & reqErr ) {
0 commit comments