Skip to content

Commit

Permalink
Merge pull request #50 from edx/efischer/annotation_serialization
Browse files Browse the repository at this point in the history
Serialize annotation information with scores
  • Loading branch information
Eric Fischer authored Dec 5, 2016
2 parents d85fd0a + d750096 commit 718a1d3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load_requirements(*requirements_paths):

setup(
name='edx-submissions',
version='1.1.2',
version='1.1.3',
author='edX',
description='An API for creating submissions and scores.',
url='http://github.com/edx/edx-submissions.git',
Expand Down
25 changes: 24 additions & 1 deletion submissions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from rest_framework import serializers
from rest_framework.fields import Field, DateTimeField, IntegerField
from submissions.models import StudentItem, Submission, Score
from submissions.models import StudentItem, Submission, Score, ScoreAnnotation


class RawField(Field):
Expand Down Expand Up @@ -85,11 +85,33 @@ class Meta:
)


class ScoreAnnotationSerializer(serializers.ModelSerializer):

class Meta:
model = ScoreAnnotation
fields = (
'creator',
'reason',
'annotation_type',
)


class ScoreSerializer(serializers.ModelSerializer):

# Ensure that the created_at datetime is not converted to a string.
created_at = DateTimeField(format=None, required=False)

annotations = serializers.SerializerMethodField()
def get_annotations(self, obj):
"""
Inspect ScoreAnnotations to attach all relevant annotations.
"""
annotations = ScoreAnnotation.objects.filter(score_id=obj.id)
return [
ScoreAnnotationSerializer(instance=annotation).data
for annotation in annotations
]

class Meta:
model = Score
fields = (
Expand All @@ -101,4 +123,5 @@ class Meta:

# Computed
'submission_uuid',
'annotations',
)
62 changes: 51 additions & 11 deletions submissions/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
"""
Tests for submissions serializers.
"""
import ddt
from django.test import TestCase
from submissions.models import Score, StudentItem
from submissions.models import Score, ScoreAnnotation, StudentItem, Submission
from submissions.serializers import ScoreSerializer


@ddt.ddt
class ScoreSerializerTest(TestCase):
"""
Tests for the score serializer.
"""

def test_score_with_null_submission(self):
item = StudentItem.objects.create(
def setUp(self):
super(ScoreSerializerTest, self).setUp()
self.item = StudentItem.objects.create(
student_id="score_test_student",
course_id="score_test_course",
item_id="i4x://mycourse/special_presentation"
)
self.submission = Submission.objects.create(student_item=self.item, attempt_number=1)

self.score = Score.objects.create(
student_item=self.item,
submission=self.submission,
points_earned=2,
points_possible=6,
)


def test_score_with_null_submission(self):
# Create a score with a null submission
score = Score.objects.create(
student_item=item,
null_sub_score = Score.objects.create(
student_item=self.item,
submission=None,
points_earned=2,
points_possible=6
points_earned=3,
points_possible=8,
)
score_dict = ScoreSerializer(score).data
null_sub_score_dict = ScoreSerializer(null_sub_score).data
self.assertIs(null_sub_score_dict['submission_uuid'], None)
self.assertEqual(null_sub_score_dict['points_earned'], 3)
self.assertEqual(null_sub_score_dict['points_possible'], 8)

self.assertIs(score_dict['submission_uuid'], None)
self.assertEqual(score_dict['points_earned'], 2)
self.assertEqual(score_dict['points_possible'], 6)
@ddt.data(['test_annotation_1', 'test_annotation_2'], [])
def test_score_annotations(self, annotation_types):
"""
Ensure that annotation types are returned with serialized scores.
"""
annotation_kwargs = {
'creator': 'test_annotator',
'reason': 'tests for the test god'
}
for test_type in annotation_types:
ScoreAnnotation.objects.create(
score=self.score,
annotation_type=test_type,
**annotation_kwargs
)
score_dict = ScoreSerializer(self.score).data
self.assertEqual(
score_dict['annotations'],
[
{
'reason': annotation_kwargs['reason'],
'annotation_type': annotation_type,
'creator': annotation_kwargs['creator'],
}
for annotation_type in annotation_types
]
)

0 comments on commit 718a1d3

Please sign in to comment.