1

I have the following factory:

class ContactFactory(DjangoModelFactory):
    name = Faker('company')
    industry = Iterator(Industry.objects.all())

    class Meta:
        model = 'sales.contact'

    @post_generation
        def requested_devices(self, create, extracted, **kwargs):
            if create:
              self.requested_devices.add(MSize.objects.first())

And I'm trying to write a test such as:

class TestUserCanAskQuestion(TestCase):
    @classmethod
    def setUpTestData(cls):
        call_command('insert_initial_data')

    def setUp(self):
        self.contact = ContactFactory()

But everytime I run the test, it results in "StopIteration" error. Here is the full stack trace:

ERROR: test_dummy (comminquiry.tests.test_views.TestUserCanAskQuestion)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/mnt/c/Users/Alire/Projects/mm/mm-bpmui/src/comminquiry/tests/test_views.py", line 32, in test_dummy
    a = ContactFactory.build()
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/base.py", line 546, in build
    return cls._generate(enums.BUILD_STRATEGY, kwargs)
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/base.py", line 500, in _generate
    return step.build()
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 272, in build
    step.resolve(pre)
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 221, in resolve
    self.attributes[field_name] = getattr(self.stub, field_name)
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/builder.py", line 375, in __getattr__
    extra=context,
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/declarations.py", line 196, in evaluate
    value = next(iter(self.iterator))
  File "/mnt/c/Users/Alire/Projects/mm/venv/lib/python3.5/site-packages/factory/utils.py", line 136, in __iter__
    value = next(self.iterator)
StopIteration

----------------------------------------------------------------------
Ran 1 test in 2.433s

If I move the ContactFactory() outside of class, the error disappears. I'm I missing something? or it's a bug in factory boy or django?

(I'm using factory_boy==2.11.1 and django==2.1.2)

  • fix your indentation – Anentropic Oct 23 '18 at 16:40
  • 1
    pay attention the whole stack trace from the error, it will be telling you useful information. it is also useful information for anyone trying to help you – Anentropic Oct 23 '18 at 16:41
  • 1
    This probably implies that `Industry.objects.all()` is an empty QuerySet. Are you sure you've populated the database with `Industry` objects before reaching the point where you call your factory? I miss for example a call to `super().setUp()` – dirkgroten Oct 23 '18 at 16:57
  • @dirkgroten You were right, one one fields was retruning an empty queryset. Thanks for help! – Alireza Amouzadeh Oct 23 '18 at 17:13

1 Answers1

6

As @dirkgroten had suggested, one the fields was returning an empty queryset. This was the root cause of the error.

  • I've run into a similar issue. Can you tell how you loaded data from fixture to model. The queryset in my case is also empty and wrapped around Iterator() method. Thank you ! – prashant bana Dec 16 '22 at 14:10