def test_missing(self): ''' ''' [ CourseDailyMetricsFactory( date_for=self.date_for, course_id=self.course_overviews[0].id), CourseDailyMetricsFactory( date_for=self.date_for, course_id=self.course_overviews[1].id), ] expected_missing = [unicode(co.id) for co in self.course_overviews[2:]] actual = pipeline_sdm.missing_course_daily_metrics(date_for=self.date_for) assert actual == set(expected_missing)
def test_missing(self): [ CourseDailyMetricsFactory(date_for=self.date_for, site=self.site, course_id=self.course_overviews[0].id), CourseDailyMetricsFactory(date_for=self.date_for, site=self.site, course_id=self.course_overviews[1].id), ] expected_missing = [unicode(co.id) for co in self.course_overviews[2:]] actual = pipeline_sdm.missing_course_daily_metrics( site=self.site, date_for=self.date_for) assert set([str(obj) for obj in actual]) == set(expected_missing)
def setup(self, db): self.date_for = datetime.date(2018, 10, 1) self.site = Site.objects.first() self.users = [ UserFactory(date_joined=as_datetime(self.date_for - datetime.timedelta(days=60))) for i in range(0, 3) ] self.course_overviews = [ CourseOverviewFactory( created=as_datetime(self.date_for - datetime.timedelta(days=60))) for i in range(0, 3) ] self.cdm_recs = [ CourseDailyMetricsFactory(site=self.site, date_for=self.date_for, **cdm) for cdm in CDM_INPUT_TEST_DATA ] self.prev_day_sdm = SiteDailyMetricsFactory(site=self.site, date_for=prev_day( self.date_for), **SDM_DATA[1]) if is_multisite(): self.organization = OrganizationFactory(sites=[self.site]) for co in self.course_overviews: OrganizationCourseFactory(organization=self.organization, course_id=str(co.id)) if organizations_support_sites(): for user in self.users: UserOrganizationMappingFactory( user=user, organization=self.organization)
def test_latest_previous_record(self): course_overview = CourseOverviewFactory() # Create a set of records with non-continuous dates dates = [ datetime.date(2019, 10, 1), datetime.date(2019, 10, 2), datetime.date(2019, 10, 5), datetime.date(2019, 10, 29), datetime.date(2019, 11, 3), ] for rec_date in dates: cdms = CourseDailyMetricsFactory(site=self.site, course_id = course_overview.id, date_for=rec_date) rec = CourseDailyMetrics.latest_previous_record( site=self.site, course_id=course_overview.id) assert rec.date_for == dates[-1] rec2 = CourseDailyMetrics.latest_previous_record( site=self.site, course_id=course_overview.id, date_for=dates[-1]) assert rec2.date_for == dates[-2] rec3 = CourseDailyMetrics.latest_previous_record( site=self.site, course_id=course_overview.id, date_for=dates[0]) assert not rec3
def setup(self, db): self.date_for = datetime.date(2018, 6, 1) self.site = Site.objects.first() self.cdm_recs = [ CourseDailyMetricsFactory(site=self.site, date_for=self.date_for, **cdm) for cdm in CDM_INPUT_TEST_DATA ]
def test_no_missing(self): ''' ''' [CourseDailyMetricsFactory( date_for=self.date_for, course_id=course.id) for course in self.course_overviews] course_ids = pipeline_sdm.missing_course_daily_metrics( date_for=self.date_for) assert course_ids == set([])
def setup(self, db): self.model = CourseDailyMetrics self.date_fields = set([ 'date_for', 'created', 'modified', ]) self.expected_results_keys = set( [o.name for o in self.model._meta.fields]) field_names = (o.name for o in self.model._meta.fields if o.name not in self.date_fields) self.metrics = CourseDailyMetricsFactory() self.serializer = CourseDailyMetricsSerializer(instance=self.metrics)
def test_get_metrics_with_cdm_records(self): '''Tests we get the data for the latest CourseDailyMetrics object ''' dates = [ '2018-01-01', '2018-02-01', ] [ CourseDailyMetricsFactory(course_id=self.course_overview.id, date_for=date) for date in dates ] assert self.serializer.get_metrics( self.course_overview)['date_for'] == dates[-1]
def cdm_test_data(db, settings): """Build CourseDailyMetrics data to test certificate counts """ our_site = SiteFactory() mock_today = date(year=2020, month=6, day=7) last_month = mock_today - relativedelta(months=1) courses = [CourseOverviewFactory() for i in range(2)] # Create data for previous month. Just need one record # purpose is to make sure it is not included in our production code request prev_month_cdm = [ CourseDailyMetrics(site=our_site, course_id=str(courses[0].id), date_for=last_month) ] # Create data for our current month curr_month_cdm = [] cdm_data = [ dict(day=1, course_id=str(courses[0].id), num_learners_completed=1), dict(day=6, course_id=str(courses[0].id), num_learners_completed=10), dict(day=1, course_id=str(courses[1].id), num_learners_completed=2), dict(day=6, course_id=str(courses[1].id), num_learners_completed=20), ] expected_cert_count = 30 for rec in cdm_data: date_for = date(year=mock_today.year, month=mock_today.month, day=rec['day']) cdm = CourseDailyMetricsFactory( site=our_site, course_id=rec['course_id'], date_for=date_for, num_learners_completed=rec['num_learners_completed']) curr_month_cdm.append(cdm) if organizations_support_sites(): settings.FEATURES['FIGURES_IS_MULTISITE'] = True our_org = OrganizationFactory(sites=[our_site]) for course in courses: OrganizationCourseFactory(organization=our_org, course_id=str(course.id)) return dict( mock_today=mock_today, our_site=our_site, courses=courses, prev_month_cdm=prev_month_cdm, curr_month_cdm=curr_month_cdm, expected_cert_count=expected_cert_count, )
def setup(self, db): self.date_for = datetime.date(2018, 10, 1) self.users = [UserFactory( date_joined=as_datetime(self.date_for - datetime.timedelta(days=60)) ) for i in range(0, 3)] self.course_overviews = [CourseOverviewFactory( created=as_datetime(self.date_for - datetime.timedelta(days=60)) ) for i in range(0, 3)] self.cdm_recs = [CourseDailyMetricsFactory( date_for=self.date_for, **cdm ) for cdm in CDM_INPUT_TEST_DATA] self.prev_day_sdm = SiteDailyMetricsFactory( date_for=prev_day(self.date_for), **SDM_PREV_DAY[1])
def create_course_daily_metrics_data(site, start_date, end_date, course_id=None): ''' Creates a daily sequence of CourseDailyMetrics objects If course_id is provided as a parameter, then all CourseDailyMetrics objects will have that course_id. This is useful for testing time series for a specific course. Otherwise FactoryBoy assigns the course id in the ``tests.factories`` module ''' # Initial values data = dict(enrollment_count=2, active_learners_today=1, average_progress=0.5, average_days_to_complete=10, num_learners_completed=3) # keys and the values to increment by incr_data = dict(enrollment_count=3, active_learners_today=2, average_progress=0, average_days_to_complete=0, num_learners_completed=1) if course_id: data['course_id'] = course_id metrics = [] for dt in rrule(DAILY, dtstart=start_date, until=end_date): metrics.append( CourseDailyMetricsFactory(date_for=dt, site=site, **data)) # This only updates the keys that are present in the incr_data dict data.update( {key: data[key] + incr_data[key] for key in incr_data.keys()}) return metrics
def test_average_progress_not_valid(self, average_progress): obj = CourseDailyMetricsFactory(average_progress=average_progress) serializer = CourseDailyMetricsSerializer(instance=obj) with pytest.raises(ValidationError): data = serializer.data
def test_average_progress_valid(self, average_progress): obj = CourseDailyMetricsFactory(average_progress=average_progress) serializer = CourseDailyMetricsSerializer(instance=obj) check_val = Decimal(average_progress).quantize(Decimal('.00')) data = serializer.data assert data['average_progress'] == six.text_type(check_val)
def generate_cdm_series(site, first_day, last_day): return [ CourseDailyMetricsFactory(site=site, date_for=dt) for dt in rrule(DAILY, dtstart=first_day, until=last_day) ]
def setUp(self): self.models = [ CourseDailyMetricsFactory() for i in range(1, 10) ]
def mock_cdm_load(self, date_for, **kwargs): return ( CourseDailyMetricsFactory(date_for=date_for), created, )
def setup(self, db): self.date_for = datetime.date(2018, 6, 1) self.cdm_recs = [CourseDailyMetricsFactory( date_for=self.date_for, **cdm ) for cdm in CDM_INPUT_TEST_DATA]