-
Notifications
You must be signed in to change notification settings - Fork 399
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
Trait drops SubFactory default value, at least with Django #542
Comments
Thanks for the report! This is indeed a nasty bug. Under the hood, we end up with the following declaration: >>> BarFactory._meta.declarations
{'foo': <factory.declarations.SubFactory object at 0x7f65070f8320>,
'foo__value': Maybe(
<SelfAttribute('done', default=False)>,
yes=3,
no=<factory.declarations.Skip object at 0x7f650afa71d0>
)} This means that:
|
I tried to fix this with my own custom Trait. May be you can use it to fix this bug class CustomTrait(Trait):
def as_declarations(self, field_name, declarations):
overrides = {}
for maybe_field, new_value in self.overrides.items():
overrides[maybe_field] = Maybe(
decider=SelfAttribute(
'%s.%s' % (
'.' * maybe_field.count(enums.SPLITTER),
field_name,
),
default=False,
),
yes_declaration=new_value,
no_declaration=_get_maybe_field(declarations, maybe_field),
)
return overrides
def _get_maybe_field(declarations, maybe_field):
field_chain = maybe_field.split('__')
default_factory = declarations.get(field_chain[0])
if not default_factory:
return SKIP
if type(default_factory) is SubFactory:
sub_maybe_field = '__'.join(field_chain[1:])
sub_declarations = default_factory.factory_wrapper.factory._meta.declarations
return _get_maybe_field(sub_declarations, sub_maybe_field)
if issubclass(type(default_factory), BaseDeclaration) and len(field_chain) == 1:
return default_factory
return SKIP |
Getting the same issue, but just extending |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
When using
Trait
to change the value of a dependant object that has been defined with a default value, this default value is dropped when building / creating.Model / Factory code
The issue
BarFactory.build().foo.value
should be2
, but it ends up being0
.If the
value=2
in theBarFactory.foo
SubFactory is removed, the value should be1
, but it still ends up being0
.The behavior is the same with
.create()
instead of.build()
Notes
Factory version is 2.11.1
The text was updated successfully, but these errors were encountered: