From 24c1cf117e7a4904c44502cc8dfc43cf56dbf7e6 Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Thu, 30 Jun 2022 22:22:45 +0200 Subject: [PATCH 1/4] Fix #308 -- do not force defaults for `make` and `prepare` PR #292 introduced explicit defaults for `make` and `prepare`, which were then overwriting custom values in certain cases (explained in #308). This PR attempts to fix this issue by not having explicit defaults. --- model_bakery/recipe.py | 37 +++++++++++++++++++--------------- tests/generic/baker_recipes.py | 4 ++++ tests/generic/models.py | 6 ++++++ tests/test_recipes.py | 4 ++++ 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/model_bakery/recipe.py b/model_bakery/recipe.py index 93f7ee28..876d0f9c 100644 --- a/model_bakery/recipe.py +++ b/model_bakery/recipe.py @@ -111,22 +111,28 @@ def make( def make( self, _quantity: Optional[int] = None, - make_m2m: bool = False, - _refresh_after_create: bool = False, - _create_files: bool = False, + make_m2m: Optional[bool] = None, + _refresh_after_create: Optional[bool] = None, + _create_files: Optional[bool] = None, _using: str = "", - _bulk_create: bool = False, + _bulk_create: Optional[bool] = None, _save_kwargs: Optional[Dict[str, Any]] = None, **attrs: Any, ) -> Union[M, List[M]]: - defaults = { - "_quantity": _quantity, - "make_m2m": make_m2m, - "_refresh_after_create": _refresh_after_create, - "_create_files": _create_files, - "_bulk_create": _bulk_create, - "_save_kwargs": _save_kwargs, - } + defaults = {} + if _quantity is not None: + defaults["_quantity"] = _quantity + if make_m2m is not None: + defaults["make_m2m"] = make_m2m + if _refresh_after_create is not None: + defaults["_refresh_after_create"] = _refresh_after_create + if _create_files is not None: + defaults["_create_files"] = _create_files + if _bulk_create is not None: + defaults["_bulk_create"] = _bulk_create + if _save_kwargs is not None: + defaults["_save_kwargs"] = _save_kwargs + defaults.update(attrs) return baker.make(self._model, _using=_using, **self._mapping(_using, defaults)) @@ -157,10 +163,9 @@ def prepare( _using: str = "", **attrs: Any, ) -> Union[M, List[M]]: - defaults = { - "_quantity": _quantity, - "_save_related": _save_related, - } + defaults = {"_save_related": _save_related} + if _quantity is not None: + defaults["_quantity"] = _quantity defaults.update(attrs) return baker.prepare( self._model, _using=_using, **self._mapping(_using, defaults) diff --git a/tests/generic/baker_recipes.py b/tests/generic/baker_recipes.py index f4e27c7a..e2dd7154 100644 --- a/tests/generic/baker_recipes.py +++ b/tests/generic/baker_recipes.py @@ -113,6 +113,10 @@ class SmallDogRecipe(Recipe): overrided_save = Recipe("generic.ModelWithOverridedSave") +with_save_kwargs = Recipe( + "generic.ModelWithSaveKwargs", _save_kwargs={"breed": "updated_breed"} +) + ip_fields = Recipe( "generic.DummyGenericIPAddressFieldModel", ipv4_field=seq("127.0.0.", increment_by=2), diff --git a/tests/generic/models.py b/tests/generic/models.py index 0acf2145..c3248cbd 100755 --- a/tests/generic/models.py +++ b/tests/generic/models.py @@ -226,6 +226,12 @@ def save(self, *args, **kwargs): return super(ModelWithOverridedSave, self).save(*args, **kwargs) +class ModelWithSaveKwargs(Dog): + def save(self, *args, **kwargs): + self.breed = kwargs.pop("breed") + return super(ModelWithSaveKwargs, self).save(*args, **kwargs) + + class Classroom(models.Model): students = models.ManyToManyField(Person, null=True) active = models.BooleanField(null=True) diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 45d8780a..ab5163c4 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -332,6 +332,10 @@ def test_pass_save_kwargs(self): ) assert owner == dog.owner + def test_pass_save_kwargs_in_recipe_definition(self): + dog = baker.make_recipe("tests.generic.with_save_kwargs") + assert dog.breed == "updated_breed" + def test_ip_fields_with_start(self): first, second = baker.make_recipe("tests.generic.ip_fields", _quantity=2) From ce2f36b2f435579edaab07b6ed3bbd71c41291fb Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Thu, 30 Jun 2022 22:31:08 +0200 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac01817f..0d3fdb30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main) ### Added + ### Changed +- Fixed a bug with overwritten `_save_kwargs` and other custom arguments [PR #330](https://github.com/model-bakers/model_bakery/pull/330) + ### Removed ## [1.6.0](https://pypi.org/project/model-bakery/1.6.0/) From 38049b43ec5bd6804bc6d5503d517167e8df93e4 Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Thu, 30 Jun 2022 23:24:58 +0200 Subject: [PATCH 3/4] Silence weird mypy assignment error --- model_bakery/recipe.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/model_bakery/recipe.py b/model_bakery/recipe.py index 876d0f9c..401c312f 100644 --- a/model_bakery/recipe.py +++ b/model_bakery/recipe.py @@ -131,7 +131,7 @@ def make( if _bulk_create is not None: defaults["_bulk_create"] = _bulk_create if _save_kwargs is not None: - defaults["_save_kwargs"] = _save_kwargs + defaults["_save_kwargs"] = _save_kwargs # type: ignore[assignment] defaults.update(attrs) return baker.make(self._model, _using=_using, **self._mapping(_using, defaults)) @@ -163,9 +163,10 @@ def prepare( _using: str = "", **attrs: Any, ) -> Union[M, List[M]]: - defaults = {"_save_related": _save_related} - if _quantity is not None: - defaults["_quantity"] = _quantity + defaults = { + "_quantity": _quantity, + "_save_related": _save_related, + } defaults.update(attrs) return baker.prepare( self._model, _using=_using, **self._mapping(_using, defaults) From c3113d020a1dd001b3c27cd25c778cee354184ef Mon Sep 17 00:00:00 2001 From: Rust Saiargaliev Date: Tue, 2 Aug 2022 14:01:35 +0200 Subject: [PATCH 4/4] Use Python 3 inheritance --- tests/generic/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/generic/models.py b/tests/generic/models.py index c3248cbd..0fefe025 100755 --- a/tests/generic/models.py +++ b/tests/generic/models.py @@ -223,13 +223,13 @@ class RelatedNamesWithEmptyDefaultsModel(models.Model): class ModelWithOverridedSave(Dog): def save(self, *args, **kwargs): self.owner = kwargs.pop("owner") - return super(ModelWithOverridedSave, self).save(*args, **kwargs) + return super().save(*args, **kwargs) class ModelWithSaveKwargs(Dog): def save(self, *args, **kwargs): self.breed = kwargs.pop("breed") - return super(ModelWithSaveKwargs, self).save(*args, **kwargs) + return super().save(*args, **kwargs) class Classroom(models.Model):