|
| 1 | +defmodule FactoryEx do |
| 2 | + @moduledoc """ |
| 3 | + This module defines an Ecto factory behaviour. |
| 4 | +
|
| 5 | + For defining your own factories just implement `schema/0`, `repo/0` and |
| 6 | + `build/0` callback e.g: |
| 7 | +
|
| 8 | + ```elixir |
| 9 | + defmodule MyFactory do |
| 10 | + @behaviour FactoryEx |
| 11 | +
|
| 12 | + def schema, do: MySchema |
| 13 | +
|
| 14 | + def repo, do: MyRepo |
| 15 | +
|
| 16 | + def build(params \\ %{}) do |
| 17 | + default = %{ |
| 18 | + foo: 21, |
| 19 | + bar: 42 |
| 20 | + } |
| 21 | +
|
| 22 | + Map.merge(default, params) |
| 23 | + end |
| 24 | + end |
| 25 | + ``` |
| 26 | +
|
| 27 | + And then using it in your tests as: |
| 28 | +
|
| 29 | + ```elixir |
| 30 | + # For getting a default parameter map. |
| 31 | + FactoryEx.build(MyFactory) |
| 32 | +
|
| 33 | + # For getting a default parameter map with a modification. |
| 34 | + FactoryEx.build(MyFactory, foo: 42) |
| 35 | +
|
| 36 | + # For inserting a default schema. |
| 37 | + FactoryEx.insert!(MyFactory) |
| 38 | +
|
| 39 | + # For inserting a default schema with a modification. |
| 40 | + FactoryEx.insert!(MyFactory, foo: 42) |
| 41 | + ``` |
| 42 | + """ |
| 43 | + |
| 44 | + @doc """ |
| 45 | + Callback that returns the schema module. |
| 46 | + """ |
| 47 | + @callback schema() :: module() |
| 48 | + |
| 49 | + @doc """ |
| 50 | + Callback that returns the schema's repo module. |
| 51 | + """ |
| 52 | + @callback repo() :: module() |
| 53 | + |
| 54 | + @doc """ |
| 55 | + Callback that returns a map with valid defaults for the schema. |
| 56 | + """ |
| 57 | + @callback build(map()) :: map() |
| 58 | + |
| 59 | + @doc """ |
| 60 | + Callback that returns a struct with valid defaults for the schema. |
| 61 | + """ |
| 62 | + @callback build_struct(map()) :: struct() |
| 63 | + |
| 64 | + @optional_callbacks [build_struct: 1] |
| 65 | + |
| 66 | + @doc """ |
| 67 | + Builds the parameters for a schema `changeset/2` function given the factory |
| 68 | + `module` and an optional list/map of `params`. |
| 69 | + """ |
| 70 | + @spec build_params(module()) :: map() |
| 71 | + @spec build_params(module(), keyword() | map()) :: map() |
| 72 | + def build_params(module, params \\ %{}) |
| 73 | + |
| 74 | + def build_params(module, params) when is_list(params) do |
| 75 | + build_params(module, Map.new(params)) |
| 76 | + end |
| 77 | + |
| 78 | + def build_params(module, params) do |
| 79 | + params |
| 80 | + |> module.build() |
| 81 | + |> SharedUtils.Map.deep_struct_to_map() |
| 82 | + end |
| 83 | + |
| 84 | + @doc """ |
| 85 | + Builds a schema given the factory `module` and an optional |
| 86 | + list/map of `params`. |
| 87 | + """ |
| 88 | + @spec build(module()) :: Ecto.Schema.t() |
| 89 | + @spec build(module(), keyword() | map()) :: Ecto.Schema.t() |
| 90 | + def build(module, params \\ %{}) |
| 91 | + |
| 92 | + def build(module, params) when is_list(params) do |
| 93 | + build(module, Map.new(params)) |
| 94 | + end |
| 95 | + |
| 96 | + def build(module, params) do |
| 97 | + struct!(module.schema(), module.build(params)) |
| 98 | + end |
| 99 | + |
| 100 | + @doc """ |
| 101 | + Inserts a schema given the factory `module` and an optional list/map of |
| 102 | + `params`. Fails on error. |
| 103 | + """ |
| 104 | + @spec insert!(module()) :: Ecto.Schema.t() | no_return() |
| 105 | + @spec insert!(module(), keyword() | map(), Keyword.t()) :: Ecto.Schema.t() | no_return() |
| 106 | + def insert!(module, params \\ %{}, options \\ []) |
| 107 | + |
| 108 | + def insert!(module, params, options) when is_list(params) do |
| 109 | + insert!(module, Map.new(params), options) |
| 110 | + end |
| 111 | + |
| 112 | + def insert!(module, params, options) do |
| 113 | + module |
| 114 | + |> build(params) |
| 115 | + |> module.repo().insert!(options) |
| 116 | + end |
| 117 | + |
| 118 | + @doc """ |
| 119 | + Insert as many as `count` schemas given the factory `module` and an optional |
| 120 | + list/map of `params`. |
| 121 | + """ |
| 122 | + @spec insert_many!(pos_integer(), module()) :: [Ecto.Schema.t()] |
| 123 | + @spec insert_many!(pos_integer(), module(), keyword() | map()) :: [Ecto.Schema.t()] |
| 124 | + def insert_many!(count, module, params \\ %{}, options \\ []) when count > 0 do |
| 125 | + Enum.map(1..count, fn _ -> insert!(module, params, options) end) |
| 126 | + end |
| 127 | + |
| 128 | + @doc """ |
| 129 | + Removes all the instances of a schema from the database given its factory |
| 130 | + `module`. |
| 131 | + """ |
| 132 | + @spec cleanup(module) :: {integer(), nil | [term()]} |
| 133 | + def cleanup(module, options \\ []) do |
| 134 | + module.repo().delete_all(module.schema(), options) |
| 135 | + end |
| 136 | +end |
0 commit comments