Skip to content

[Issue 125 Fix] : Added Serializer Field to Properly Resolve Choices in Resource.media_type Field #140

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

Merged
merged 4 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions project/resources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@
from tagging.serializers import TagSerializer, TagsSerializerField


class MediaTypeSerializerField(serializers.ChoiceField):

def to_representation(self, value):

valid_media_types = ', '.join(item for item in self.choices)

if not value:
return ''

else:
try:
media_type = self.choices[value]

except KeyError as err:
raise KeyError(f'Invalid media type. The media type should be one of the following: {valid_media_types}') from err

return media_type

def to_internal_value(self, value):
return value


class ResourceSerializer(TagSerializer, serializers.ModelSerializer):

tags = TagsSerializerField(model_field='tags', default='')
media_type = serializers.SerializerMethodField()
media_type = MediaTypeSerializerField(choices=Resource.RESOURCE_TYPES, allow_blank=True)
user = UserSerializer(read_only=True)

class Meta:
Expand All @@ -31,8 +52,3 @@ class Meta:
'paid',
'tags'
)


def get_media_type(self, obj):
return obj.get_media_type_display()

46 changes: 43 additions & 3 deletions project/resources/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import skip
from pytest import raises
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, didn't know about pytest.raises 👍

from random import randint
from rest_framework import status
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -145,8 +146,7 @@ def test_view_one_resource(self):
self.assertEqual(response.data['title'], new_resource.title)
self.assertEqual(response.data['description'], new_resource.description)

@skip('https://github.com/codebuddies/backend/issues/125')
def test_create_one_resource(self):
def test_create_one_resource_with_media_type(self):
url = '/api/v1/resources/'
data = {"title": "The Modern JavaScript Tutorial",
"author": "iliakan",
Expand All @@ -165,4 +165,44 @@ def test_create_one_resource(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['title'], "The Modern JavaScript Tutorial")
self.assertEqual(response.data['other_referring_source'], "iliakan@javascript.info")
self.assertEqual(response.data['media_type'], 'WEB')
self.assertEqual(response.data['media_type'], 'Website')

def test_create_one_resource_without_media_type(self):
url = '/api/v1/resources/'
data = {"title": "The Best Medium-Hard Data Analyst SQL Interview Questions",
"author": "Zachary Thomas",
"description": "The first 70% of SQL is pretty straightforward but the remaining 30% can be pretty tricky. These are good practice problems for that tricky 30% part.",
"url": "https://quip.com/2gwZArKuWk7W",
"referring_url": "https://quip.com",
"other_referring_source": "twitter.com/lpnotes",
"date_published": "2020-04-19T03:27:06Z",
"created": "2020-05-02T03:27:06.485Z",
"modified": "2020-05-02T03:27:06.485Z",
"media_type": "",
"tags": ["SQLt", "BackEnd", "Databases"]
}

response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data['title'], "The Best Medium-Hard Data Analyst SQL Interview Questions")
self.assertEqual(response.data['other_referring_source'], "twitter.com/lpnotes")
self.assertEqual(response.data['media_type'], '')

def test_create_one_resource_with_invalid_media_type(self):
with raises(KeyError, match=r"The media type should be one of the following:"):
url = '/api/v1/resources/'
data = {"title": "The Best Medium-Hard Data Analyst SQL Interview Questions",
"author": "Zachary Thomas",
"description": "The first 70% of SQL is pretty straightforward but the remaining 30% can be pretty tricky. These are good practice problems for that tricky 30% part.",
"url": "https://quip.com/2gwZArKuWk7W",
"referring_url": "https://quip.com",
"other_referring_source": "twitter.com/lpnotes",
"date_published": "2020-04-19T03:27:06Z",
"created": "2020-05-02T03:27:06.485Z",
"modified": "2020-05-02T03:27:06.485Z",
"media_type": "DOP",
"tags": ["SQLt", "BackEnd", "Databases"]
}

response = self.client.post(url, data, format='json')