Skip to content

Slim yet powerful and extensible data generator in Elixir

License

Notifications You must be signed in to change notification settings

kraftjectory/forgery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Forgery

CI Status Hex Version

Forgery is a slim yet extensible data generator in Elixir.

Installation

def deps() do
  [{:forgery, "~> 0.1"}]
end

Overview

Full documentation can be found at https://hexdocs.pm/forgery.

Forgery provides a few simple APIs to work with:

defmodule User do
  defstruct [:id, :name, :password]
end

defmodule MyFactory do
  use Forgery

  def make(:user, fields) do
    fields
    |> put_new_field(:id, lazy(make_unique_integer()))
    |> put_new_field(:name, &("user#" <> Integer.to_string(&1.id)))
    |> create_struct(User)
  end
end

iex> import MyFactory
iex>
iex> %User{} = make(:user)
iex> %User{id: 42, name: "user#42"} = make(:user, id: 42)
iex> [%User{}, %User{}] = make_many(:user, 2)

And just as simple as that!

Ecto integration

Forgery was built with easy Ecto integration in mind, though not limiting to it.

For example you use Ecto and have MyRepo. You can add a function, says insert! and insert_many!, into the factory:

defmodule MyFactory do
  def insert!(factory_name, fields \\ %{}) do
    factory_name
    |> make(fields)
    |> MyRepo.insert!()
  end

  def insert_many!(factory_name, amount, fields \\ %{}) when amount >= 1 do
    [%schema{} | _] = entities = make_many(factory_name, amount, fields)

    {^amount, persisted_entities} = MyRepo.insert_all(schema, entities, returning: true)

    persisted_entities
  end
end

user = insert!(:user)
users = insert_many!(:user, 10, %{password: "1234567890"})

Licensing

This software is licensed under the ISC license.

About

Slim yet powerful and extensible data generator in Elixir

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages