Skip to content

Clear _original_params after _generate and during SubFactory evaluation - #1160

Open
DYNOSuprovo wants to merge 1 commit into
FactoryBoy:masterfrom
DYNOSuprovo:fix/clear-original-params-subfactory-1099
Open

DYNOSuprovo wants to merge 1 commit into
FactoryBoy:masterfrom
DYNOSuprovo:fix/clear-original-params-subfactory-1099

Conversation

@DYNOSuprovo

Copy link
Copy Markdown

Summary

Fixes #1099.

When a DjangoModelFactory or SQLAlchemyModelFactory is called directly via .create(**params) or .build(**params), _generate() sets cls._original_params = params on the factory class so _get_or_create() can recover from an IntegrityError using the caller-provided lookup fields. However:

  1. _generate() never reset cls._original_params back to None upon completion (or when an exception was raised), leaving stale parameters attached to the factory class across subsequent calls.
  2. When the same factory was later invoked via SubFactory (which calls step.recurse() directly rather than _generate()), the stale cls._original_params from the previous direct call remained set on the class. This caused _get_or_create() (or custom _create() methods inspecting _original_params) to use stale parameters from an earlier call instead of None.

Changes

  • Reset cls._original_params = None in a try ... finally block in DjangoModelFactory._generate() and SQLAlchemyModelFactory._generate().
  • Temporarily clear _original_params on the target subfactory during SubFactory.evaluate() (and restore any outer _original_params afterward to support nested/self-referential subfactories).
  • Add regression tests in tests/test_django.py and tests/test_alchemy.py.

Fixes FactoryBoy#1099

Signed-off-by: DYNOSuprovo <DYNOSuprovo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_original_params do not get cleared out when the factory is used as a SubFactory

1 participant