Skip to content

Commit

Permalink
Explicitly add _fill_optional parameters to baker.make and baker.prep…
Browse files Browse the repository at this point in the history
…are (#264)

* Explicitly add _fill_optional parameters to baker.make and baker.prepare

* Add changelog message

Co-authored-by: berin <bernardoxhc@gmail.com>
  • Loading branch information
Honza-m and berinhard committed Mar 31, 2022
1 parent 99d4f52 commit 2debc46
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Support for Django 4.0 [PR #236](https://github.com/model-bakers/model_bakery/pull/236)

### Changed
- Explicitly add _fill_optional parameters to baker.make and baker.prepare to aid IDE autocomplete function. [PR #264](https://github.com/model-bakers/model_bakery/pull/264)
- Validate `increment_by` parameter of `seq` helper when `value` is an instance of `datetime` [PR #247](https://github.com/model-bakers/model_bakery/pull/247)
- Fix a simple typo in `bulk_create` disclaimer in docs [PR #245](https://github.com/model-bakers/model_bakery/pull/245)
- Allow relation `_id` fields to use sequences [PR #253](https://github.com/model-bakers/model_bakery/pull/253)
Expand Down
23 changes: 21 additions & 2 deletions model_bakery/baker.py
Expand Up @@ -83,6 +83,7 @@ def make(
_create_files: bool = False,
_using: str = "",
_bulk_create: bool = False,
_fill_optional: Union[List[str], bool] = False,
**attrs: Any,
) -> List[M]:
...
Expand All @@ -97,6 +98,7 @@ def make(
_create_files: bool = False,
_using: str = "",
_bulk_create: bool = False,
_fill_optional: Union[List[str], bool] = False,
**attrs: Any,
):
"""Create a persisted instance from a given model its associated models.
Expand All @@ -105,6 +107,7 @@ def make(
fields you want to define its values by yourself.
"""
_save_kwargs = _save_kwargs or {}
attrs.update({"_fill_optional": _fill_optional})
baker: Baker = Baker.create(
_model, make_m2m=make_m2m, create_files=_create_files, _using=_using
)
Expand Down Expand Up @@ -145,6 +148,7 @@ def prepare(
_quantity: int,
_save_related: bool = False,
_using: str = "",
_fill_optional: Union[List[str], bool] = False,
**attrs,
) -> List[M]:
...
Expand All @@ -155,13 +159,15 @@ def prepare(
_quantity: Optional[int] = None,
_save_related: bool = False,
_using: str = "",
_fill_optional: Union[List[str], bool] = False,
**attrs,
):
"""Create but do not persist an instance from a given model.
It fill the fields with random values or you can specify which
fields you want to define its values by yourself.
"""
attrs.update({"_fill_optional": _fill_optional})
baker = Baker.create(_model, _using=_using)
if _valid_quantity(_quantity):
raise InvalidQuantityException
Expand Down Expand Up @@ -363,6 +369,7 @@ def make(
_save_kwargs: Optional[Dict[str, Any]] = None,
_refresh_after_create: bool = False,
_from_manager=None,
_fill_optional: Union[List[str], bool] = False,
**attrs: Any,
):
"""Create and persist an instance of the model associated with Baker instance."""
Expand All @@ -372,13 +379,25 @@ def make(
"_save_kwargs": _save_kwargs,
"_refresh_after_create": _refresh_after_create,
"_from_manager": _from_manager,
"_fill_optional": _fill_optional,
}
params.update(attrs)
return self._make(**params)

def prepare(self, _save_related=False, **attrs: Any) -> M:
def prepare(
self,
_save_related=False,
_fill_optional: Union[List[str], bool] = False,
**attrs: Any
) -> M:
"""Create, but do not persist, an instance of the associated model."""
return self._make(commit=False, commit_related=_save_related, **attrs)
params = {
"commit": False,
"commit_related": _save_related,
"_fill_optional": _fill_optional,
}
params.update(attrs)
return self._make(**params)

def get_fields(self) -> Any:
return set(self.model._meta.get_fields()) - set(self.get_related())
Expand Down
26 changes: 26 additions & 0 deletions tests/test_baker.py
Expand Up @@ -635,6 +635,21 @@ def test_skip_blank(self):
assert dummy.blank_char_field == ""
assert dummy.blank_text_field == ""

def test_skip_blank_with_argument(self):
dummy = baker.make(models.DummyBlankFieldsModel, _fill_optional=False)
assert dummy.blank_char_field == ""
assert dummy.blank_text_field == ""

def test_skip_blank_when_preparing(self):
dummy = baker.prepare(models.DummyBlankFieldsModel)
assert dummy.blank_char_field == ""
assert dummy.blank_text_field == ""

def test_skip_blank_when_preparing_with_argument(self):
dummy = baker.prepare(models.DummyBlankFieldsModel, _fill_optional=False)
assert dummy.blank_char_field == ""
assert dummy.blank_text_field == ""


@pytest.mark.django_db
class TestFillBlanksTestCase:
Expand All @@ -644,6 +659,12 @@ def test_fill_field_optional(self):
)
assert len(dummy.blank_char_field) == 50

def test_fill_field_optinal_when_preparing(self):
dummy = baker.prepare(
models.DummyBlankFieldsModel, _fill_optional=["blank_char_field"]
)
assert len(dummy.blank_char_field) == 50

def test_fill_wrong_field(self):
with pytest.raises(AttributeError) as exc_info:
baker.make(
Expand All @@ -670,6 +691,11 @@ def test_fill_all_optional(self):
assert len(dummy.blank_char_field) == 50
assert len(dummy.blank_text_field) == 300

def test_fill_all_optional_when_preparing(self):
dummy = baker.prepare(models.DummyBlankFieldsModel, _fill_optional=True)
assert len(dummy.blank_char_field) == 50
assert len(dummy.blank_text_field) == 300

def test_fill_optional_with_integer(self):
with pytest.raises(TypeError):
baker.make(models.DummyBlankFieldsModel, _fill_optional=1)
Expand Down

0 comments on commit 2debc46

Please sign in to comment.