Skip to content

Commit 26d7c6b

Browse files
committed
use require package
1 parent 4d459bb commit 26d7c6b

File tree

9 files changed

+150
-248
lines changed

9 files changed

+150
-248
lines changed

complete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type CompleteResponse struct {
3636
Type string `json:"type"`
3737
ID string `json:"id"`
3838
Completion string `json:"completion"`
39-
// possible values are: stop_sequencemax_tokensnull
39+
// possible values are: stop_sequence, max_tokens, null
4040
StopReason string `json:"stop_reason"`
4141
Model Model `json:"model"`
4242
}

complete_stream_test.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010

1111
"github.com/liushuangls/go-anthropic/v2"
1212
"github.com/liushuangls/go-anthropic/v2/internal/test"
13+
"github.com/stretchr/testify/require"
1314
)
1415

1516
var (
1617
testCompletionStreamContent = []string{"My", " name", " is", " Claude", "."}
1718
)
1819

1920
func TestCompleteStream(t *testing.T) {
21+
is := require.New(t)
2022
server := test.NewTestServer()
2123
server.RegisterHandler("/v1/complete", handlerCompleteStream)
2224

@@ -38,26 +40,21 @@ func TestCompleteStream(t *testing.T) {
3840
},
3941
OnCompletion: func(data anthropic.CompleteResponse) {
4042
receivedContent += data.Completion
41-
//t.Logf("CreateCompleteStream OnCompletion data: %+v", data)
4243
},
4344
OnPing: func(data anthropic.CompleteStreamPingData) {},
4445
OnError: func(response anthropic.ErrorResponse) {},
4546
})
46-
if err != nil {
47-
t.Fatalf("CreateCompleteStream error: %s", err)
48-
}
47+
is.NoError(err)
4948

5049
expected := strings.Join(testCompletionStreamContent, "")
51-
if receivedContent != expected {
52-
t.Fatalf("CreateCompleteStream content not match expected: %s, got: %s", expected, receivedContent)
53-
}
54-
if resp.Completion != expected {
55-
t.Fatalf("CreateCompleteStream content not match expected: %s, got: %s", expected, resp.Completion)
56-
}
50+
is.Equal(expected, receivedContent)
51+
is.Equal(expected, resp.Completion)
52+
5753
t.Logf("CreateCompleteStream resp: %+v", resp)
5854
}
5955

6056
func TestCompleteStreamError(t *testing.T) {
57+
is := require.New(t)
6158
server := test.NewTestServer()
6259
server.RegisterHandler("/v1/complete", handlerCompleteStream)
6360

@@ -76,25 +73,20 @@ func TestCompleteStreamError(t *testing.T) {
7673
Model: anthropic.ModelClaudeInstant1Dot2,
7774
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
7875
MaxTokensToSample: 1000,
79-
//Temperature: &temperature,
8076
},
8177
OnCompletion: func(data anthropic.CompleteResponse) {
8278
receivedContent += data.Completion
83-
//t.Logf("CreateCompleteStream OnCompletion data: %+v", data)
8479
},
8580
OnPing: func(data anthropic.CompleteStreamPingData) {},
8681
OnError: func(response anthropic.ErrorResponse) {},
8782
}
8883
param.SetTemperature(2)
8984
_, err := client.CreateCompleteStream(context.Background(), param)
90-
if err == nil {
91-
t.Fatal("should error")
92-
}
85+
is.Error(err)
86+
is.Contains(err.Error(), "Overloaded")
9387

9488
var e *anthropic.APIError
95-
if !errors.As(err, &e) {
96-
t.Fatal("should api error")
97-
}
89+
is.True(errors.As(err, &e), "should api error")
9890

9991
t.Logf("CreateCompleteStream error: %+v", err)
10092
}

complete_test.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111

1212
"github.com/liushuangls/go-anthropic/v2"
1313
"github.com/liushuangls/go-anthropic/v2/internal/test"
14+
"github.com/stretchr/testify/require"
1415
)
1516

1617
func TestComplete(t *testing.T) {
18+
is := require.New(t)
1719
server := test.NewTestServer()
1820
server.RegisterHandler("/v1/complete", handleCompleteEndpoint)
1921

@@ -30,9 +32,7 @@ func TestComplete(t *testing.T) {
3032
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
3133
MaxTokensToSample: 1000,
3234
})
33-
if err != nil {
34-
t.Fatalf("CreateComplete error: %v", err)
35-
}
35+
is.NoError(err)
3636

3737
t.Logf("Create Complete resp: %+v", resp)
3838
})
@@ -44,55 +44,51 @@ func TestComplete(t *testing.T) {
4444
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
4545
MaxTokensToSample: 1000,
4646
})
47-
if err == nil {
48-
t.Fatalf("CreateComplete expected error, got nil")
49-
}
47+
is.Error(err)
48+
is.Contains(err.Error(), "401")
5049
})
5150
}
5251

5352
func TestSetTemperature(t *testing.T) {
53+
is := require.New(t)
5454
cr := anthropic.CompleteRequest{
5555
Model: anthropic.ModelClaudeInstant1Dot2,
5656
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
5757
MaxTokensToSample: 1000,
5858
}
5959

6060
temp := float32(0.5)
61-
6261
cr.SetTemperature(temp)
63-
if *cr.Temperature != temp {
64-
t.Fatalf("SetTemperature failed: %v", cr.Temperature)
65-
}
62+
63+
is.Equal(temp, *cr.Temperature)
6664
}
6765

6866
func TestSetTopP(t *testing.T) {
67+
is := require.New(t)
6968
cr := anthropic.CompleteRequest{
7069
Model: anthropic.ModelClaudeInstant1Dot2,
7170
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
7271
MaxTokensToSample: 1000,
7372
}
7473

7574
topP := float32(0.5)
76-
7775
cr.SetTopP(topP)
78-
if *cr.TopP != topP {
79-
t.Fatalf("SetTopP failed: %v", cr.TopP)
80-
}
76+
77+
is.Equal(topP, *cr.TopP)
8178
}
8279

8380
func TestSetTopK(t *testing.T) {
81+
is := require.New(t)
8482
cr := anthropic.CompleteRequest{
8583
Model: anthropic.ModelClaudeInstant1Dot2,
8684
Prompt: "\n\nHuman: What is your name?\n\nAssistant:",
8785
MaxTokensToSample: 1000,
8886
}
8987

9088
topK := 5
91-
9289
cr.SetTopK(topK)
93-
if *cr.TopK != topK {
94-
t.Fatalf("SetTopK failed: %v", cr.TopK)
95-
}
90+
91+
is.Equal(topK, *cr.TopK)
9692
}
9793

9894
func handleCompleteEndpoint(w http.ResponseWriter, r *http.Request) {

error_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"testing"
55

66
"github.com/liushuangls/go-anthropic/v2"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestIsXError(t *testing.T) {
11+
is := require.New(t)
1012
countBool := func(bools []bool) int {
1113
count := 0
1214
for _, b := range bools {
@@ -53,13 +55,9 @@ func TestIsXError(t *testing.T) {
5355

5456
// Expect only one error type to be true for each error
5557
numErrorType := countBool(isErrorType)
56-
if numErrorType != 1 {
57-
t.Errorf("Expected 1 error type to be true, got %d, for error %d", numErrorType, i)
58-
}
58+
is.Equal(1, numErrorType, "Expected 1 error type to be true, got %d, for error %T", numErrorType, i)
5959

6060
// Expect the error type to be true for the correct error
61-
if !isErrorType[i] {
62-
t.Errorf("Expected error type %T to be true, got false", e)
63-
}
61+
is.True(isErrorType[i], "Expected error type %T to be true, got false", e)
6462
}
6563
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/liushuangls/go-anthropic/v2
22

33
go 1.21
4+
5+
require github.com/stretchr/testify v1.9.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

message.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (m MessagesRequest) MarshalJSON() ([]byte, error) {
5959
Alias: (Alias)(m),
6060
}
6161

62-
// 根据 MultiSystem 是否为空来设置 system 字段
6362
if len(m.MultiSystem) > 0 {
6463
aux.System = m.MultiSystem
6564
} else if len(m.System) > 0 {
@@ -317,7 +316,7 @@ type MessagesUsage struct {
317316
// The number of tokens written to the cache when creating a new entry.
318317
CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
319318
// The number of tokens retrieved from the cache for associated request.
320-
CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"`
319+
CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"`
321320
}
322321

323322
type ToolDefinition struct {

0 commit comments

Comments
 (0)