Open sobolevn opened 5 years ago
I can take this issue as well as https://github.com/pytest-dev/pytest-factoryboy/issues/65, because it affected me several times. Could someone give me some directions firstly? I am still looking for the clearest solution for that
Awesome, @skarzi! I would appreciate a fix. Sadly, I am not familiar with the codebase.
Currently it's really hard to implement factory.declarations.List
and factory.declarations.Dict
support in a proper way, because of few reasons:
SubFactory
subclasses with factory_class
set to concrete, but not registered by pytest-factoryboy
factoriesfactory.base.ListFactory
model is set to list
and factory.base.DictFactory
is set to dict
so by default pytest-factoryboy
expects to find list
and dict
fixtures, we can register such fixtures but this will override builtin list
and dict
in some places. factory.declarations.List
is almost always used with params
(saved in defaults
attribute on instances of ParameteredAttribute
subclasses), but params
aren't supported by pytest-factoryboy
- this is connected with following issues https://github.com/pytest-dev/pytest-factoryboy/issues/40, https://github.com/pytest-dev/pytest-factoryboy/issues/90 - but in my opinion following code is the simplest example of this issue:from dataclasses import dataclass
import factory
from pytest_factoryboy import LazyFixture, register
@dataclass
class Author:
full_name: str
@dataclass
class Book:
author: Author
title: str
class AuthorFactory(factory.Factory):
full_name = factory.Sequence(lambda counter: f'Author {counter}')
class Meta:
model = Author
class LemonySnicketBookFactory(factory.Factory):
author = factory.SubFactory(AuthorFactory, full_name='Lemony Snicket')
title = factory.Sequence(lambda counter: f'A Series of Unfortunate Events {counter}')
class Meta:
model = Book
register(AuthorFactory)
register(LemonySnicketBookFactory, _name='lemony_snicket_book')
def test_subfactory_defaults(lemony_snicket_book):
"""Failure because ``lemony_snicket_book.author.full_name`` is ``'Author 1'``"""
assert lemony_snicket_book.author.full_name == 'Lemony Snicket'
So with my current knowledge, I see 2 possible solutions:
params
into the codebase and then add some more logic related to factory.declarations.List
and factory.declarations.Dict
somewhere in register
functionfactory.declarations.List
and factory.declarations.Dict
for instance, generate unique fixture for every instance of these classesI am still not so much into pytest-factoryboy
and factoryboy
codebase, because it's really dynamic and sometimes hard to debug code, so any responses with feedback or any advice is really welcome!
@youtux @hugovk maybe do you have any ideas/insights about this issue and the comment I have added above?
@skarzi did you ever get around to a fix?
Unfortunately not fully :(
It would be great to discuss this issue with some more experienced pytest-factoryboy
developers
I just ran into this issue. Is there any idea on how to solve this?
Setup
Here are my models:
And my factories:
And finally my test:
Error
This tests fails due to unresolved fixtures:
I guess this happens due to the fact that I am using
factory.List
.Workaround