-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: autogenerate schema associations with factories #5
Open
cylkdev
wants to merge
27
commits into
theblitzapp:main
Choose a base branch
from
cylkdev:feat/ecto-association-builder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
cylkdev
changed the title
feat: factory gen for schema associations
feat: autogenerate schema associations with factories
Aug 31, 2022
Just a note I'm currently working with @cylkdev to refactor this a bit before review |
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
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.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