-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
30 lines (26 loc) · 865 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package utils
import (
"crypto/rand"
"fmt"
"math/big"
)
// randomDBLength is the length of the string returned by RandomDatabase()
const randomDBLength = 15
// randomDBChars is the set of characters used by RandomDatabase()
const randomDBChars = "abcdefghijklmnopqrstuvwxyz"
// RandomDatabase returns a random valid mongo database name. You can use to
// to pick a new database name for each test to isolate tests from each other
// without having to tear down the whole server.
//
// This function will panic if it cannot generate a random number.
func RandomDatabase() string {
b := make([]byte, randomDBLength)
for i := range b {
bigN, err := rand.Int(rand.Reader, big.NewInt(int64(len(randomDBChars))))
if err != nil {
panic(fmt.Errorf("error getting a random int: %s", err))
}
b[i] = randomDBChars[int(bigN.Int64())]
}
return string(b)
}