-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore cast-on-assign behavior on Django 1.8+
Turns out the future-deprecated SubfieldBase class did more than what was initially thought. Since it is removed in Django 1.10+, for clarity we no longer use it at all. Instead we intern the behavior of its `Creator` descriptor class as `CastOnAssignDescriptor`. Test cases and original bug report by @andrewdodd. Thanks! Fixes #60 Refs https://code.djangoproject.com/ticket/26807 Refs 8260050
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
|
||
from .models import MyModel | ||
|
||
try: | ||
from .enums import Color # Use the new location of Color enum | ||
except ImportError: | ||
Color = MyModel.Color # Attempt the 0.7.4 location of color enum | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_fields_value_is_enum_when_unsaved(): | ||
obj = MyModel(color='r') | ||
assert Color.RED == obj.color | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_fields_value_is_enum_when_saved(): | ||
obj = MyModel(color='r') | ||
obj.save() | ||
assert Color.RED == obj.color | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_fields_value_is_enum_when_created(): | ||
obj = MyModel.objects.create(color='r') | ||
assert Color.RED == obj.color | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_fields_value_is_enum_when_retrieved(): | ||
MyModel.objects.create(color='r') | ||
obj = MyModel.objects.all()[:1][0] # .first() not available on all Djangoes | ||
assert Color.RED == obj.color |