Skip to content

Commit

Permalink
Fix Recipe.prepare without _quantity (on one-to-one relation) (#512)
Browse files Browse the repository at this point in the history
Co-authored-by: velizhanin_ie <[email protected]>
  • Loading branch information
MaHryCT3 and velizhanin_ie authored Jan 5, 2025
1 parent 3818ad8 commit 2a3564f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion model_bakery/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ def prepare(
**attrs: Any,
) -> Union[M, List[M]]:
defaults = {
"_quantity": _quantity,
"_save_related": _save_related,
}
if _quantity is not None:
defaults["_quantity"] = _quantity # type: ignore[assignment]

defaults.update(attrs)
return baker.prepare(
self._model, _using=_using, **self._mapping(_using, defaults)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
)
from model_bakery.timezone import tz_aware
from tests.generic import models
from tests.generic.baker_recipes import lonely_person

Check failure on line 23 in tests/test_baker.py

View workflow job for this annotation

GitHub Actions / linters (ruff check --output-format=github .)

Ruff (F401)

tests/test_baker.py:23:41: F401 `tests.generic.baker_recipes.lonely_person` imported but unused
from tests.generic.forms import DummyGenericIPAddressFieldForm
from tests.generic import baker_recipes


def test_import_seq_from_baker():

Check failure on line 28 in tests/test_baker.py

View workflow job for this annotation

GitHub Actions / linters (ruff check --output-format=github .)

Ruff (I001)

tests/test_baker.py:1:1: I001 Import block is un-sorted or un-formatted
Expand Down Expand Up @@ -286,6 +288,12 @@ def test_create_one_to_one(self):
assert lonely_person.pk is None
assert lonely_person.only_friend.pk

def test_recipe_prepare_model_with_one_to_one_and_save_related(self):
lonely_person = baker_recipes.lonely_person.prepare(_save_related=True)

Check failure on line 292 in tests/test_baker.py

View workflow job for this annotation

GitHub Actions / linters (ruff check --output-format=github .)

Ruff (F811)

tests/test_baker.py:292:9: F811 Redefinition of unused `lonely_person` from line 23

assert lonely_person.pk is None
assert lonely_person.only_friend.pk


@pytest.mark.django_db
class TestBakerCreatesAssociatedModels(TestCase):
Expand Down

0 comments on commit 2a3564f

Please sign in to comment.