QuickFactory behaviour (quick_factory v0.2.1)
View SourceSummary
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
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 changeset() :: atom()
Callback that returns which changeset function to use.
@callback repo() :: module()
Callback that returns the schema's repo module.
@callback schema() :: module()
Callback that returns the schema module.
Functions
Builds a schema given the factory module
and an optional
list/map of params
.
@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
.
@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
.
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.
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
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
@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