-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
image_test.go
158 lines (128 loc) · 4.57 KB
/
image_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package openai //nolint:testpackage // testing private field
import (
utils "github.com/sashabaranov/go-openai/internal"
"github.com/sashabaranov/go-openai/internal/test/checks"
"context"
"fmt"
"io"
"os"
"testing"
)
type mockFormBuilder struct {
mockCreateFormFile func(string, *os.File) error
mockCreateFormFileReader func(string, io.Reader, string) error
mockWriteField func(string, string) error
mockClose func() error
}
func (fb *mockFormBuilder) CreateFormFile(fieldname string, file *os.File) error {
return fb.mockCreateFormFile(fieldname, file)
}
func (fb *mockFormBuilder) CreateFormFileReader(fieldname string, r io.Reader, filename string) error {
return fb.mockCreateFormFileReader(fieldname, r, filename)
}
func (fb *mockFormBuilder) WriteField(fieldname, value string) error {
return fb.mockWriteField(fieldname, value)
}
func (fb *mockFormBuilder) Close() error {
return fb.mockClose()
}
func (fb *mockFormBuilder) FormDataContentType() string {
return ""
}
func TestImageFormBuilderFailures(t *testing.T) {
config := DefaultConfig("")
config.BaseURL = ""
client := NewClientWithConfig(config)
mockBuilder := &mockFormBuilder{}
client.createFormBuilder = func(io.Writer) utils.FormBuilder {
return mockBuilder
}
ctx := context.Background()
req := ImageEditRequest{
Mask: &os.File{},
}
mockFailedErr := fmt.Errorf("mock form builder fail")
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return mockFailedErr
}
_, err := client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
mockBuilder.mockCreateFormFile = func(name string, _ *os.File) error {
if name == "mask" {
return mockFailedErr
}
return nil
}
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return nil
}
var failForField string
mockBuilder.mockWriteField = func(fieldname, _ string) error {
if fieldname == failForField {
return mockFailedErr
}
return nil
}
failForField = "prompt"
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
failForField = "n"
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
failForField = "size"
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
failForField = "response_format"
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
failForField = ""
mockBuilder.mockClose = func() error {
return mockFailedErr
}
_, err = client.CreateEditImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
}
func TestVariImageFormBuilderFailures(t *testing.T) {
config := DefaultConfig("")
config.BaseURL = ""
client := NewClientWithConfig(config)
mockBuilder := &mockFormBuilder{}
client.createFormBuilder = func(io.Writer) utils.FormBuilder {
return mockBuilder
}
ctx := context.Background()
req := ImageVariRequest{}
mockFailedErr := fmt.Errorf("mock form builder fail")
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return mockFailedErr
}
_, err := client.CreateVariImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateVariImage should return error if form builder fails")
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return nil
}
var failForField string
mockBuilder.mockWriteField = func(fieldname, _ string) error {
if fieldname == failForField {
return mockFailedErr
}
return nil
}
failForField = "n"
_, err = client.CreateVariImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateVariImage should return error if form builder fails")
failForField = "size"
_, err = client.CreateVariImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateVariImage should return error if form builder fails")
failForField = "response_format"
_, err = client.CreateVariImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateVariImage should return error if form builder fails")
failForField = ""
mockBuilder.mockClose = func() error {
return mockFailedErr
}
_, err = client.CreateVariImage(ctx, req)
checks.ErrorIs(t, err, mockFailedErr, "CreateImage should return error if form builder fails")
}