pytest-factoryboy icon indicating copy to clipboard operation
pytest-factoryboy copied to clipboard

Improve handling of `factory.Maybe`

Open youtux opened this issue 2 years ago • 0 comments

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)

youtux avatar May 29 '23 07:05 youtux