Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #487: Enhance tests and fix linting issues #491

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
ealvar3z marked this conversation as resolved.
Show resolved Hide resolved
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, test.RandomInt(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
60 changes: 60 additions & 0 deletions internal/test/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test

import (
"crypto/rand"
ealvar3z marked this conversation as resolved.
Show resolved Hide resolved
"log"
"strings"
)

const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const strLen = 10
const bitLen = 0xFF

// See StackOverflow answer:
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
// RandomString generates a cryptographically secure random string of length
// strLen.
func RandomString() string {
sb := strings.Builder{}
sb.Grow(strLen)

for i := 0; i < strLen; i++ {
randomByte := make([]byte, 1)
_, err := rand.Read(randomByte)
if err != nil {
log.Fatalf("Error generating random string: %v", err)
}
randomIndex := randomByte[0] % byte(len(letters))
sb.WriteByte(letters[randomIndex])
}

return sb.String()
}

// RandomInt generates a random integer between 0 (inclusive) and 'max'
// (exclusive). We uses the crypto/rand library for generating random
// bytes. It then performs a bitwise AND operation with 0xFF to keep only the
// least significant 8 bits, effectively converting the byte to an integer. The
// resulting integer is then modulo'd with 'max'.
func RandomInt(max int) int {
var b [1]byte
_, err := rand.Read(b[:])
if err != nil {
log.Fatalf("Error generating random int: %v", err)
}
n := int(b[0]&bitLen) % max
return n
}

// RandomBool generates a cryptographically secure random boolean value.
// It reads a single byte from the crypto/rand library and uses its least
// significant bit to determine the boolean value. The function returns
// true if the least significant bit is 1, and false otherwise.
func RandomBool() bool {
var b [1]byte
_, err := rand.Read(b[:])
if err != nil {
log.Fatalf("Error generating random bool: %v", err)
}
return b[0]&1 == 1
}
Loading