-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhuggingface_test.go
104 lines (87 loc) · 2.62 KB
/
huggingface_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
package huggingface
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
// Mock HTTP Client for testing purposes
type mockHTTPClient struct {
Response []byte
Err error
}
func (c *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
if c.Err != nil {
return nil, c.Err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBuffer(c.Response)),
}, nil
}
func TestSummarization(t *testing.T) {
client := NewInferenceClient("your-token")
mockResponse := []byte(`[{"summary_text": "This is a summary"}]`)
t.Run("Successful Request", func(t *testing.T) {
// Mock HTTP Client with successful response
mockHTTP := &mockHTTPClient{Response: mockResponse}
client.httpClient = mockHTTP
req := &SummarizationRequest{
Inputs: []string{"This is a test input"},
Model: "t5-base",
}
response, err := client.Summarization(context.Background(), req)
assert.NoError(t, err)
assert.NotNil(t, response)
assert.Equal(t, "This is a summary", response[0].SummaryText)
})
t.Run("Empty Inputs", func(t *testing.T) {
req := &SummarizationRequest{
Inputs: nil, // Empty inputs
Model: "t5-base",
}
response, err := client.Summarization(context.Background(), req)
assert.Error(t, err)
assert.Nil(t, response)
assert.Equal(t, "inputs are required", err.Error())
})
t.Run("HTTP Request Error", func(t *testing.T) {
// Mock HTTP Client with error response
mockHTTP := &mockHTTPClient{Err: errors.New("request error")}
client.httpClient = mockHTTP
req := &SummarizationRequest{
Inputs: []string{"This is a test input"},
Model: "t5-base",
}
response, err := client.Summarization(context.Background(), req)
assert.Error(t, err)
assert.Nil(t, response)
assert.Equal(t, "request error", err.Error())
})
}
func TestQuestionAnswering(t *testing.T) {
client := NewInferenceClient("your-token")
t.Run("Missing question input", func(t *testing.T) {
req := &QuestionAnsweringRequest{
Model: "distilbert-base-uncased-distilled-squad",
Inputs: QuestionAnsweringInputs{
Context: "Paris is the capital of France.",
},
}
_, err := client.QuestionAnswering(context.Background(), req)
assert.EqualError(t, err, "question is required")
})
t.Run("Missing context input", func(t *testing.T) {
req := &QuestionAnsweringRequest{
Model: "distilbert-base-uncased-distilled-squad",
Inputs: QuestionAnsweringInputs{
Question: "What is the capital of France?",
},
}
_, err := client.QuestionAnswering(context.Background(), req)
assert.EqualError(t, err, "context is required")
})
}