Easily generate data based on your ecto schemas.
Installation
Add ecto_factory to your list of dependencies in mix.exs
:
def deps do
[
{:ecto_factory, "~> 0.0.7"}
]
end
Usage
Assume we're using the following User
module:
defmodule User do
use Ecto.Schema
schema "users" do
field :age, :integer
field :username, :string
field :date_of_birth, :date
field :address, :map
end
end
Now you can use EctoFactory to generate a Ecto struct with random data for each of your differnt fields
EctoFactory.schema(User)
#=> %User{
age: 230419832, #random number for an :integer
username: aduasdaitquweofads, #random string for a :string
date_of_birth: ~D[2019-10-10], #today for a :date
address: %{ #random map of string for a :map
"aduiasdoufp" => "eruiqweu"
"wfhsddha" => "uudfadsuyanbasvasd"
}
}
You can set defaults in your config:
# config/config.exs
config :ecto_factory, factories: [
user_with_defaults: { User, [
age: 99,
username: "mrmicahcooper",
date_of_birth: Date.from_iso8601!("2012-12-12")
] }
]
Which will allow you to do the following:
EctoFactory.schema(:user_with_defaults)
#=> %User{
age: 99
username: "mrmicahcooper"
date_of_birth: ~D[2012-12-12]
address: %{
"aduiasdoufp" => "eruiqweu"
"wfhsddha" => "uudfadsuyanbasvasd"
}
}
Notice the :address
is still random data
And if you don't want to use random data or defaults, you can specify your attributes at runtime:
EctoFactory.schema(User,
username: "foo",
age: 2,
date_of_birth: ~D[2019-01-01],
address: %{},
)
#=> %User{
address: %{}
age: 2,
date_of_birth: ~d[2019-01-01],
username: "foo",
}
Development
$ git clone https://github.com/mrmicahcooper/ecto_factory
$ cd ecto_factory
$ mix deps.get
$ mix test