pytest-dev / pytest-dev/pytest-factoryboy
Partial specialization with custom post_generation for M2M
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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