pytest-dev / pytest-dev/pytest-factoryboy

Improve handling of `factory.Maybe`

Open
#196 0 comments 1 reaction 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

It would be great if pytest-factoryboy could better handle factory.Maybe, so that it can reflect how factoryboy handles it.

from __future__ import annotations

import pytest
from factory import *
from pytest_factoryboy import register
from dataclasses import *

@dataclass
class Company:
    name: str


@dataclass
class User:
    is_staff: bool
    company: Company | None


@register
class CompanyFactory(Factory):
    class Meta:
        model = Company
    name = "foo"


@register
class UserFactory(Factory):
    class Meta:
        model = User
    is_staff = False
    company = Maybe("is_staff", yes_declaration=None, no_declaration=SubFactory(CompanyFactory))


@pytest.mark.parametrize("user__is_staff", [False])
def test_staff_user_has_no_company_by_default(user):
    assert user.company is None


@pytest.mark.parametrize("user__is_staff", [False])
def test_normal_user_has_company_by_default(user, company):
    assert user.company is company

This test module should succeed, but it doesn't. (I don't have a stack trace with me at the moment)

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 by running the supplied pytest example to reproduce the failure and inspect how the registered UserFactory handles factory.Maybe declarations and parameterized user__is_staff values. Done means both tests pass, with a staff user's company set to None and a normal user's company matching the registered company fixture.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.