fixtures

package
v0.55.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Fixtures

Fixture helps you create randomized input values. This allows you to stress test your project with built in tools like:

go test -count n

Usage

Fixture creation
package main_test

import "github.com/adamluzsi/testcase/fixtures"

func Example() {
	type ExampleStruct struct {
		Name string
	}

	_ = fixtures.New(ExampleStruct{}).(*ExampleStruct)
}
Random
package main_test

import (
	"math/rand"
	"time"

	"github.com/adamluzsi/testcase/random"
)

func Example() {
	rnd := random.NewRandom(rand.NewSource(time.Now().Unix()))

	_ = rnd.Bool()
	_ = rnd.Float32()
	_ = rnd.Float64()
	_ = rnd.Int()
	_ = rnd.IntBetween(24, 42)
	_ = rnd.IntN(42)
	_ = rnd.String()
	_ = rnd.StringN(42)
	_ = rnd.Time()
	_ = rnd.TimeBetween(time.Now(), time.Now().Add(time.Hour))
	_ = rnd.ElementFromSlice([]string{`foo`, `bar`, `baz`}).(string)
	_ = rnd.KeyFromMap(map[string]struct{}{
		`foo`: {},
		`bar`: {},
		`baz`: {},
	}).(string)
}

or as a shortcut you can use random initialized by default in the fixtures package:

package main_test

import (
	"math/rand"
	"time"

	"github.com/adamluzsi/testcase/fixtures"
)

func Example() {
	_ = fixtures.Random.Bool()
	_ = fixtures.Random.Float32()
	_ = fixtures.Random.Float64()
	_ = fixtures.Random.Int()
	_ = fixtures.Random.IntBetween(24, 42)
	_ = fixtures.Random.IntN(42)
	_ = fixtures.Random.String()
	_ = fixtures.Random.StringN(42)
	_ = fixtures.Random.Time()
	_ = fixtures.Random.TimeBetween(time.Now(), time.Now().Add(time.Hour))
	_ = fixtures.Random.ElementFromSlice([]string{`foo`, `bar`, `baz`}).(string)
	_ = fixtures.Random.KeyFromMap(map[string]struct{}{
		`foo`: {},
		`bar`: {},
		`baz`: {},
	}).(string)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Random = random.New(rand.NewSource(time.Now().Unix()))
View Source
var SecureRandom = random.New(random.CryptoSeed{})

Functions

func New

func New(T interface{}, opts ...Option) (pointer interface{})

New returns a populated entity for a given business data entity. This is primary and only used for testing. With fixtures, it become easy to create generic query objects that can be used during testing with randomly generated data.

DEPRECATED: please consider using fixtures.Factory instead

Example
package main

import (
	"github.com/adamluzsi/testcase/fixtures"
)

func main() {
	type ExampleStruct struct {
		Name string
	}

	_ = fixtures.New(ExampleStruct{}).(*ExampleStruct)
}
Output:

Types

type Factory added in v0.42.0

type Factory struct {
	Random      *random.Random
	StubContext context.Context
	Options     []Option
	// contains filtered or unexported fields
}

func (*Factory) Fixture added in v0.55.0

func (f *Factory) Fixture(T interface{}, ctx context.Context) (_T interface{})

func (*Factory) RegisterType added in v0.42.0

func (f *Factory) RegisterType(T any, ff FactoryFunc)

type FactoryFunc added in v0.42.0

type FactoryFunc func(context.Context) any

type Option added in v0.31.0

type Option interface {
	// contains filtered or unexported methods
}

func SkipByTag added in v0.31.0

func SkipByTag(tag string, values ...string) Option

SkipByTag is an Option to skip a certain tag during the New function value population. If value is not provided, all matching tag will be skipped. If value or multiple value is provided, then matching tag only skipped if it matches the values.

Example
package main

import (
	"github.com/adamluzsi/testcase/fixtures"
)

func main() {
	type Entity struct {
		ID    string `external-resource:"ID" json:"id"`
		Value string `external-resource:"something" json:"value"`
	}

	skipByTagOption := fixtures.SkipByTag("external-resource", "ID")
	ent := fixtures.New(Entity{}, skipByTagOption).(*Entity)
	_ = ent.ID    // no value populated
	_ = ent.Value // value populated
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL