Skip to content
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

Fixes CourseDailyMetricsSerializer when average_progress is 1.00 #230

Merged
merged 3 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions figures/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,16 @@ class Meta:

class CourseDailyMetricsSerializer(serializers.ModelSerializer):
"""Provides summary data about a specific course

[john@appsembler] min_value and max_value validation added to average_progress,
however, unit tests show these are ignored. I timeboxed an hour to go through
the DRF 3.6.3 source code, and try to trace why then stopped due to time
constraints.
"""
average_progress = serializers.DecimalField(max_digits=2, decimal_places=2)
average_progress = serializers.DecimalField(max_digits=3,
decimal_places=2,
min_value=0.00,
max_value=1.00)

class Meta:
model = CourseDailyMetrics
Expand Down Expand Up @@ -290,9 +298,9 @@ def get_staff(self, obj):
def get_metrics(self, obj):
qs = CourseDailyMetrics.objects.filter(course_id=str(obj.id))
if qs:
return CourseDailyMetricsSerializer(qs.latest('date_for')).data
return CourseDailyMetricsSerializer(qs.order_by('-date_for')[0]).data
else:
return []
return None


def get_course_history_metric(site, course_id, func, date_for, months_back):
Expand Down
25 changes: 23 additions & 2 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.contrib.sites.models import Site
from django.db import models
from django.utils.timezone import utc
from rest_framework.exceptions import ValidationError

from student.models import CourseEnrollment

Expand Down Expand Up @@ -240,6 +241,22 @@ def test_has_fields(self):
else:
assert data[field_name] == db_field

@pytest.mark.parametrize('average_progress', [0, 0.00, 0.5, 1.0, 1.00])
def test_average_progress_valid(self, average_progress):
obj = CourseDailyMetricsFactory(average_progress=average_progress)
serializer = CourseDailyMetricsSerializer(instance=obj)
check_val = Decimal(average_progress).quantize(Decimal('.00'))
data = serializer.data
assert data['average_progress'] == unicode(check_val)

@pytest.mark.xfail
@pytest.mark.parametrize('average_progress', [-1.0, 9.0])
def test_average_progress_not_valid(self, average_progress):
obj = CourseDailyMetricsFactory(average_progress=average_progress)
serializer = CourseDailyMetricsSerializer(instance=obj)
with pytest.raises(ValidationError):
data = serializer.data


@pytest.mark.django_db
class TestSiteDailyMetricsSerializer(object):
Expand Down Expand Up @@ -353,8 +370,12 @@ def test_get_metrics_with_cdm_records(self):
[CourseDailyMetricsFactory(site=self.site,
course_id=self.course_overview.id,
date_for=date) for date in dates]
assert self.serializer.get_metrics(
self.course_overview)['date_for'] == dates[-1]
data = self.serializer.get_metrics(self.course_overview)
assert data['date_for'] == dates[-1]

def test_get_metrics_with_no_cdm_records(self):
data = self.serializer.get_metrics(self.course_overview)
assert not data


class TestGeneralUserDataSerializer(object):
Expand Down