-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement SerializerMethodResourceRelatedField (#781)
Fixes #639 - interface not consistent with `SerializerMethodField` Fixes #779 - no enforcement of `read_only` Fixes #780 - broken `parent` chain
- Loading branch information
Showing
9 changed files
with
190 additions
and
59 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
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
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,66 @@ | ||
from __future__ import absolute_import | ||
|
||
import pytest | ||
from rest_framework import serializers | ||
|
||
from rest_framework_json_api.relations import SerializerMethodResourceRelatedField | ||
|
||
from example.models import Blog, Entry | ||
|
||
|
||
def test_method_name_default(): | ||
class BlogSerializer(serializers.ModelSerializer): | ||
one_entry = SerializerMethodResourceRelatedField(model=Entry) | ||
|
||
class Meta: | ||
model = Blog | ||
fields = ['one_entry'] | ||
|
||
def get_one_entry(self, instance): | ||
return Entry(id=100) | ||
|
||
serializer = BlogSerializer(instance=Blog()) | ||
assert serializer.data['one_entry']['id'] == '100' | ||
|
||
|
||
def test_method_name_custom(): | ||
class BlogSerializer(serializers.ModelSerializer): | ||
one_entry = SerializerMethodResourceRelatedField( | ||
model=Entry, | ||
method_name='get_custom_entry' | ||
) | ||
|
||
class Meta: | ||
model = Blog | ||
fields = ['one_entry'] | ||
|
||
def get_custom_entry(self, instance): | ||
return Entry(id=100) | ||
|
||
serializer = BlogSerializer(instance=Blog()) | ||
assert serializer.data['one_entry']['id'] == '100' | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore::DeprecationWarning") | ||
def test_source(): | ||
class BlogSerializer(serializers.ModelSerializer): | ||
one_entry = SerializerMethodResourceRelatedField( | ||
model=Entry, | ||
source='get_custom_entry' | ||
) | ||
|
||
class Meta: | ||
model = Blog | ||
fields = ['one_entry'] | ||
|
||
def get_custom_entry(self, instance): | ||
return Entry(id=100) | ||
|
||
serializer = BlogSerializer(instance=Blog()) | ||
assert serializer.data['one_entry']['id'] == '100' | ||
|
||
|
||
@pytest.mark.filterwarnings("error::DeprecationWarning") | ||
def test_source_is_deprecated(): | ||
with pytest.raises(DeprecationWarning): | ||
SerializerMethodResourceRelatedField(model=Entry, source='get_custom_entry') |
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