View Source EctoFactory (EctoFactory v0.3.1)

EctoFactory is a super easy way to generate fake data for an Ecto Schema. It requires zero setup and generates random data based on the fields you've defined.

Link to this section Summary

Functions

Create a map with randomly generated of the passed in Ecto schema. The keys of this map are Strings

Create a map with randomly generated of the passed in Ecto schema. The keys of this map are atoms

Generate a random value. gen/1 will return a value of the atom or tuple to pass it.

Create a struct of the passed in factory

Link to this section Functions

Link to this function

attrs(factory_name, attrs \\ [])

View Source
@spec attrs(atom() | Ecto.Schema, keyword() | map()) :: map()

Create a map with randomly generated of the passed in Ecto schema. The keys of this map are Strings

Link to this function

build(factory_name, attrs \\ [])

View Source
@spec build(atom() | Ecto.Schema, keyword() | map()) :: map()

Create a map with randomly generated of the passed in Ecto schema. The keys of this map are atoms

@spec gen(atom() | tuple()) ::
  integer()
  | binary()
  | float()
  | boolean()
  | list()
  | Time
  | NaiveDateTime
  | Date
  | DateTime

Generate a random value. gen/1 will return a value of the atom or tuple to pass it.

Link to this function

schema(factory_name, attrs \\ [])

View Source
@spec schema(atom() | Ecto.Schema, keyword() | map()) :: Ecto.Schema

Create a struct of the passed in factory

After configuring a factory

config :ecto_factory, factories: [
  user: User,

  user_with_default_username: { User,
    username: "mrmicahcooper"
  }
]

You can build a struct with the attributes from your factory as defaults.

EctoFactory.schema(:user_with_default_username)

%User{
  age: 124309132# random number
  username: "mrmicahcooper"
}

And you can pass in your own attributes of course:

EctoFactory.schema(:user, age: 99, username: "hashrocket")

%User{
  age: 99,
  username: "hashrocket"
}