Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of factory.Maybe #196

Open
youtux opened this issue May 29, 2023 · 0 comments
Open

Improve handling of factory.Maybe #196

youtux opened this issue May 29, 2023 · 0 comments

Comments

@youtux
Copy link
Contributor

youtux commented May 29, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant