feat: autogenerate schema associations with factories #5
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.
I'm proposing an implementation in this pr for auto generating an Ecto.Schema's association fields for factories. The factories are great for a source of truth however with complex relations the amount of calls made to create one increases. The goal is reduce the amount of code required to setup relations while being simple enough to define in one function call.
The use the association building feature you need to specify the app to load the factory modules. There are two options for configuring the behavior:
app
: OTP app nameprefix
: The factory modules prefix.for eg.
MyApp.Support.Factory.Accounts.User
has theapp
namemy_app
and the prefixFactory
for the module name.To use the factory generation for associations you must set the
relational
option to a list of association fields as atoms or:all
and thevalidate
option must be true. Associations are derived from an Ecto.Schema's Reflection and is handled by theFactoryEx.Utils.Ecto.Schema.Reflection
module.Ecto.Changeset.cast_assoc/3
is used when validating to cast the associated fields generated in thechangeset.params
.A map is created for Ecto
one
associations from the specified params. If the schema also exists in the schema factory and the relational key is set, the factory's build function is called and the specified params are merged on top of the result.There are two conveniences when building the paramters for ecto
many
associations.changeset/2
functionusing the default syntax. This allows you to apply many changes to many paramters.
{count, params}
for applying one set of changes to many paramters. This syntax only is expanded when therelational
option is set for the key.The parameters are created before being converted to a struct or changeset.
Changelog
FactoryEx.AssociationBuilder
andFactoryEx.FactoryStore
for generating parametersfor Ecto Associations with Factory.
FactoryEx.SchemaCounter.start
is now automatically when the factory_ex application starts.validate
when false deep converts params to ecto schema structs.test/support
totest/support/schema
test/support/factory