Skip to content
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

Errors with factory_boy master: 'FactoryOptions' object has no attribute 'postgen_declarations' #47

Closed
blueyed opened this issue Jul 20, 2017 · 5 comments

Comments

@blueyed
Copy link
Contributor

blueyed commented Jul 20, 2017

There are errors with factory_boy master:

================================================ ERRORS ================================================
_______________________________ ERROR collecting tests/test_circular.py ________________________________
tests/test_circular.py:50: in <module>
    register(AuthorFactory)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
___________________________ ERROR collecting tests/test_factory_fixtures.py ____________________________
tests/test_factory_fixtures.py:56: in <module>
    class AuthorFactory(factory.Factory):
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
_____________________________ ERROR collecting tests/test_lazy_fixture.py ______________________________
tests/test_lazy_fixture.py:29: in <module>
    register(UserFactory)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
_________________________ ERROR collecting tests/test_postgen_dependencies.py __________________________
tests/test_postgen_dependencies.py:23: in <module>
    class FooFactory(factory.Factory):
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:60: in register
    deps = get_deps(factory_class, model_name=model_name)
.tox/py36-pytest3/lib/python3.6/site-packages/pytest_factoryboy/fixture.py:154: in get_deps
    for attr, value in factory_class.declarations(factory_class._meta.postgen_declarations).items()
E   AttributeError: 'FactoryOptions' object has no attribute 'postgen_declarations'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 4 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================= 4 error in 0.30 seconds ========================================
ERROR: InvocationError: '…/Vcs/pytest-factoryboy/.tox/py36-pytest3/bin/py.test --junitxml=…/Vcs/pytest-factoryboy/.tox/py36-pytest3/log/junit-py36-pytest3.xml pytest_factoryboy tests'
_______________________________________________ summary ________________________________________________
ERROR:   py36-pytest3: commands failed

Bisecting brings up the following commits (2 are skipped, since they cause a different error):

FactoryBoy/factory_boy@c079d87
FactoryBoy/factory_boy@e447662
FactoryBoy/factory_boy@6f20207

Likely related to the refactoring mentioned in #23 (comment).

@blueyed blueyed changed the title Errors with factory_boy master Errors with factory_boy master: 'FactoryOptions' object has no attribute 'postgen_declarations' Jul 20, 2017
@subhashb
Copy link

subhashb commented Jul 31, 2017

Confirming that this has something to do with 2.9.0 (master) changes alone. I switched to FactoryBoy 2.8.1 and was able to avoid this issue for now.

elbenfreund added a commit to projecthamster/hamster-lib that referenced this issue Aug 8, 2017
So far, ``factory-boy`` was not part of our explicit requirements
because we relied on ``pytest-factoryboy`` to pull it as on of its
dependencies. This however prooved error prone as recent ``factory-boy``
versions raise failures (at least in our context).
In order to avoid this we move from installing ``factory-boy``
implicitly to making it part of our requirements as well as pin its last
known functioning version.

For details please also the [relevant ``pytest-factoryboy``
issue](pytest-dev/pytest-factoryboy#47)
elbenfreund added a commit to projecthamster/hamster-lib that referenced this issue Aug 8, 2017
So far, ``factory-boy`` was not part of our explicit requirements
because we relied on ``pytest-factoryboy`` to pull it as on of its
dependencies. This however prooved error prone as recent ``factory-boy``
versions raise failures (at least in our context).
In order to avoid this we move from installing ``factory-boy``
implicitly to making it part of our requirements as well as pin its last
known functioning version.

For details please also the [relevant ``pytest-factoryboy``
issue](pytest-dev/pytest-factoryboy#47)
@MRigal
Copy link

MRigal commented Oct 23, 2017

I also do report the same problems with 2.9.2

@pawelad
Copy link

pawelad commented Nov 30, 2017

I would be happy to help with this but would prefer some feedback from the maintainers regarding their preferred way.

My preference would be to go along with factory_boy changes / deprecations (that is, change postgen_declarations to post_declarations .as_dict(), more or less, which I did locally - it works and is easy) but that would require pytest-factoryboy to specify the minimal factory_boy version, possibly with a new major release and a note in the README about it.

Let me know what you guys think.

@Matoking
Copy link

Matoking commented Dec 26, 2017

I decided to ditch pytest-factoryboy and simply write a function that injects the factories as fixtures as-is, per the example in pytest-dev/pytest#2424

So, if your current conftest.py looks like this:

import factory
from pytest_factoryboy import register


class UserFactory(factory.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: "TestUser%d" % n)


register(UserFactory)

try the following instead:

import factory
import pytest


class UserFactory(factory.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: "TestUser%d" % n)


def register(factory_cls):
    def generate_fixture(cls):
        @pytest.fixture(scope="module")
        def created_fixture():
            return cls
        return created_fixture

    def to_snakecase(text):
        """
        Converts text from CamelCase to snake_case
        Source: https://stackoverflow.com/a/1176023/8981236
        """
        s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
        return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

    # Inject the fixture with snakecase name
    name = to_snakecase(factory_cls.__name__)

    globals()[name] = generate_fixture(factory_cls)


register(UserFactory)

My current test suite works with it without modifications, and I no longer need pytest-factoryboy as the dependency. Your mileage may vary, so this may not be enough for your needs.

elbenfreund added a commit to ninjaduck-solutions/absys that referenced this issue Jan 30, 2018
Um Probleme in der Testausführung zu vermeiden legen wir eine explizite
Verison für ``Factory-Boy`` fest so das ``pytest-factoryboy`` keine
höhere (inkompatible) Version als Abhänigkeit installiert.
Siehe dazu auch: pytest-dev/pytest-factoryboy#47
@olegpidsadnyi
Copy link
Contributor

Please try 2.0.1

llazzaro pushed a commit to infobyte/faraday that referenced this issue Jun 28, 2018
pytest-dev/pytest-factoryboy#47 was fixed in
2.0.1. Now it isn't needed (actually, it break its) to require old
versions of factory boy
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 a pull request may close this issue.

6 participants