Skip to content

Commit

Permalink
Merge pull request #141 from pytest-dev/fix_after_postgeneration_results
Browse files Browse the repository at this point in the history
Fix _after_postgeneration results parameter
  • Loading branch information
youtux committed May 1, 2022
2 parents 2e85fcd + 8c0d705 commit b5f8bdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
- Drop support for Python 3.6. We now support only python >= 3.7.
- Improve "debuggability". Internal pytest-factoryboy calls are now visible when using a debugger like PDB or PyCharm.
- Add type annotations. Now `register` and `LazyFixture` are type annotated.
- Fix `_after_postgeneration` not getting the evaluated post_generations and RelatedFactory results correctly in the `result` param.


2.1.0
Expand Down
10 changes: 4 additions & 6 deletions pytest_factoryboy/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,8 @@ def make_deferred_related(factory: FactoryType, fixture: str, attr: str) -> Defe
"""
name = SEPARATOR.join((fixture, attr))

def deferred_impl(request: FixtureRequest) -> None:
# TODO: Shouldn't we return this result?
request.getfixturevalue(name)
def deferred_impl(request: FixtureRequest) -> Any:
return request.getfixturevalue(name)

return DeferredFunction(
name=name,
Expand Down Expand Up @@ -312,9 +311,8 @@ def make_deferred_postgen(
"""
name = SEPARATOR.join((fixture, attr))

def deferred_impl(request: FixtureRequest) -> None:
# TODO: Shouldn't we return this result?
declaration.call(instance, step, context)
def deferred_impl(request: FixtureRequest) -> Any:
return declaration.call(instance, step, context)

return DeferredFunction(
name=name,
Expand Down
23 changes: 21 additions & 2 deletions tests/test_postgen_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ class Bar:
foo: Foo


@dataclass
class Baz:
foo: Foo


@register
class BazFactory(factory.Factory):
class Meta:
model = Baz

foo = None


@register
class FooFactory(factory.Factory):

Expand All @@ -38,8 +51,11 @@ class Meta:
"""Value that is expected at the constructor."""

@factory.post_generation
def set1(foo: Foo, create: bool, value: Any, **kwargs: Any) -> None:
def set1(foo: Foo, create: bool, value: Any, **kwargs: Any) -> str:
foo.value = 1
return "set to 1"

baz = factory.RelatedFactory(BazFactory, "foo")

@classmethod
def _after_postgeneration(cls, obj: Foo, create: bool, results: dict[str, Any] | None = None) -> None:
Expand Down Expand Up @@ -88,9 +104,12 @@ def test_getfixturevalue(request, factoryboy_request: Request):

def test_after_postgeneration(foo: Foo):
"""Test _after_postgeneration is called."""
assert foo._postgeneration_results == {"set1": None}
assert foo._create is True

foo._postgeneration_results["set1"] == "set to 1"
foo._postgeneration_results["baz"].foo is foo
assert len(foo._postgeneration_results) == 2


class Ordered:
value = None
Expand Down

0 comments on commit b5f8bdf

Please sign in to comment.