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

factory.DictFactory fixtures have incorrect names #65

Closed
sobolevn opened this issue Jun 17, 2018 · 4 comments
Closed

factory.DictFactory fixtures have incorrect names #65

sobolevn opened this issue Jun 17, 2018 · 4 comments

Comments

@sobolevn
Copy link
Member

sobolevn commented Jun 17, 2018

Here's a reproduction:

import factory
from pytest_factoryboy import register


@register
class OAuthPayloadFactory(factory.DictFactory):
    """Fake factory for OAuth payload response."""

    refresh_token = 'some-token'
    access_token = 'some-refresh-token'
    token_type = 'bearer'
    expires_in = 0

When trying to access this fixture I get an error:

@pytest.fixture()
def access_token(o_auth_payload):  # failing here: o_auth_payload is not found
    """A demo fixture to illustrate the issue."""
    return o_auth_payload['access_token']

What I expect to happen

I expect that o_auth_payload would be injected as a fixture.

What actually happens

  @pytest.fixture()
  def user_email(user, o_auth_payload, user_payload):
E       fixture 'o_auth_payload' not found
>       available fixtures:

pytest --fixtures output

dict
    <string>:3: no docstring available
dict__access_token
    <string>:3: no docstring available
dict__expires_in
    <string>:3: no docstring available
dict__refresh_token
    <string>:3: no docstring available
dict__token_type
    <string>:3: no docstring available
o_auth_payload_factory
    <string>:3: no docstring available

env

  • pytest==3.6.1
  • pytest-factoryboy==2.0.1
  • factory-boy==2.11.1
@sobolevn
Copy link
Member Author

As a work around I use this method:

register(OAuthPayloadFactory, _name='o_auth_payload')

but it feels weird.

@sobolevn
Copy link
Member Author

sobolevn commented Jul 14, 2018

New workaround:

import factory
from mimesis_factory import MimesisField
from pytest_factoryboy import register

try:
    from mypy_extensions import TypedDict  # typechecks your dicts
except ImportError:
    TypedDict = dict   # does nothing


class GitlabUserPayload(TypedDict):
    """
    Gitlab API response for user endpoint request.

    We define this class here, since we do not actually use it in the code.
    """

    username: str
    email: str
    name: str


@register
class GitlabUserPayloadFactory(factory.BaseDictFactory):
    """Fake factory for Gitlab's user response."""

    class Meta:
        model = GitlabUserPayload

    username = MimesisField('username')
    email = MimesisField('email')
    name = MimesisField('full_name')

@phankiewicz
Copy link
Contributor

Actually the behaviour you are describing is not the bug. The name of the fixture by the convention, as stated in the docs is:

  • for factory fixture: its lowercase-underscore class name
  • for model fixture: its underlying model's lowercase-underscore class name (docs are not phrased well in this case)

So in first case there is no underlying model called OAuthPayload, therefore there is no model fixture called o_auth_payload as you would expect. The underlying model for this factory (as it is derived from factory.DictFactory) is dict as stated in DictFactory definition here. This means that the name of the model fixture would be dict in this case. That is exactly why example with GitlabUserPayload model works as you would expect it to.

@youtux
Copy link
Contributor

youtux commented Jun 11, 2022

Fixed in #167

@youtux youtux closed this as completed Jun 11, 2022
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

3 participants