Skip to content

Commit

Permalink
Merge pull request #159 from skarzi/issue-158
Browse files Browse the repository at this point in the history
fix: fix subfactory value override with lazy fixture
  • Loading branch information
youtux committed May 21, 2022
2 parents 40b039b + 0940027 commit 843f8e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pytest_factoryboy/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ def register_(factory_class: F) -> F:
for attr, value in factory_class._meta.declarations.items():
args = []
attr_name = SEPARATOR.join((model_name, attr))
value = kwargs.get(attr, value)

if isinstance(value, (factory.SubFactory, factory.RelatedFactory)):
value = kwargs.get(attr, value)
subfactory_class = value.get_factory()
subfactory_deps = get_deps(subfactory_class, factory_class)

Expand Down
32 changes: 20 additions & 12 deletions tests/test_factory_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def test_partial(self, partial_author: Author):

class TestLazyFixture:
register(AuthorFactory, "another_author", name=LazyFixture(lambda: "Another Author"))
register(BookFactory, "another_book", author=LazyFixture("another_author"))

@pytest.mark.parametrize("book__author", [LazyFixture("another_author")])
def test_lazy_fixture_name(self, book: Book, another_author: Author):
Expand All @@ -192,15 +193,22 @@ def test_lazy_fixture_callable(self, book: Book, another_author: Author) -> None
assert book.author == another_author
assert book.author.name == "Another Author"


@pytest.mark.parametrize(
("author__register_user", "author__register_user__password"),
[
(LazyFixture(lambda: "lazyfixture"), LazyFixture(lambda: "asdasd")),
],
)
def test_lazy_fixture_post_generation(author: Author):
"""Test that post-generation values are replaced with lazy fixtures."""
# assert author.user.username == "lazyfixture"
assert author.user
assert author.user.password == "asdasd"
@pytest.mark.parametrize(
("author__register_user", "author__register_user__password"),
[
(LazyFixture(lambda: "lazyfixture"), LazyFixture(lambda: "asdasd")),
],
)
def test_lazy_fixture_post_generation(self, author: Author):
"""Test that post-generation values are replaced with lazy fixtures."""
assert author.user
assert author.user.username == "lazyfixture"
assert author.user.password == "asdasd"

def test_override_subfactory_with_lazy_fixture(self, another_book: Book):
"""Ensure subfactory fixture can be overriden with ``LazyFixture``.
Issue: https://github.com/pytest-dev/pytest-factoryboy/issues/158
"""
assert another_book.author.name == "Another Author"

0 comments on commit 843f8e2

Please sign in to comment.