Skip to content

Commit

Permalink
adds completion test
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoescher committed Nov 14, 2023
1 parent 922e2e1 commit f95074a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 33 deletions.
80 changes: 47 additions & 33 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
Expand All @@ -12,35 +13,32 @@ import (
"github.com/stretchr/testify/require"
)

func TestCall(t *testing.T) {
t.Run("Call", func(t *testing.T) {
ctx := context.Background()
url := "/fake-url"
method := http.MethodPost

b := map[string]string{"name": "test"}
rJson, err := json.Marshal(b)
require.NoError(t, err)
body := bytes.NewReader(rJson)

mocked := getMockedClient(getMockedClientParams{
T: t,
Context: ctx,
URL: url,
Method: method,
Body: body,
})
defer mocked.Controller.Finish()

expected := &http.Response{Status: "200 OK"}
mocked.HttpClient.EXPECT().Do(requestMatcher{mocked.Request}).Return(expected, nil)

response, err := mocked.Client.Call(ctx, method, url, body)

// asserts
require.NoError(t, err)
require.Equal(t, expected, response)
func Test_Call(t *testing.T) {
ctx := context.Background()
url := "/fake-url"
method := http.MethodPost

b := map[string]string{"name": "test"}
rJson, err := json.Marshal(b)
require.NoError(t, err)

mocked := getMockedClient(getMockedClientParams{
T: t,
Context: ctx,
URL: url,
Method: method,
Body: bytes.NewReader(rJson),
})
defer mocked.Controller.Finish()

expected := &http.Response{Status: "200 OK"}
mocked.HttpClient.EXPECT().Do(newRequestMatcher(mocked.Request)).Return(expected, nil)

response, err := mocked.Client.Call(ctx, method, url, bytes.NewReader(rJson))

// asserts
require.NoError(t, err)
require.Equal(t, expected, response)
}

type getMockedClientParams struct {
Expand Down Expand Up @@ -89,6 +87,13 @@ type requestMatcher struct {
req *http.Request
}

// asserts interface
var _ gomock.Matcher = (*requestMatcher)(nil)

func newRequestMatcher(req *http.Request) gomock.Matcher {
return &requestMatcher{req: req}
}

func (m requestMatcher) Matches(x interface{}) bool {
// check x type
parsed, ok := x.(*http.Request)
Expand All @@ -110,12 +115,21 @@ func (m requestMatcher) Matches(x interface{}) bool {
if m.req.Header.Get("OpenAI-Organization") != parsed.Header.Get("OpenAI-Organization") {
return false
}
if m.req.Body != parsed.Body {
return false
}
return true

buf1, _ := io.ReadAll(m.req.Body)
buf2, _ := io.ReadAll(parsed.Body)
return bytes.Equal(buf1, buf2)
}

func (m requestMatcher) String() string {
return "is an http request with the same method, url, required headers and body"
bodyString, _ := io.ReadAll(m.req.Body)
fields := map[string]string{
"\nMethod": m.req.Method,
"\nURL": m.req.URL.String(),
"\nBody": string(bodyString),
"\nHeader[Authorization]": m.req.Header.Get("Authorization"),
"\nHeader[Content-Type]": m.req.Header.Get("Content-Type"),
"\nHeader[OpenAI-Organization]": m.req.Header.Get("OpenAI-Organization"),
}
return fmt.Sprintf("is an http request matching the data: %v", fields)
}
53 changes: 53 additions & 0 deletions completions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package goopenai

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"testing"

"github.com/stretchr/testify/require"
)

func Test_CreateChatCompletions_OK(t *testing.T) {
ctx := context.Background()
method := http.MethodPost

b := CreateChatCompletionsRequest{
Model: "gpt-4",
Messages: []Message{
{Role: "user", Content: "Hi!"},
},
ResponseFormat: ResponseFormat{
Type: "text",
},
}
reqJson, err := json.Marshal(b)
require.NoError(t, err)
body := bytes.NewReader(reqJson)

mocked := getMockedClient(getMockedClientParams{
T: t,
Context: ctx,
URL: completionsUrl,
Method: method,
Body: body,
})
defer mocked.Controller.Finish()

expected := CreateChatCompletionsResponse{}
responseJson, err := json.Marshal(expected)
require.NoError(t, err)
responseBody := io.NopCloser(bytes.NewReader(responseJson))

expectedHttpResponse := &http.Response{Status: "200 OK", Body: responseBody}
mocked.HttpClient.EXPECT().Do(newRequestMatcher(mocked.Request)).Return(expectedHttpResponse, nil)

response, err := mocked.Client.CreateChatCompletions(ctx, b)

// asserts
require.NoError(t, err)
require.Equal(t, expected, response)
}

0 comments on commit f95074a

Please sign in to comment.