forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_test.go
73 lines (63 loc) · 1.99 KB
/
files_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
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"
)
func TestFileUploadWithFailingFormBuilder(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 := FileRequest{
FileName: "test.go",
FilePath: "client.go",
Purpose: "fine-tune",
}
mockError := fmt.Errorf("mockWriteField error")
mockBuilder.mockWriteField = func(string, string) error {
return mockError
}
_, err := client.CreateFile(ctx, req)
checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails")
mockError = fmt.Errorf("mockCreateFormFile error")
mockBuilder.mockWriteField = func(string, string) error {
return nil
}
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return mockError
}
_, err = client.CreateFile(ctx, req)
checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails")
mockError = fmt.Errorf("mockClose error")
mockBuilder.mockWriteField = func(string, string) error {
return nil
}
mockBuilder.mockCreateFormFile = func(string, *os.File) error {
return nil
}
mockBuilder.mockClose = func() error {
return mockError
}
_, err = client.CreateFile(ctx, req)
checks.ErrorIs(t, err, mockError, "CreateFile should return error if form builder fails")
}
func TestFileUploadWithNonExistentPath(t *testing.T) {
config := DefaultConfig("")
config.BaseURL = ""
client := NewClientWithConfig(config)
ctx := context.Background()
req := FileRequest{
FilePath: "some non existent file path/F616FD18-589E-44A8-BF0C-891EAE69C455",
}
_, err := client.CreateFile(ctx, req)
checks.ErrorIs(t, err, os.ErrNotExist, "CreateFile should return error if file does not exist")
}