Skip to content

Commit 9ddedcb

Browse files
committed
Factory fixture should apply parametrizations
1 parent 29c01c5 commit 9ddedcb

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

pytest_factoryboy/fixture.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,18 @@ def generate_fixtures(
153153
),
154154
)
155155

156+
deps = get_deps(factory_class, model_name=model_name)
156157
if factory_name not in caller_locals.value:
157158
yield (
158159
factory_name,
159160
create_fixture_with_related(
160161
name=factory_name,
161162
function=functools.partial(factory_fixture, factory_class=factory_class),
163+
fixtures=deps,
164+
# TODO: related too?
162165
),
163166
)
164167

165-
deps = get_deps(factory_class, model_name=model_name)
166168
yield (
167169
model_name,
168170
create_fixture_with_related(
@@ -345,6 +347,7 @@ def model_fixture(request: SubRequest, factory_name: str) -> Any:
345347
fixture_name = request.fixturename
346348
prefix = "".join((fixture_name, SEPARATOR))
347349

350+
# TODO: This should be a dependency of the current fixture (i.e. use `usefixtures`)
348351
factory_class: FactoryType = request.getfixturevalue(factory_name)
349352

350353
# Create model fixture instance
@@ -478,7 +481,20 @@ def deferred_impl(request: SubRequest) -> Any:
478481

479482
def factory_fixture(request: SubRequest, factory_class: F) -> F:
480483
"""Factory fixture implementation."""
481-
return factory_class
484+
fixture_name = request.fixturename
485+
# TODO: Not good to check the fixture name, we should know what to expect (via args?)
486+
assert fixture_name.endswith("_factory")
487+
fixture_name = fixture_name[: -len("_factory")]
488+
prefix = "".join((fixture_name, SEPARATOR))
489+
490+
# TODO: copy-paste from model_fixture; refactor
491+
kwargs = {}
492+
for key in factory_class._meta.pre_declarations:
493+
argname = "".join((prefix, key))
494+
if argname in request._fixturedef.argnames:
495+
kwargs[key] = evaluate(request, request.getfixturevalue(argname))
496+
497+
return type(f"{factory_class.__name__}Fixture", (factory_class,), kwargs)
482498

483499

484500
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
@@ -111,7 +111,7 @@ class Meta:
111111

112112
def test_factory(book_factory) -> None:
113113
"""Test model factory fixture."""
114-
assert book_factory == BookFactory
114+
assert issubclass(book_factory, BookFactory)
115115

116116

117117
def test_model(book: Book):
@@ -213,12 +213,3 @@ def test_override_subfactory_with_lazy_fixture(self, another_book: Book):
213213
214214
"""
215215
assert another_book.author.name == "Another Author"
216-
217-
218-
class TestDeferredEvaluation:
219-
@pytest.mark.parametrize("book__name", ["bar"])
220-
def test_book_initialise_later(self, book_factory, book):
221-
assert book.name == "bar"
222-
223-
book_f = book_factory()
224-
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)