pytest-dev / pytest-dev/pytest-factoryboy

Partial specialization with custom post_generation for M2M

Open
#36 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
400
Forks
44
Avg merge
6h 34m
Merged PRs (30d)
1

Description

I may be missing something, but I am having trouble registering a "partial specialization" fixture for a factory with a ManyToMany field.

I tried using [the example in factoryboy docs][1], combined with the example in fixture attributes, but it throws TypeError: int() argument must be a string or a number, not 'LazyFixture'. Is this something we can even do?

Here's a test case:

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

    name = "John Doe"

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

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

register(UserFactory, "special_user", groups=[LazyFixture("group")])

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied UserFactory test case and the fixture-attributes example linked in the issue. Trace how register handles groups=[LazyFixture("group")] alongside the custom post_generation method, then verify the TypeError no longer occurs and the partial specialization works as intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.