-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add insert, insert_pair and insert_list to ExMachina.Ecto, and ExMachina.Strategy #102
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ defmodule ExMachina do | |
In depth examples are in the [README](README.html) | ||
""" | ||
|
||
defmodule UndefinedFactory do | ||
defmodule UndefinedFactoryError do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is Elixir convention to add |
||
@moduledoc """ | ||
Error raised when trying to build or create a factory that is undefined. | ||
""" | ||
|
@@ -15,26 +15,15 @@ defmodule ExMachina do | |
def exception(factory_name) do | ||
message = | ||
""" | ||
No factory defined for #{inspect factory_name}. This may be because you | ||
defined a factory with two parameters like this: | ||
No factory defined for #{inspect factory_name}. | ||
|
||
def factory(#{inspect factory_name}, attrs) | ||
|
||
As of ExMachina 0.5.0, we no longer call factory/2. Please define your | ||
factory function without the second attrs parameter: | ||
Please check for typos or define your factory: | ||
|
||
def factory(#{inspect factory_name}) do | ||
... | ||
end | ||
|
||
The assoc/3 function has also been removed. belongs_to relationships | ||
can now be used with build: | ||
|
||
def factory(#{inspect factory_name}) do | ||
parent: build(:parent) | ||
end | ||
""" | ||
%UndefinedFactory{message: message} | ||
%UndefinedFactoryError{message: message} | ||
end | ||
end | ||
|
||
|
@@ -61,7 +50,7 @@ defmodule ExMachina do | |
quote do | ||
@before_compile unquote(__MODULE__) | ||
|
||
import ExMachina, only: [sequence: 1, sequence: 2, factory: 2] | ||
import ExMachina, only: [sequence: 1, sequence: 2] | ||
|
||
def build(factory_name, attrs \\ %{}) do | ||
ExMachina.build(__MODULE__, factory_name, attrs) | ||
|
@@ -74,40 +63,9 @@ defmodule ExMachina do | |
def build_list(number_of_factories, factory_name, attrs \\ %{}) do | ||
ExMachina.build_list(__MODULE__, number_of_factories, factory_name, attrs) | ||
end | ||
|
||
def create(built_record) when is_map(built_record) do | ||
ExMachina.create(__MODULE__, built_record) | ||
end | ||
|
||
def create(factory_name, attrs \\ %{}) do | ||
ExMachina.create(__MODULE__, factory_name, attrs) | ||
end | ||
|
||
def create_pair(factory_name, attrs \\ %{}) do | ||
ExMachina.create_pair(__MODULE__, factory_name, attrs) | ||
end | ||
|
||
def create_list(number_of_factories, factory_name, attrs \\ %{}) do | ||
ExMachina.create_list(__MODULE__, number_of_factories, factory_name, attrs) | ||
end | ||
end | ||
end | ||
|
||
defmacro factory(factory_name, do: _block) do | ||
raise """ | ||
The factory and assoc macros have been removed. Please use regular | ||
functions instead. | ||
|
||
def factory(#{factory_name}) do | ||
%{ | ||
... | ||
some_assoc: build(:some_assoc) | ||
... | ||
} | ||
end | ||
""" | ||
end | ||
|
||
@doc """ | ||
Shortcut for creating unique values. Similar to sequence/2 | ||
|
||
|
@@ -188,59 +146,6 @@ defmodule ExMachina do | |
end) | ||
end | ||
|
||
@doc """ | ||
Builds and saves a factory with the passed in factory_name | ||
|
||
If using ExMachina.Ecto it will use the Ecto Repo passed in to save the | ||
record automatically. | ||
|
||
If you are not using ExMachina.Ecto, you need to define a `save_record/1` | ||
function in your module. See `save_record` docs for more information. | ||
|
||
## Example | ||
|
||
def factory(:user, _attrs) do | ||
%{name: "John Doe", admin: false} | ||
end | ||
|
||
# Saves and returns %{name: "John Doe", admin: true} | ||
create(:user, admin: true) | ||
""" | ||
|
||
def create(module, built_record) when is_map(built_record) do | ||
module.save_record(built_record) | ||
end | ||
|
||
def create(module, factory_name, attrs \\ %{}) do | ||
ExMachina.build(module, factory_name, attrs) |> module.save_record | ||
end | ||
|
||
@doc """ | ||
Creates and returns 2 records with the passed in factory_name and attrs | ||
|
||
## Example | ||
|
||
# Returns a list of 2 saved users | ||
create_pair(:user) | ||
""" | ||
def create_pair(module, factory_name, attrs \\ %{}) do | ||
ExMachina.create_list(module, 2, factory_name, attrs) | ||
end | ||
|
||
@doc """ | ||
Creates and returns X records with the passed in factory_name and attrs | ||
|
||
## Example | ||
|
||
# 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(_) -> | ||
ExMachina.create(module, factory_name, attrs) | ||
end) | ||
end | ||
|
||
defmacro __before_compile__(_env) do | ||
# We are using line -1 because we don't want warnings coming from | ||
# save_record/1 when someone defines there own save_record/1 function. | ||
|
@@ -249,7 +154,7 @@ defmodule ExMachina do | |
Raises a helpful error if no factory is defined. | ||
""" | ||
def factory(factory_name) do | ||
raise UndefinedFactory, factory_name | ||
raise UndefinedFactoryError, factory_name | ||
end | ||
|
||
@doc """ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ecto changed the function to
Ecto.build_assoc
so this no longer appliesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!