Skip to content

Commit

Permalink
Merge pull request #86 from phankiewicz/prevent-name-conflict-between…
Browse files Browse the repository at this point in the history
…-model-and-factory-fixtures

Prevent name conflict between model and factory fixtures
  • Loading branch information
youtux committed Jun 11, 2022
2 parents e08486e + 7d9d410 commit b31cd6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
21 changes: 18 additions & 3 deletions pytest_factoryboy/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,23 @@ def register_(factory_class: F) -> F:
assert not factory_class._meta.abstract, "Can't register abstract factories."
assert factory_class._meta.model is not None, "Factory model class is not specified."

factory_name = get_factory_name(factory_class)
model_name = get_model_name(factory_class) if _name is None else _name

assert model_name != factory_name, (
f"Naming collision for {factory_class}:\n"
f" * factory fixture name: {factory_name}\n"
f" * model fixture name: {model_name}\n"
f"Please provide different name for model fixture."
)

fixture_defs = dict(
generate_fixtures(
factory_class=factory_class, model_name=model_name, overrides=kwargs, caller_locals=_caller_locals
factory_class=factory_class,
model_name=model_name,
factory_name=factory_name,
overrides=kwargs,
caller_locals=_caller_locals,
)
)
for name, fixture in fixture_defs.items():
Expand All @@ -120,10 +132,13 @@ def register_(factory_class: F) -> F:


def generate_fixtures(
factory_class: FactoryType, model_name: str, overrides: Mapping[str, Any], caller_locals: Mapping[str, Any]
factory_class: FactoryType,
model_name: str,
factory_name: str,
overrides: Mapping[str, Any],
caller_locals: Mapping[str, Any],
) -> Iterable[tuple[str, Callable[..., Any]]]:
"""Generate all the FixtureDefs for the given factory class."""
factory_name = get_factory_name(factory_class)

related: list[str] = []
for attr, value in factory_class._meta.declarations.items():
Expand Down
39 changes: 25 additions & 14 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@
from pytest_factoryboy import register


class WithoutModelFactory(factory.Factory):
"""A factory without model."""


class AbstractFactory(factory.Factory):
"""Abstract factory."""

class Meta:
abstract = True
model = dict


def test_without_model():
"""Test that factory without model can't be registered."""
with pytest.raises(AssertionError):

class WithoutModelFactory(factory.Factory):
"""A factory without model."""

with pytest.raises(AssertionError, match="Can't register abstract factories"):
register(WithoutModelFactory)


def test_abstract():
with pytest.raises(AssertionError):
class AbstractFactory(factory.Factory):
"""Abstract factory."""

class Meta:
abstract = True
model = dict

with pytest.raises(AssertionError, match="Can't register abstract factories"):
register(AbstractFactory)


def test_fixture_name_conflict():
class Foo:
pass

class FooFactory(factory.Factory):
class Meta:
model = Foo

with pytest.raises(AssertionError, match="Naming collision"):
register(FooFactory, "foo_factory")

0 comments on commit b31cd6b

Please sign in to comment.