Skip to content

Commit

Permalink
Resolve sashabaranov#487: Enhance tests and fix linting issues
Browse files Browse the repository at this point in the history
- Add randomized fields to Engine structs in tests
- Implement cryptographically secure random utility functions
- Address golangci-lint warnings
  • Loading branch information
ealvar3z committed Sep 12, 2023
1 parent 8e4b796 commit 57a4fb4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
20 changes: 18 additions & 2 deletions engines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,27 @@ import (
"testing"

. "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
)

// Helper function to test the ListEngines endpoint.
func RandomEngine() Engine {
return Engine{
ID: test.RandomString(),
Object: test.RandomString(),
Owner: test.RandomString(),
Ready: test.RandomBool(),
}
}

// TestGetEngine Tests the retrieve engine endpoint of the API using the mocked server.
func TestGetEngine(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/engines/text-davinci-003", func(w http.ResponseWriter, r *http.Request) {
resBytes, _ := json.Marshal(Engine{})
engine := RandomEngine()
resBytes, _ := json.Marshal(engine)
fmt.Fprintln(w, string(resBytes))
})
_, err := client.GetEngine(context.Background(), "text-davinci-003")
Expand All @@ -28,7 +40,11 @@ func TestListEngines(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, r *http.Request) {
resBytes, _ := json.Marshal(EnginesList{})
engines := make([]Engine, 5)
for i := range engines {
engines[i] = RandomEngine()
}
resBytes, _ := json.Marshal(EnginesList{Engines: engines})
fmt.Fprintln(w, string(resBytes))
})
_, err := client.ListEngines(context.Background())
Expand Down
35 changes: 35 additions & 0 deletions internal/test/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test

import (
"crypto/rand"
"math/big"
)

var (
strLen = 10
//nolint:gomnd // this avoids the golangci-lint "magic number" warning
n = big.NewInt(2)
)

// RandomString generates a cryptographically secure random string of a fixed
// length. The string is composed of alphanumeric characters and is generated
// using the crypto/rand library. The length of the string is determined by the
// constant strLen.
func RandomString() string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
s := make([]rune, strLen)
max := big.NewInt(int64(len(letters)))
for i := range s {
randomInt, _ := rand.Int(rand.Reader, max)
s[i] = letters[randomInt.Int64()]
}
return string(s)
}

// RandomBool generates a cryptographically secure random boolean value. It
// uses the crypto/rand library to generate a random integer (either 0 or 1),
// and returns true if the integer is 1, and false otherwise.
func RandomBool() bool {
randomInt, _ := rand.Int(rand.Reader, n)
return randomInt.Int64() == 1
}

0 comments on commit 57a4fb4

Please sign in to comment.