Skip to content

Commit

Permalink
Fix backfill_enrollment_data_for_course task
Browse files Browse the repository at this point in the history
* Problem was that `update_enrollment_data_for_course` returns a list of
tuples. Each tuple is `(object, created)`, where `object` is the
EnrollmentData record and `created` tells if the record was created
* Added logging to the backfill task
* Updated tests
  • Loading branch information
johnbaldwin committed Mar 11, 2022
1 parent 21838ef commit e3faba1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
13 changes: 10 additions & 3 deletions figures/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,20 @@ def populate_daily_metrics_next(site_id=None, force_update=False):
def backfill_enrollment_data_for_course(course_id):
"""Create or update EnrollmentData records for the course
Simple wrapper to run the enrollment update as a Celery task
This is a simple wrapper to run the enrollment update as a Celery task
We usually run this task through the Figures Django management command,
`backfill_figures_enrollment_data`
"""
ed_objects = update_enrollment_data_for_course(course_id)
return [obj.id for obj in ed_objects]
# results are a list of (object, created_flag) tuples
updated = update_enrollment_data_for_course(course_id)
# Do we want to log this?
# ids = [obj.id for obj, _ in ed_objects]
# Since this is a Celery task, don't return a value
msg = ('figures.tasks.backfill_enrollment_data_for_course "{course_id}".'
' Updated {edrec_count} enrollment data records.')

logger.info(msg.format(course_id=course_id, edrec_count=len(updated)))


#
Expand Down
55 changes: 45 additions & 10 deletions tests/tasks/test_backfill_tasks.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
"""Test Figures backfill Celery tasks
"""
from __future__ import absolute_import
import logging
import pytest

from figures.tasks import backfill_enrollment_data_for_course

from tests.factories import EnrollmentDataFactory


def test_backfill_enrollment_data_for_course(transactional_db, monkeypatch):
"""
The Celery task is a simple wrapper around the pipeline function
"""
course_id = 'course-v1:SomeOrg+SomeNum+SomeRun'
ed_recs = [EnrollmentDataFactory() for _ in range(2)]
@pytest.mark.django_db
class TestBackfillEnrollmentDataForCourse(object):

func_path = 'figures.tasks.update_enrollment_data_for_course'
monkeypatch.setattr(func_path, lambda course_id: ed_recs)
ed_ids = backfill_enrollment_data_for_course(course_id)
assert set(ed_ids) == set([obj.id for obj in ed_recs])
@pytest.fixture(autouse=True)
def setup(self, db):
self.expected_message_template = (
'figures.tasks.backfill_enrollment_data_for_course "{course_id}".'
' Updated {edrec_count} enrollment data records.')

def test_backfill_enrollment_data_for_course_no_update(self, transactional_db,
monkeypatch, caplog):
"""
The Celery task is a simple wrapper around the pipeline function
"""
course_id = 'course-v1:SomeOrg+SomeNum+SomeRun'

# The function returns a list of tuples with (object, created)
# ed_recs = [(EnrollmentDataFactory(), False) for _ in range(2)]
caplog.set_level(logging.INFO)
func_path = 'figures.tasks.update_enrollment_data_for_course'
monkeypatch.setattr(func_path, lambda course_id: [])
backfill_enrollment_data_for_course(course_id)
assert len(caplog.records) == 1
assert caplog.records[0].message == self.expected_message_template.format(
course_id=course_id,
edrec_count=0)

def test_backfill_enrollment_data_for_course_with_updates(self, transactional_db,
monkeypatch, caplog):
"""
The Celery task is a simple wrapper around the pipeline function
"""
course_id = 'course-v1:SomeOrg+SomeNum+SomeRun'

# The function returns a list of tuples with (object, created)
ed_recs = [(EnrollmentDataFactory(), False) for _ in range(2)]
caplog.set_level(logging.INFO)
func_path = 'figures.tasks.update_enrollment_data_for_course'
monkeypatch.setattr(func_path, lambda course_id: ed_recs)
backfill_enrollment_data_for_course(course_id)
assert len(caplog.records) == 1
assert caplog.records[0].message == self.expected_message_template.format(
course_id=course_id,
edrec_count=len(ed_recs))

0 comments on commit e3faba1

Please sign in to comment.