feat: Add support for StreamData generators #497
Draft
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.
This is a WIP PR that adds support for defining "generators",
StreamData
based functions that allow users to create property tests (usingExUnitProperties
), and which overloads the existingbuild
functions to use generators if they exist.The goal is to allow users to define generators like this:
And then, in tests,
build(:user)
would return the first element created from thatStreamData
stream, whilegenerate(:user)
(a new function) would return the stream itself, to be used in property tests. The benefit is that users wouldn't have to both create a ExMachina factory for their regular unit tests and aStreamData
generator for their property tests.Users may define both a generator and a factory (e.g.
user_generator
anduser_factory
), in which case the factory will take precedence over the generator when callingbuild
, but the generator will still be used forgenerate
. Using lazy attributes in generators is not supported, because usingStreamData
solves the same problem.I am not 100% sure this is useful in its current incarnation, how it should play together with the
sequence
function or how to test this properly, but some preliminary local testing using another application seemed to indicate that the basic functionality works as expected.As such, I am not looking to get this merged as is, but to start a conversation about whether something like this is desirable in ExMachina and, if so, how we might bring this PR (or another PR) to a state that could get it merged.
Inspiration: #351 and https://hexdocs.pm/ecto_stream_factory/readme.html