Generate and fill structs with fake data based on types for testing purposes
Will generate all values for all public fields of any type, including custom types eg type Foo string
. The only
type it will not generate data for will be anything that includes an interface{}
- e.g. map[string]interface{}
will not be generated as we have no knowledge to what the interface should be.
Difference with this and faker is this module will not generate specific types
like IPV4
ip address. It deals with simply generating fields on their base type. It comes with providers
to
allow this function however.
$ go get -u github.com/amwill04/randomiser
package myPkg_test
import (
"github.com/amwill04/randomiser"
"testing"
"time"
)
type Foo struct {
FieldA string
FieldB int16
FieldC *time.Time
}
func MyTest(t *testing.T) {
foo := Foo{}
r := randomise.NewRandomise(time.Now().UnixNano())
err := r.Struct(&foo)
if err != nil {
t.Fatal(err)
}
// can now use generated fields
}
Comes with the providers
that allow you to narrow down the generated field or specify it. This allows you to use
3rd party modules to generate phone numbers, email address etc.
As("use_this_value")
Between(1, 10)
// only acceptstime.Time
,[u]int[8/26/32/64]
types and any custom types of theseOneOf("option_a", "option_b", "option_c")
All arguments to the provider must be of the field type, or it will error.
You can also generate your own Provider
package myPkg_test
import (
"github.com/amwill04/randomiser"
"testing"
"time"
)
type EnumType string
const (
OptionA = "option_a"
OptionB = "option_b"
OptionC = "option_c"
)
type PhoneNumber string
type DateOfBirth time.Time
type Foo struct {
FieldA EnumType
FieldB PhoneNumber
FieldC DateOfBirth
}
func MyTest(t *testing.T) {
foo := Foo{}
r := randomise.NewRandomise(time.Now().UnixNano())
r.AddTypeConfig("FieldA", randomise.WithProvider(randomise.OneOf(OptionA, OptionB, OptionC)))
r.AddTypeConfig("FieldB", randomise.WithProvider(randomise.As("07934536665")))
r.AddTypeConfig("FieldC", randomise.WithProvider(randomise.Between(DateOfBirth(time.Date(2000, 1,1,0,0,0,0,time.UTC)), DateOfBirth(time.Now()))))
err := r.Struct(&foo)
if err != nil {
t.Fatal(err)
}
// can now use generated fields
}
If you wish to generate random values of a specific kind - e.g. email address
package myPkg_test
import (
"github.com/amwill04/randomiser"
"syreclabs.com/go/faker"
"testing"
"time"
)
type Foo struct {
FieldEmail string
}
func MyTest(t *testing.T) {
foo := Foo{}
r := randomise.NewRandomise(time.Now().UnixNano())
r.AddTypeConfig("FieldEmail", randomise.WithProvider(randomise.As(faker.Internet().Email())))
err := r.Struct(&foo)
if err != nil {
t.Fatal(err)
}
// can now use generated fields
}
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkRandom_Generate-12 574525 2043 ns/op 896 B/op 50 allocs/op
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkFakerDataNOTTagged-12 273898 4268 ns/op 1368 B/op 63 allocs/op
randomiser's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.