From e3faba15cca87bc204b8eed55f3949b674c9ee25 Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Fri, 11 Mar 2022 13:37:05 -0500 Subject: [PATCH] Fix backfill_enrollment_data_for_course task * 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 --- figures/tasks.py | 13 +++++-- tests/tasks/test_backfill_tasks.py | 55 ++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/figures/tasks.py b/figures/tasks.py index b731d29c0..e497be85d 100644 --- a/figures/tasks.py +++ b/figures/tasks.py @@ -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))) # diff --git a/tests/tasks/test_backfill_tasks.py b/tests/tasks/test_backfill_tasks.py index 7c5420fe1..2e870d6eb 100644 --- a/tests/tasks/test_backfill_tasks.py +++ b/tests/tasks/test_backfill_tasks.py @@ -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))