forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_builder_test.go
54 lines (43 loc) · 1.38 KB
/
form_builder_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
package openai //nolint:testpackage // testing private field
import (
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
"bytes"
"errors"
"os"
"testing"
)
type failingWriter struct {
}
var errMockFailingWriterError = errors.New("mock writer failed")
func (*failingWriter) Write([]byte) (int, error) {
return 0, errMockFailingWriterError
}
func TestFormBuilderWithFailingWriter(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()
file, err := os.CreateTemp(dir, "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
}
defer file.Close()
defer os.Remove(file.Name())
builder := NewFormBuilder(&failingWriter{})
err = builder.CreateFormFile("file", file)
checks.ErrorIs(t, err, errMockFailingWriterError, "formbuilder should return error if writer fails")
}
func TestFormBuilderWithClosedFile(t *testing.T) {
dir, cleanup := test.CreateTestDirectory(t)
defer cleanup()
file, err := os.CreateTemp(dir, "")
if err != nil {
t.Errorf("Error creating tmp file: %v", err)
}
file.Close()
defer os.Remove(file.Name())
body := &bytes.Buffer{}
builder := NewFormBuilder(body)
err = builder.CreateFormFile("file", file)
checks.HasError(t, err, "formbuilder should return error if file is closed")
checks.ErrorIs(t, err, os.ErrClosed, "formbuilder should return error if file is closed")
}