Skip to content

Commit d04dd97

Browse files
committed
Factory fixture should apply parametrizations
1 parent 00038aa commit d04dd97

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

src/pytest_factoryboy/fixture.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,18 @@ def generate_fixtures(
172172
),
173173
)
174174

175+
deps = get_deps(factory_class, model_name=model_name)
175176
if factory_name not in caller_locals.value:
176177
yield (
177178
factory_name,
178179
create_fixture_with_related(
179180
name=factory_name,
180181
function=functools.partial(factory_fixture, factory_class=factory_class),
182+
fixtures=deps,
183+
# TODO: related too?
181184
),
182185
)
183186

184-
deps = get_deps(factory_class, model_name=model_name)
185187
yield (
186188
model_name,
187189
create_fixture_with_related(
@@ -366,6 +368,7 @@ def model_fixture(request: SubRequest, factory_name: str) -> object:
366368
fixture_name = request.fixturename
367369
prefix = "".join((fixture_name, SEPARATOR))
368370

371+
# TODO: This should be a dependency of the current fixture (i.e. use `usefixtures`)
369372
factory_class: type[Factory[object]] = request.getfixturevalue(factory_name)
370373

371374
# create Factory override for the model fixture
@@ -500,7 +503,20 @@ def deferred_impl(request: SubRequest) -> object:
500503

501504
def factory_fixture(request: SubRequest, factory_class: type[Factory[T]]) -> type[Factory[T]]:
502505
"""Factory fixture implementation."""
503-
return factory_class
506+
fixture_name = request.fixturename
507+
# TODO: Not good to check the fixture name, we should know what to expect (via args?)
508+
assert fixture_name.endswith("_factory")
509+
fixture_name = fixture_name[: -len("_factory")]
510+
prefix = "".join((fixture_name, SEPARATOR))
511+
512+
# TODO: copy-paste from model_fixture; refactor
513+
kwargs = {}
514+
for key in factory_class._meta.pre_declarations:
515+
argname = "".join((prefix, key))
516+
if argname in request._fixturedef.argnames:
517+
kwargs[key] = evaluate(request, request.getfixturevalue(argname))
518+
519+
return type(f"{factory_class.__name__}Fixture", (factory_class,), kwargs)
504520

505521

506522
def attr_fixture(request: SubRequest, value: T) -> T:

tests/test_factory_fixtures.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Meta:
109109

110110
def test_factory(book_factory) -> None:
111111
"""Test model factory fixture."""
112-
assert book_factory == BookFactory
112+
assert issubclass(book_factory, BookFactory)
113113

114114

115115
def test_model(book: Book):
@@ -211,12 +211,3 @@ def test_override_subfactory_with_lazy_fixture(self, another_book: Book):
211211
212212
"""
213213
assert another_book.author.name == "Another Author"
214-
215-
216-
class TestDeferredEvaluation:
217-
@pytest.mark.parametrize("book__name", ["bar"])
218-
def test_book_initialise_later(self, book_factory, book):
219-
assert book.name == "bar"
220-
221-
book_f = book_factory()
222-
assert book_f.name == "bar"

tests/test_foo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# TODO: Improve tests
2+
# TODO: Change test module
3+
4+
from dataclasses import dataclass
5+
6+
import factory
7+
import pytest
8+
9+
from pytest_factoryboy import register
10+
11+
12+
@dataclass
13+
class Book:
14+
name: str
15+
16+
17+
@register
18+
class BookFactory(factory.Factory):
19+
class Meta:
20+
model = Book
21+
22+
name = "foo"
23+
24+
25+
@pytest.mark.parametrize("book__name", ["bar"])
26+
def test_book_initialise_later(book_factory, book__name, book):
27+
assert book.name == "bar"
28+
29+
book_f = book_factory()
30+
assert book_f.name == "bar"

0 commit comments

Comments
 (0)