ex_machina v2.4.0 ExMachina behaviour View Source

Defines functions for generating data

In depth examples are in the README

Link to this section Summary

Functions

Helper function to merge attributes into a factory that could be either a map or a struct.

Shortcut for creating unique string values.

Create sequences for generating unique values.

Link to this section Functions

Link to this function

merge_attributes(record, attrs) View Source
merge_attributes(struct() | map(), map()) :: struct() | map() | no_return()

Helper function to merge attributes into a factory that could be either a map or a struct.

Example

# custom factory
def article_factory(attrs) do
  title = Map.get(attrs, :title, "default title")

  article = %Article{
    title: title
  }

  merge_attributes(article, attrs)
end

Note that when trying to merge attributes into a struct, this function will raise if one of the attributes is not defined in the struct.

Shortcut for creating unique string values.

This is automatically imported into a model factory when you use ExMachina.

This is equivalent to sequence(name, &"#{name}#{&1}"). If you need to customize the returned string, see sequence/2.

Note that sequences keep growing and are not reset by ExMachina. Most of the time you won't need to reset the sequence, but when you do need to reset them, you can use ExMachina.Sequence.reset/0.

Examples

def user_factory do
  %User{
    # Will generate "username0" then "username1", etc.
    username: sequence("username")
  }
end

def article_factory do
  %Article{
    # Will generate "Article Title0" then "Article Title1", etc.
    title: sequence("Article Title")
  }
end
Link to this function

sequence(name, formatter) View Source
sequence(any(), (integer() -> any()) | [...]) :: any()

Create sequences for generating unique values.

This is automatically imported into a model factory when you use ExMachina.

The name can be any term, although it is typically an atom describing the sequence. Each time a sequence is called with the same name, its number is incremented by one.

The formatter function takes the sequence number, and returns a sequential representation of that number – typically a formatted string.

Examples

def user_factory do
  %{
    # Will generate "me-0@foo.com" then "me-1@foo.com", etc.
    email: sequence(:email, &"me-#{&1}@foo.com"),
    # Will generate "admin" then "user", "other", "admin" etc.
    role: sequence(:role, ["admin", "user", "other"])
  }
end

Link to this section Callbacks

Link to this callback

build(factory_name) View Source
build(factory_name :: atom()) :: any()

Builds a single factory.

This will defer to the [factory_name]_factory/0 callback defined in the factory module in which it is used.

Example

def user_factory do
  %{name: "John Doe", admin: false}
end

# Returns %{name: "John Doe", admin: false}
build(:user)

# Returns %{name: "John Doe", admin: true}
build(:user, admin: true)

If you want full control over the factory attributes, you can define the factory with [factory_name]_factory/1. Note that you will need to merge the attributes passed if you want to emulate ExMachina's default behavior.

Example

def article_factory(attrs) do
  title = Map.get(attrs, :title, "default title")
  slug = Article.title_to_slug(title)

  article = %Article{title: title, slug: slug}

  # merge attributes on your own
  merge_attributes(article, attrs)
end

# Returns %Article{title: "default title", slug: "default-title"}
build(:article)

# Returns %Article{title: "hello world", slug: "hello-world"}
build(:article, title: "hello world")
Link to this callback

build(factory_name, attrs) View Source
build(factory_name :: atom(), attrs :: keyword() | map()) :: any()

Link to this callback

build_list(number_of_records, factory_name) View Source
build_list(number_of_records :: integer(), factory_name :: atom()) :: list()

Builds any number of factories.

Example

# Returns a list of 3 users
build_list(3, :user)
Link to this callback

build_list(number_of_records, factory_name, attrs) View Source
build_list(
  number_of_records :: integer(),
  factory_name :: atom(),
  attrs :: keyword() | map()
) :: list()

Link to this callback

build_pair(factory_name) View Source
build_pair(factory_name :: atom()) :: list()

Builds two factories.

This is just an alias for build_list(2, factory_name, attrs).

Example

# Returns a list of 2 users
build_pair(:user)
Link to this callback

build_pair(factory_name, attrs) View Source
build_pair(factory_name :: atom(), attrs :: keyword() | map()) :: list()