Skip to content

Commit

Permalink
Resolve code review comments in PR #491
Browse files Browse the repository at this point in the history
- Add a seed for the random number generator to ensure that the tests are deterministic.
- Move the expected engine outside of the handler
- Add logic to compare the ID of the engine object returned by the client with the expected engine object.
  • Loading branch information
ealvar3z committed Sep 26, 2023
1 parent d8c78aa commit dbb476e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
14 changes: 11 additions & 3 deletions engines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@ func RandomEngine() Engine {

// TestGetEngine Tests the retrieve engine endpoint of the API using the mocked server.
func TestGetEngine(t *testing.T) {
test.Seed(42) // Seed the RNG
client, server, teardown := setupOpenAITestServer()
defer teardown()

expectedEngine := RandomEngine() // move outside of handler per code review comment
server.RegisterHandler("/v1/engines/text-davinci-003", func(w http.ResponseWriter, r *http.Request) {
engine := RandomEngine()
resBytes, _ := json.Marshal(engine)
resBytes, _ := json.Marshal(expectedEngine)
fmt.Fprintln(w, string(resBytes))
})
_, err := client.GetEngine(context.Background(), "text-davinci-003")
actualEngine, err := client.GetEngine(context.Background(), "text-davinci-003")
checks.NoError(t, err, "GetEngine error")

// Compare the two using only one field per code review comment
if actualEngine.ID != expectedEngine.ID {
t.Errorf("Engine ID mismatch: got %s, expected %s", actualEngine.ID, expectedEngine.ID)
}
}

// TestListEngines Tests the list engines endpoint of the API using the mocked server.
func TestListEngines(t *testing.T) {
test.Seed(42) // Seed the RNG
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/engines", func(w http.ResponseWriter, r *http.Request) {
Expand Down
50 changes: 19 additions & 31 deletions internal/test/random.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,48 @@
package test

import (
"crypto/rand"
"log"
"math/rand"
"strings"
"time"
)

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

// #nosec G404
var r = rand.New(rand.NewSource(time.Now().UnixNano()))

// Seeding func.
// #nosec G404
func Seed(s int64) {
r = rand.New(rand.NewSource(s))
}

// 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
// RandomString generates a 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))
randomIndex := r.Intn(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'.
// (exclusive).
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
return r.Intn(max)
}

// 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.
// RandomBool generates a random boolean value.
// #nosec G404
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
n := 2 // #gomnd (golangci-lint magic number suppression)
return r.Intn(n) == 1
}

0 comments on commit dbb476e

Please sign in to comment.