benoit
benoit

Reputation: 1

RecursionError: when using factory boy

I can't use factory boy correctly.

That is my factories:

import factory

from harrispierce.models import Article, Journal, Section

class JournalFactory(factory.Factory):
    class Meta:
        model = Journal

    name = factory.sequence(lambda n: 'Journal%d'%n)

    @factory.post_generation
    def sections(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return

        if extracted:
            # A list of groups were passed in, use them
            for section in extracted:
                self.sections.add(section)


class SectionFactory(factory.Factory):
    class Meta:
        model = Section

    name = factory.sequence(lambda n: 'Section%d'%n)

and my test:

import pytest
from django.test import TestCase, client
from harrispierce.factories import JournalFactory, SectionFactory

@pytest.mark.django_db
class TestIndex(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.myclient = client.Client()

    def test_index_view(self):
        response = self.myclient.get('/')
        assert response.status_code == 200

    def test_index_content(self):
        section0 = SectionFactory()
        section1 = SectionFactory()
        section2 = SectionFactory()
        print('wijhdjk: ', section0)
        journal1 = JournalFactory.create(sections=(section0, section1, section2))

        response = self.myclient.get('/')

        print('wijhdjk: ', journal1)
        self.assertEquals(journal1.name, 'Section0')
        self.assertContains(response, journal1.name)

But I get this when running pytest:

journal1 = JournalFactory.create(sections=(section0, section1, section2))

harrispierce_tests/test_index.py:22:

RecursionError: maximum recursion depth exceeded while calling a Python object

!!! Recursion detected (same locals & position)

Upvotes: 0

Views: 1277

Answers (1)

Xelnor
Xelnor

Reputation: 3609

One possible issue would be that you're not using the proper Factory base class: for a Django model, use factory.django.DjangoModelFactory.

This shouldn't cause the issue you have, though; a full stack trace would be useful.

Try to remove the @factory.post_generation section, and see whether you get a proper Journal object; then inspect what parameters where passed.

If this is not enough to fix your code, I suggest opening an issue on the factory_boy repository, with a reproducible test case (there are already some branches/commits attempting to reproduce a reported bug, which can be used as a template).

Upvotes: 1

Related Questions