Skip to content

Commit

Permalink
Updated enrollment metrics test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbaldwin committed Jul 15, 2020
1 parent 12eca82 commit d611dd8
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 31 deletions.
9 changes: 6 additions & 3 deletions figures/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
See the following for breaking changes when upgrading to Django Filter 1.0:
https://django-filter.readthedocs.io/en/master/guide/migration.html#migrating-to-1-0
TODO: Rename classes so they eiher all end with "Filter" or "FilterSet" then
update the test class names in "tests/test_filters.py" to match.
"""

from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -50,7 +53,7 @@ def boolean_method_filter(method):
First check for old style (pre version 1 Django Filters)
"""
if hasattr(django_filters, 'MethodFilter'):
return django_filters.MethodFilter(action=method)
return django_filters.MethodFilter(action=method) # pylint: disable=no-member
else:
return django_filters.BooleanFilter(method=method)

Expand Down Expand Up @@ -166,7 +169,7 @@ def filter_only_completed(self, queryset, name, value): # pylint: disable=unuse
"""
The "value" parameter is either `True` or `False`
"""
if value:
if value is True:
return queryset.filter(sections_possible__gt=0,
sections_worked=F('sections_possible'))
else:
Expand All @@ -176,7 +179,7 @@ def filter_exclude_completed(self, queryset, name, value): # pylint: disable=un
"""
The "value" parameter is either `True` or `False`
"""
if value:
if value is True:
# This is a hack until we add `completed` field to LCGM
return queryset.filter(sections_worked__lt=F('sections_possible'))
else:
Expand Down
4 changes: 2 additions & 2 deletions figures/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def get_queryset(self):
return queryset

@list_route()
def completed_ids(self, request, *args, **kwargs):
def completed_ids(self, request):
"""Return distinct course id/user id pairs for completed enrollments
Endpoint is `/figures/api/enrollment-metrics/completed_ids/`
Expand All @@ -438,7 +438,7 @@ def completed_ids(self, request, *args, **kwargs):
return Response(serializer.data)

@list_route()
def completed(self, reqwuest, *args, **kwargs):
def completed(self, request):
"""Experimental endpoint to return completed LCGM records
This is the same as `/figures/api/enrollment-metrics/?only_completed=True
Expand Down
52 changes: 51 additions & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
CourseDailyMetricsFilter,
CourseEnrollmentFilter,
CourseOverviewFilter,
EnrollmentMetricsFilter,
SiteDailyMetricsFilter,
CourseMauMetricsFilter,
SiteMauMetricsFilter,
Expand All @@ -30,13 +31,15 @@
SiteDailyMetrics,
CourseMauMetrics,
SiteMauMetrics,
LearnerCourseGradeMetrics,
)

from tests.factories import (
CourseDailyMetricsFactory,
CourseEnrollmentFactory,
CourseMauMetricsFactory,
CourseOverviewFactory,
LearnerCourseGradeMetricsFactory,
SiteDailyMetricsFactory,
SiteMauMetricsFactory,
SiteFactory,
Expand Down Expand Up @@ -101,7 +104,6 @@ def test_filter_course_id(self):
course_id = CourseEnrollment.objects.all()[0].course_id
expected_results = CourseEnrollment.objects.filter(course_id=course_id)
assert expected_results.count() != len(self.course_enrollments)
f = CourseEnrollmentFilter(queryset=expected_results)

res = CourseEnrollmentFilter().filter_course_id(
queryset=CourseEnrollment.objects.all(),
Expand Down Expand Up @@ -278,6 +280,54 @@ def test_get_by_date(self):
lambda o: o.id, ordered=False)


@pytest.mark.skipif(django_filters_pre_v1(),
reason='Django Filter backward compatibility not implemented')
@pytest.mark.django_db
class EnrollmentMetricsFilterTest(TestCase):
"""
Initially adding coverage where view tests are not covering
"""
def setUp(self):
self.site = SiteFactory()

self.not_complete = LearnerCourseGradeMetricsFactory(site=self.site,
sections_worked=1,
sections_possible=2)
self.complete = LearnerCourseGradeMetricsFactory(site=self.site,
sections_worked=2,
sections_possible=2)
self.site_qs = LearnerCourseGradeMetrics.objects.filter(site=self.site)
self.filter = EnrollmentMetricsFilter(queryset=self.site_qs)

def test_filter_only_completed(self):
qs = self.filter.filter_only_completed(queryset=self.site_qs,
name='only_completed',
value=True)
assert qs.count() == 1 and qs[0] == self.complete

def test_filter_only_completed_no_value(self):
"""Test that the method returns the queryset passed in
"""
qs = self.filter.filter_only_completed(queryset=self.site_qs,
name='only_completed',
value=False)
assert qs == self.site_qs

def test_filter_exclude_completed(self):
qs = self.filter.filter_exclude_completed(queryset=self.site_qs,
name='exclude_completed',
value=True)
assert qs.count() == 1 and qs[0] == self.not_complete

def test_filter_only_excluded_no_value(self):
"""Test that the method returns the queryset passed in
"""
qs = self.filter.filter_exclude_completed(queryset=self.site_qs,
name='exclude_completed',
value=False)
assert qs == self.site_qs


@pytest.mark.skipif(django_filters_pre_v1(),
reason='Django Filter backward compatibility not implemented')
@pytest.mark.django_db
Expand Down
Loading

0 comments on commit d611dd8

Please sign in to comment.