Skip to content

Commit

Permalink
Enrollment Metrics - Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbaldwin committed Jul 15, 2020
1 parent af91760 commit 533da01
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
7 changes: 5 additions & 2 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 @@ -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 == 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 == True:
# This is a hack until we add `completed` field to LCGM
return queryset.filter(sections_worked__lt=F('sections_possible'))
else:
Expand Down
52 changes: 52 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.db.models import F
from django.test import TestCase

from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
Expand All @@ -19,6 +20,7 @@
CourseDailyMetricsFilter,
CourseEnrollmentFilter,
CourseOverviewFilter,
EnrollmentMetricsFilter,
SiteDailyMetricsFilter,
CourseMauMetricsFilter,
SiteMauMetricsFilter,
Expand All @@ -30,13 +32,15 @@
SiteDailyMetrics,
CourseMauMetrics,
SiteMauMetrics,
LearnerCourseGradeMetrics,
)

from tests.factories import (
CourseDailyMetricsFactory,
CourseEnrollmentFactory,
CourseMauMetricsFactory,
CourseOverviewFactory,
LearnerCourseGradeMetricsFactory,
SiteDailyMetricsFactory,
SiteMauMetricsFactory,
SiteFactory,
Expand Down Expand Up @@ -278,6 +282,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_completed_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
2 changes: 1 addition & 1 deletion tests/views/test_enrollment_metrics_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def make_request(self, monkeypatch, request_path, site, caller, action):
view = self.view_class.as_view({'get': action})
return view(request)

def test_list_method(self, monkeypatch, enrollment_test_data):
def test_list_method_all(self, monkeypatch, enrollment_test_data):
site = enrollment_test_data['site']
users = enrollment_test_data['users']

Expand Down

0 comments on commit 533da01

Please sign in to comment.