Skip to content

Improve handling of factory.Maybe #196

Open
@youtux

Description

@youtux

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions