randomize

package
Version: v3.7.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2020 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package randomize has helpers for randomization of structs and fields

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteSlice

func ByteSlice(nextInt func() int64, ln int) []byte

ByteSlice creates a random set of bytes (non-printables included)

func Date

func Date(nextInt func() int64) time.Time

Date generates a random time.Time between 1850 and 2050. Only the Day/Month/Year columns are set so that Dates and DateTimes do not cause mismatches in the test data comparisons.

func EnumValue

func EnumValue(nextInt func() int64, enum string) (string, error)

EnumValue takes an enum field type, parses it's definition to figure out valid values, and selects a random one from within them.

func FormattedString

func FormattedString(nextInt func() int64, fieldType string) (string, bool)

FormattedString checks a field type to see if it's in a range of special values and if so returns a randomized string for it.

func MediumInt

func MediumInt(nextInt func() int64, fieldType string) (int32, bool)

MediumInt is a special case in mysql (thanks for that -_-) this function checks if the fieldtype matches and if so returns a random value in the proper range.

func MediumUint

func MediumUint(nextInt func() int64, fieldType string) (uint32, bool)

MediumUint is the unsigned version of MediumInt

func StableDBName

func StableDBName(input string) string

StableDBName takes a database name in, and generates a random string using the database name as the rand Seed. getDBNameHash is used to generate unique test database names.

func Str

func Str(nextInt func() int64, ln int) string

Str creates a randomized string from printable characters in the alphabet

func Struct

func Struct(s *Seed, str interface{}, colTypes map[string]string, canBeNull bool, blacklist ...string) error

Struct gets its fields filled with random data based on the seed. It will ignore the fields in the blacklist. It will ignore fields that have the struct tag boil:"-"

Types

type Randomizer

type Randomizer interface {
	// Randomize should panic if there's no ability to randomize with the current parameters.
	//
	// nextInt can be called to create "random" sequential integers. This is done to avoid collisions in unique columns
	// for the tests.
	//
	// fieldType is used in the cases where the actual type (string, null string etc.) can actually be multiple
	// types of things that have specific randomization requirements, like a uuid for example is a normal null.String
	// but when randomizing that null string it must create a valid uuid or the database will reject it.
	//
	// shouldBeNull is a suggestion that a field should be null in this instance. The randomize implementation
	// can ignore this if the field cannot be null either because the type doesn't support it or there
	// is no ability for a field of this type to be null.
	Randomize(nextInt func() int64, fieldType string, shouldBeNull bool)
}

Randomizer allows a field to be randomized

type Seed

type Seed int64

Seed is an atomic counter for pseudo-randomization structs. Using full randomization leads to collisions in a domain where uniqueness is an important factor.

func NewSeed

func NewSeed() *Seed

NewSeed creates a new seed for pseudo-randomization.

func (*Seed) NextInt

func (s *Seed) NextInt() int64

NextInt retrives an integer in order

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL