def test_needs_update_has_lcgm(self, monkeypatch): """We have an LCGM record, but it is not up to date with the SM The function under test should return a new LCGM TODO: Add test where we pass in `date_for` to the function under test """ lcgm = self.create_lcgm(date_for=self.date_1) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics.get_site_for_course', lambda val: self.site) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics._collect_progress_data', lambda val: self.progress_data) course_sm = StudentModule.objects.filter( course_id=self.course_enrollment.course_id) metrics = collect_metrics_for_enrollment( site=self.site, course_enrollment=self.course_enrollment, course_sm=course_sm) self.check_response_metrics(metrics, self.progress_data) assert metrics != lcgm # This works because we pick dates in the past. Better is to fix this # by either using freezegun or monkeypatching `_add_enrollment_metrics_record assert metrics.date_for >= self.date_2.date()
def test_no_update_has_lcgm_no_sm(self, monkeypatch): """We have an LCGM but not an SM record The function under test should return the existing LCGM """ monkeypatch.setattr( 'figures.pipeline.enrollment_metrics.get_site_for_course', lambda val: self.site) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics._collect_progress_data', lambda val: self.progress_data) # Create a course enrollment for which we won't have student module records ce = CourseEnrollmentFactory( course_id=self.course_enrollment.course_id) if organizations_support_sites(): UserOrganizationMappingFactory(organization=self.org, user=ce.user) lcgm = LearnerCourseGradeMetricsFactory(course_id=ce.course_id, user=ce.user) ce_sm = StudentModule.objects.filter(course_id=ce.course_id, student_id=ce.user.id) assert not ce_sm metrics = collect_metrics_for_enrollment(site=self.site, course_enrollment=ce, date_for=self.today, student_modules=ce_sm) assert not metrics
def test_does_not_need_update(self, monkeypatch): """We have an LCGM record that is up to date with the SM The function under test should return the LCGM TODO: Add test where we pass in `date_for` to the function under test """ lcgm = self.create_lcgm(date_for=self.datetime_2.date(), course_enrollment=self.course_enrollment) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics.get_site_for_course', lambda val: self.site) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics._collect_progress_data', lambda val: self.progress_data) metrics = collect_metrics_for_enrollment( site=self.site, course_enrollment=self.course_enrollment, date_for=self.today, student_modules=self.learner_sm) self.check_response_metrics(metrics, self.progress_data) assert metrics == lcgm assert metrics.date_for == self.datetime_2.date()
def test_no_update_no_lcgm_no_sm(self, monkeypatch): """We have neither an LCGM nor an SM record The function under test should return None """ monkeypatch.setattr( 'figures.pipeline.enrollment_metrics.get_site_for_course', lambda val: self.site) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics._collect_progress_data', lambda val: self.progress_data) # Create a course enrollment for which we won't have student module records ce = CourseEnrollmentFactory() course_sm = StudentModule.objects.filter(course_id=ce.course_id) metrics = collect_metrics_for_enrollment(site=self.site, course_enrollment=ce, course_sm=course_sm) assert not metrics
def test_has_no_lcgm_and_needs_update(self, monkeypatch): """We have an SM record, but no LCGM record The function under test should return a new LCGM record """ assert not LearnerCourseGradeMetrics.objects.count() monkeypatch.setattr( 'figures.pipeline.enrollment_metrics.get_site_for_course', lambda val: self.site) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics._collect_progress_data', lambda val: self.progress_data) metrics = collect_metrics_for_enrollment( site=self.site, course_enrollment=self.course_enrollment, date_for=self.today, student_modules=self.learner_sm) self.check_response_metrics(metrics, self.progress_data) assert LearnerCourseGradeMetrics.objects.count() == 1
def test_has_lcgm_and_needs_update(self, monkeypatch): """We have an LCGM record, but it is not up to date with the SM The function under test should return a new LCGM TODO: Add test where we pass in `date_for` to the function under test """ assert not LearnerCourseGradeMetrics.objects.filter( user=self.course_enrollment.user, course_id=str(self.course_enrollment.course_id)).exists() lcgm = self.create_lcgm(date_for=self.datetime_1.date(), course_enrollment=self.course_enrollment) # lcgm = LearnerCourseGradeMetricsFactory() # Maybe remove these, but first make code sample to show how to mock # multisite. This helps in abstracting our tests, but risks wrong # behavior in production # If we do monkeypatch like this, we will need to also monkeypatch the # user belonging to the site monkeypatch.setattr( 'figures.pipeline.enrollment_metrics.get_site_for_course', lambda val: self.site) monkeypatch.setattr( 'figures.pipeline.enrollment_metrics._collect_progress_data', lambda val: self.progress_data) # assert isinstance(lcgm.date_for, date) # import pdb; pdb.set_trace() assert _enrollment_metrics_needs_update(lcgm, self.learner_sm[0]) metrics = collect_metrics_for_enrollment( site=self.site, course_enrollment=self.course_enrollment, date_for=self.today, student_modules=self.learner_sm) self.check_response_metrics(metrics, self.progress_data) assert metrics != lcgm # This works because we pick dates in the past. Better is to fix this # by either using freezegun or monkeypatching `_new_enrollment_metrics_record assert metrics.date_for >= self.datetime_2.date()