def test_get_learner_details_list(self):
        """Tests retrieving a list of users with abbreviated details

        The fields in each returned record are identified by
            `figures.serializers.UserIndexSerializer`

        """

        request = APIRequestFactory().get(self.request_path)
        force_authenticate(request, user=self.caller)
        view = self.view_class.as_view({'get': 'list'})
        response = view(request)

        # Later, we'll elaborate on the tests. For now, some basic checks
        assert response.status_code == 200
        assert set(response.data.keys()) == set(
            ['count', 'next', 'previous', 'results'])

        results = response.data['results']
        assert len(results) == len(self.my_site_users)
        enrollments = get_course_enrollments_for_site(self.site)
        assert enrollments.count() == len(self.my_enrollments)

        for rec in results:
            assert set(rec.keys()) == set(self.expected_result_keys)
            # fail if we cannot find the user in the models
            user = get_user_model().objects.get(username=rec['username'])
            user_course_enrollments = enrollments.filter(user=user)
            # Check that each course enrollment is represented
            assert len(rec['courses']) == user_course_enrollments.count()
            for ce in user_course_enrollments:
                assert get_course_rec(ce.course_id, rec['courses'])
Exemplo n.º 2
0
def backfill_enrollment_data_for_site(site):
    """Convenience function to fill EnrollmentData records

    This backfills EnrollmentData records for existing CourseEnrollment
    and LearnerCourseGradeMetrics records

    ```

    Potential improvements: iterate by course id within site, have a function
    specific to a course. more queries, but breaks up the work
    """
    enrollment_data = []
    errors = []
    site_course_enrollments = get_course_enrollments_for_site(site)
    for rec in site_course_enrollments:
        try:
            obj, created = EnrollmentData.objects.set_enrollment_data(
                site=site, user=rec.user, course_id=rec.course_id)
            enrollment_data.append((obj, created))
        except CourseNotFound:
            msg = ('CourseNotFound for course "{course}". '
                   ' CourseEnrollment ID={ce_id}')
            errors.append(msg.format(course=str(rec.course_id), ce_id=rec.id))

    return dict(results=enrollment_data, errors=errors)
    def test_serializer(self):
        '''
        This test makes sure the serializer works with the test data provided
        in this Test class
        '''

        # Spot test with the first CourseEnrollment for the first user
        enrollments = get_course_enrollments_for_site(self.site)
        queryset = enrollments.filter(user=self.my_site_users[0])
        assert queryset
        serializer = LearnerCourseDetailsSerializer(queryset[0])
        # We're asserting that we can get the serializer `data` property without
        # error, not checking the contents of the data. That should be done in
        # the serializer specific tests (see tests/test_serializers.py).
        assert serializer.data
Exemplo n.º 4
0
def backfill_enrollment_data_for_site(site):
    """Convenience function to fill EnrollmentData records

    This backfills EnrollmentData records for existing CourseEnrollment
    and LearnerCourseGradeMetrics records.

    The only exception it handles is `figures.compat.CourseNotFound`. All other
    exceptions are passed through this function to its caller.

    Potential improvements: iterate by course id within site, have a function
    specific to a course. more queries, but breaks up the work

    TODO: move the contents of this function to
      `figures.tasks.update_enrollment_data`

    TODO: Performance and handling improvement:
      * Get course ids for which the site has enrollments (Skip over courses
        without enrollments)
      * Check if the course actually exists
      * If so, iterate over enrollments for the course
      * Else log the error. This is a good candidate to track in a Figures
        model, like a future reworked 'PipelineError'
    """
    enrollment_data = []
    errors = []
    site_course_enrollments = get_course_enrollments_for_site(site)
    for rec in site_course_enrollments:
        try:
            obj, created = EnrollmentData.objects.set_enrollment_data(
                site=site, user=rec.user, course_id=rec.course_id)
            enrollment_data.append((obj, created))
        except CourseNotFound:
            msg = ('CourseNotFound for course "{course}". '
                   ' CourseEnrollment ID={ce_id}')
            errors.append(msg.format(course=str(rec.course_id), ce_id=rec.id))

    return dict(results=enrollment_data, errors=errors)