QuickFactory behaviour (quick_factory v0.2.1)

View Source

Summary

Callbacks

Callback that returns a struct with valid defaults for the schema.

Callback that returns a map with valid defaults for the schema. <=== THE MAIN CALLBACK

Callback that returns which changeset function to use.

Callback that returns the schema's repo module.

Callback that returns the schema module.

Functions

Builds a schema given the factory module and an optional list/map of params.

Builds many parameters for a schema changeset/2 function given the factory module and an optional list/map of params.

Builds the parameters for a schema changeset/2 function given the factory module and an optional list/map of params.

Removes all the instances of a schema from the database given its factory module.

Inserts a schema given the factory module and an optional list/map of params. Fails on error.

Insert as many as count schemas given the factory module and an optional list/map of params.

Shortcut for creating unique string values.

Create sequences for generating unique values.

Similar to sequence/2 but it allows for passing a start_at option to the sequence generation.

Types

build_opts()

@type build_opts() :: [{:keys, :atom | :string | :camel_string}]

Callbacks

build_struct(map)

(optional)
@callback build_struct(map()) :: struct()

Callback that returns a struct with valid defaults for the schema.

call(map)

@callback call(map()) :: map()

Callback that returns a map with valid defaults for the schema. <=== THE MAIN CALLBACK

changeset()

@callback changeset() :: atom()

Callback that returns which changeset function to use.

repo()

@callback repo() :: module()

Callback that returns the schema's repo module.

schema()

@callback schema() :: module()

Callback that returns the schema module.

Functions

build(module, params \\ %{}, options \\ [])

Builds a schema given the factory module and an optional list/map of params.

build_invalid_params(module)

@spec build_invalid_params(module()) :: map()

build_many_params(count, module, params \\ %{}, opts \\ [])

@spec build_many_params(pos_integer(), module(), keyword() | map(), build_opts()) :: [
  map()
]

Builds many parameters for a schema changeset/2 function given the factory module and an optional list/map of params.

build_params(module, params \\ %{}, opts \\ [])

@spec build_params(module(), keyword() | map(), build_opts()) :: map()

Builds the parameters for a schema changeset/2 function given the factory module and an optional list/map of params.

cleanup!(module, options \\ [])

Removes all the instances of a schema from the database given its factory module.

insert!(module, params \\ %{}, options \\ [])

@spec insert!(module(), keyword() | map(), Keyword.t()) ::
  Ecto.Schema.t() | no_return()

Inserts a schema given the factory module and an optional list/map of params. Fails on error.

insert_many!(count, module, params \\ %{}, options \\ [])

Insert as many as count schemas given the factory module and an optional list/map of params.

sequence(name)

@spec sequence(String.t()) :: String.t()

Shortcut for creating unique string values.

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

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 build(params) do
  %{
    # Will generate "username0" then "username1", etc.
    username: sequence("username")
  }
end

def build(params) do
  %{
    # Will generate "Article Title0" then "Article Title1", etc.
    title: sequence("Article Title")
  }
end

sequence(name, formatter)

@spec sequence(any(), (integer() -> any()) | [...]) :: any()

Create sequences for generating unique values.

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

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 build(params) 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

sequence(name, formatter, opts)

@spec sequence(any(), (integer() -> any()) | [...], [{:start_at, non_neg_integer()}]) ::
  any()

Similar to sequence/2 but it allows for passing a start_at option to the sequence generation.

Examples

def build(params) do
  %{
    # Will generate "me-100@foo.com" then "me-101@foo.com", etc.
    email: sequence(:email, &"me-#{&1}@foo.com", start_at: 100),
  }
end