-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[116] Support Callable default
and Always Use default
If Present
#117
Merged
berinhard
merged 6 commits into
model-bakers:main
from
timjklein36:tk/116-support-callable-default
Oct 13, 2020
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a16722
[#116] Call field default when it is callable
timjk-gp d93725a
[116] Add a unit test to cover kwarg over attr_mapping
timjk-gp 3305326
[116] Reduce complexity of custom field class
timjk-gp 1029f73
[116] Update CHANGELOG
timjk-gp cdf0d1d
Merge branch 'main' into tk/116-support-callable-default
berinhard bf03875
Merge branch 'main' into tk/116-support-callable-default
berinhard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this catch! I didn't notice that the
assert
was wrong before