def setup(self, db): self.site = Site.objects.first() self.certificate_date = datetime.datetime(2018, 4, 1, tzinfo=utc) self.course_enrollment = CourseEnrollmentFactory() self.generated_certificate = GeneratedCertificateFactory( user=self.course_enrollment.user, course_id=self.course_enrollment.course_id, created_date=self.certificate_date, ) self.serializer = LearnerCourseDetailsSerializer( instance=self.course_enrollment)
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 queryset = CourseEnrollment.objects.filter(user=self.users[0]) serializer = LearnerCourseDetailsSerializer(queryset[0]) assert serializer.data
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
def setup(self, db): # self.model = CourseEnrollment # self.user_attributes = { # 'username': '******', # 'profile__name': 'Alpha One', # 'profile__country': 'CA', # } #self.user = UserFactory(**self.user_attributes) self.certificate_date = datetime.datetime(2018, 4, 1, tzinfo=utc) self.course_enrollment = CourseEnrollmentFactory() self.generated_certificate = GeneratedCertificateFactory( user=self.course_enrollment.user, course_id=self.course_enrollment.course_id, created_date=self.certificate_date, ) self.serializer = LearnerCourseDetailsSerializer( instance=self.course_enrollment)
class TestLearnerCourseDetailsSerializer(object): ''' ''' @pytest.fixture(autouse=True) def setup(self, db): self.site = Site.objects.first() self.certificate_date = datetime.datetime(2018, 4, 1, tzinfo=utc) self.course_enrollment = CourseEnrollmentFactory() self.generated_certificate = GeneratedCertificateFactory( user=self.course_enrollment.user, course_id=self.course_enrollment.course_id, created_date=self.certificate_date, ) self.serializer = LearnerCourseDetailsSerializer( instance=self.course_enrollment) def test_has_fields(self): expected_fields = set([ 'course_name', 'course_code', 'course_id', 'date_enrolled', 'progress_data', 'enrollment_id', ]) data = self.serializer.data assert set(data.keys()) == expected_fields def test_get_progress_data(self): """ Method should return data of the form: {'course_progress_history': [], 'course_progress_details': { 'sections_worked': 5, 'points_possible': 30.0, 'sections_possible': 10, 'points_earned': 15.0 }, 'course_progress': (0.5,), 'course_completed': datetime.datetime(2018, 4, 1, 0, 0, tzinfo=<UTC>) } """ metrics_data = dict(points_possible=1, points_earned=2, sections_worked=3, sections_possible=4) lcgm = LearnerCourseGradeMetricsFactory( user=self.course_enrollment.user, course_id=str(self.course_enrollment.course_id), **metrics_data) data = self.serializer.get_progress_data(self.course_enrollment) details = data['course_progress_details'] for key, val in metrics_data.items(): assert details[key] == val assert data['course_progress'] == lcgm.progress_percent assert data[ 'course_completed'] == self.generated_certificate.created_date def test_get_progress_data_with_no_data(self): """Tests that the serializer method succeeds when no learner course grade metrics records """ expected_data = { 'course_progress_history': [], 'course_progress_details': None, 'course_progress': 0.0, 'course_completed': False } assert not LearnerCourseGradeMetrics.objects.count() course_enrollment = CourseEnrollmentFactory() data = self.serializer.get_progress_data(course_enrollment) assert data == expected_data