feat: autogenerate schema associations with factories#5
Open
cylkdev wants to merge 27 commits into
Open
Conversation
Contributor
|
Just a note I'm currently working with @cylkdev to refactor this a bit before review |
MikaAK
marked this pull request as draft
September 1, 2022 04:51
cylkdev
force-pushed
the
feat/ecto-association-builder
branch
from
September 4, 2022 20:02
8d3ba29 to
c5c3797
Compare
cylkdev
force-pushed
the
feat/ecto-association-builder
branch
from
October 16, 2022 21:42
c5c3797 to
1c3e75c
Compare
cylkdev
force-pushed
the
feat/ecto-association-builder
branch
2 times, most recently
from
November 24, 2022 20:30
04b0fa7 to
789e171
Compare
cylkdev
force-pushed
the
feat/ecto-association-builder
branch
from
March 31, 2023 15:40
41fd08b to
2f8cd35
Compare
MikaAK
reviewed
Jun 30, 2023
MikaAK
reviewed
Jun 30, 2023
MikaAK
reviewed
Jun 30, 2023
MikaAK
reviewed
Jun 30, 2023
cylkdev
force-pushed
the
feat/ecto-association-builder
branch
from
October 30, 2023 23:09
e6dffdf to
4b8b38c
Compare
cylkdev
marked this pull request as ready for review
October 30, 2023 23:12
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.Userhas theappnamemy_appand the prefixFactoryfor the module name.To use the factory generation for associations you must set the
relationaloption to a list of association fields as atoms or:alland thevalidateoption must be true. Associations are derived from an Ecto.Schema's Reflection and is handled by theFactoryEx.Utils.Ecto.Schema.Reflectionmodule.Ecto.Changeset.cast_assoc/3is used when validating to cast the associated fields generated in thechangeset.params.A map is created for Ecto
oneassociations 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
manyassociations.changeset/2functionusing 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 therelationaloption is set for the key.The parameters are created before being converted to a struct or changeset.
Changelog
FactoryEx.AssociationBuilderandFactoryEx.FactoryStorefor generating parametersfor Ecto Associations with Factory.
FactoryEx.SchemaCounter.startis now automatically when the factory_ex application starts.validatewhen false deep converts params to ecto schema structs.test/supporttotest/support/schematest/support/factory