-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…117) * [#116] Call field default when it is callable - Also, always generate default value if `field.default` is supplied - Previously, this would not happen, since the `generator` determination was being done earlier in the same `if, elif` block - Add unit tests for generating default value of callables of varying kinds * [116] Add a unit test to cover kwarg over attr_mapping * [116] Reduce complexity of custom field class * [116] Update CHANGELOG Co-authored-by: Bernardo Fontes <[email protected]>
- Loading branch information
Showing
5 changed files
with
34 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.core.files.storage import FileSystemStorage | ||
from django.utils.timezone import now | ||
|
||
from model_bakery.gis import BAKER_GIS | ||
from model_bakery.timezone import smart_datetime as datetime | ||
|
@@ -265,6 +266,10 @@ class DummyBlankFieldsModel(models.Model): | |
blank_text_field = models.TextField(max_length=300, blank=True) | ||
|
||
|
||
class ExtendedDefaultField(models.IntegerField): | ||
pass | ||
|
||
|
||
class DummyDefaultFieldsModel(models.Model): | ||
default_id = models.AutoField(primary_key=True) | ||
default_char_field = models.CharField(max_length=50, default="default") | ||
|
@@ -279,6 +284,9 @@ class DummyDefaultFieldsModel(models.Model): | |
) | ||
default_email_field = models.EmailField(default="[email protected]") | ||
default_slug_field = models.SlugField(default="a-slug") | ||
default_unknown_class_field = ExtendedDefaultField(default=42) | ||
default_callable_int_field = models.IntegerField(default=lambda: 12) | ||
default_callable_datetime_field = models.DateTimeField(default=now) | ||
|
||
|
||
class DummyFileFieldModel(models.Model): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -542,6 +542,15 @@ def test_fill_optional_with_integer(self): | |
with pytest.raises(TypeError): | ||
baker.make(models.DummyBlankFieldsModel, _fill_optional=1) | ||
|
||
def test_fill_optional_with_default(self): | ||
dummy = baker.make(models.DummyDefaultFieldsModel, _fill_optional=True) | ||
assert dummy.default_callable_int_field == 12 | ||
assert isinstance(dummy.default_callable_datetime_field, datetime.datetime) | ||
|
||
def test_fill_optional_with_default_unknown_class(self): | ||
dummy = baker.make(models.DummyDefaultFieldsModel, _fill_optional=True) | ||
assert dummy.default_unknown_class_field == 42 | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestFillAutoFieldsTestCase: | ||
|
@@ -572,6 +581,9 @@ def test_skip_fields_with_default(self): | |
assert dummy.default_decimal_field == Decimal("0") | ||
assert dummy.default_email_field == "[email protected]" | ||
assert dummy.default_slug_field == "a-slug" | ||
assert dummy.default_unknown_class_field == 42 | ||
assert dummy.default_callable_int_field == 12 | ||
assert isinstance(dummy.default_callable_datetime_field, datetime.datetime) | ||
|
||
|
||
@pytest.mark.django_db | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters