-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
887 additions
and
247 deletions.
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule FactoryEx.AppSchemaFactory do | ||
defstruct [:app, :prefix, :factories] | ||
|
||
def create(app, prefix) do | ||
app | ||
|> FactoryEx.Utils.Application.find_modules(prefix) | ||
|> Enum.reduce(%{}, &maybe_put_schema_factory/2) | ||
|> then(&struct(__MODULE__, %{app: app, prefix: prefix, factories: &1})) | ||
end | ||
|
||
defp maybe_put_schema_factory(module, acc) when is_atom(module) do | ||
if FactoryEx.Utils.Code.ensure_function_exported?(module, :schema, 0) do | ||
Map.put(acc, module.schema(), module) | ||
else | ||
acc | ||
end | ||
end | ||
|
||
def build_params(%__MODULE__{} = mapper, ecto_schema, params \\ %{}) do | ||
case Map.get(mapper.factories, ecto_schema) do | ||
nil -> | ||
raise FactoryEx.Exceptions.RuntimeError, | ||
message: """ | ||
No Factory module found for `#{ecto_schema}`! Unable to build params. | ||
Expected the `#{inspect(mapper.app)}` app to define the module with the prefix `#{inspect(mapper.prefix)}`. | ||
""" | ||
|
||
factory -> factory.build(params) | ||
end | ||
end | ||
|
||
def build_many_params(count, %__MODULE__{} = mapper, ecto_schema, params \\ %{}) do | ||
Enum.map(1..count, fn _ -> build_params(mapper, ecto_schema, params) end) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule FactoryEx.Exceptions.ArgumentError do | ||
defexception [:message] | ||
end |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule FactoryEx.Exceptions.RuntimeError do | ||
defexception [:message] | ||
end |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule FactoryEx.OptionsValidator do | ||
@moduledoc """ | ||
FactoryEx Options Validator | ||
This module defines the NimbleOptions schema for validating options. | ||
""" | ||
|
||
@build_schema_definition [ | ||
keys: [ | ||
type: {:in, [:atom, :string, :camel_string]}, | ||
doc: "Sets the type of keys to have in the built object, can be one of `:atom`, `:string` or `:camel_string`" | ||
], | ||
except: [ | ||
type: {:list, :any}, | ||
doc: "Sets the keys to drop from an enumerable. Can be a list of `:atom` or `:map`" | ||
], | ||
only: [ | ||
type: {:list, :any}, | ||
doc: "Sets the keys to take from an enumerable. Can be a list of `:atom` or `:map`" | ||
], | ||
validate: [ | ||
type: :boolean, | ||
doc: "Validate the param keys with `struct/2` or `changeset/2`. Can be `true` or `false`" | ||
] | ||
] | ||
|
||
@build_assoc_definition [ | ||
relational: [ | ||
type: :any, | ||
doc: "Sets the association fields that are autogenerated by the association loader, can be `:all` or a list of `:atom` of the key." | ||
] | ||
] | ||
|
||
@build_schema_factory_definition [ | ||
app: [ | ||
type: :atom, | ||
doc: "Sets the app to load the factory modules. Can be a `:atom`." | ||
], | ||
app: [ | ||
type: :atom, | ||
doc: "Sets the prefix of the factory modules to load. Can be a module `:atom`." | ||
] | ||
] | ||
|
||
@build_definition @build_schema_definition | ||
++ @build_assoc_definition | ||
++ @build_schema_factory_definition | ||
|
||
@doc """ | ||
FactoryEx Build Options Schema | ||
""" | ||
@spec schema :: Keyword.t() | ||
def schema, do: @build_definition | ||
|
||
@doc """ | ||
Validate Build Options. | ||
Raises on error. | ||
""" | ||
@spec validate! :: list() | ||
def validate!(options \\ []) do | ||
NimbleOptions.validate!(options, @build_definition) | ||
end | ||
|
||
end |
Oops, something went wrong.