Skip to content

Commit

Permalink
Add build_list/3 and build_pair/2
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteiner committed Oct 9, 2015
1 parent 1a3a03a commit 8f332ce
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
42 changes: 38 additions & 4 deletions lib/ex_machina.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ defmodule ExMachina do
ExMachina.build(__MODULE__, factory_name, attrs)
end

def build_pair(factory_name, attrs \\ %{}) do
ExMachina.build_pair(__MODULE__, factory_name, attrs)
end

def build_list(number_of_factories, factory_name, attrs \\ %{}) do
ExMachina.build_list(__MODULE__, number_of_factories, factory_name, attrs)
end

def create(factory_name, attrs \\ %{}) do
ExMachina.create(__MODULE__, factory_name, attrs)
end
Expand Down Expand Up @@ -76,7 +84,7 @@ defmodule ExMachina do
def sequence(name, formatter), do: ExMachina.Sequence.next(name, formatter)

@doc """
Builds a factory with the passed in factory_name
Builds a factory with the passed in factory_name and attrs
## Example
Expand All @@ -92,6 +100,32 @@ defmodule ExMachina do
module.factory(factory_name, attrs) |> Map.merge(attrs)
end

@doc """
Builds and returns 2 records with the passed in factory_name and attrs
## Example
# Returns a list of 2 users
build_pair(:user)
"""
def build_pair(module, factory_name, attrs \\ %{}) do
ExMachina.build_list(module, 2, factory_name, attrs)
end

@doc """
Builds and returns X records with the passed in factory_name and attrs
## Example
# Returns a list of 3 users
build_list(3, :user)
"""
def build_list(module, number_of_factories, factory_name, attrs \\ %{}) do
Enum.map(1..number_of_factories, fn(_) ->
ExMachina.build(module, factory_name, attrs)
end)
end

@doc """
Builds and saves a factory with the passed in factory_name
Expand Down Expand Up @@ -119,7 +153,7 @@ defmodule ExMachina do
## Example
# Returns a list of 2 users
# Returns a list of 2 saved users
create_pair(:user)
"""
def create_pair(module, factory_name, attrs \\ %{}) do
Expand All @@ -131,8 +165,8 @@ defmodule ExMachina do
## Example
# Returns a list of 3 users
create_pair(3, :user)
# Returns a list of 3 saved users
create_list(3, :user)
"""
def create_list(module, number_of_factories, factory_name, attrs \\ %{}) do
Enum.map(1..number_of_factories, fn(_) ->
Expand Down
22 changes: 22 additions & 0 deletions test/ex_machina_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ defmodule ExMachinaTest do
}
end

test "build_pair/2 builds 2 factories" do
records = MyApp.Factories.build_pair(:user, admin: true)

expected_record = %{
id: 3,
name: "John Doe",
admin: true
}
assert records == [expected_record, expected_record]
end

test "build_list/3 builds the factory the passed in number of times" do
records = MyApp.Factories.build_list(3, :user, admin: true)

expected_record = %{
id: 3,
name: "John Doe",
admin: true
}
assert records == [expected_record, expected_record, expected_record]
end

test "create/2 builds factory and performs save with user defined save_record/1" do
record = MyApp.Factories.create(:user)

Expand Down

0 comments on commit 8f332ce

Please sign in to comment.