Packages
ecto_test_data_builder
0.1.0
Code that uses Ecto needs to be tested. Such tests need the database to be populated. It pays to write a test data builder for that purpose. This package makes writing that builder easier.
Current section
Files
Jump to
Current section
Files
lib/macro.ex
defmodule EctoTestDataBuilder.Macro do
@moduledoc """
Macros useful for writing a test-data builder.
"""
@doc """
Given a function that produces a single repo value, produce a function
that creates many.
B.Macro.make_plural_builder(:procedures, from: :procedure)
The new function (`procedures`) is passed a `repo` and a list of
names.
procedures(["haltering", "pregnancy checking"])
`procedures` ensures that all of the names exist in the `repo` by passing
each of them to the old function (`procedure`).
The new function is used to quicky and easily create values that have
nothing special about them other than that they exist and can be retrieved
by name.
"""
defmacro make_plural_builder(plural, from_keyword)
defmacro make_plural_builder(plural, from: singular) do
quote do
def unquote(plural)(repo, names) do
Enum.reduce(names, repo, fn name, acc ->
apply __MODULE__, unquote(singular), [acc, name, []]
end)
end
end
end
end