diff --git a/lms/djangoapps/program_enrollments/models.py b/lms/djangoapps/program_enrollments/models.py index 3ed2b36680..d552656abc 100644 --- a/lms/djangoapps/program_enrollments/models.py +++ b/lms/djangoapps/program_enrollments/models.py @@ -17,7 +17,7 @@ from student.models import CourseEnrollment from .constants import ProgramCourseEnrollmentStatuses, ProgramEnrollmentStatuses -class ProgramEnrollment(TimeStampedModel): # pylint: disable=model-missing-unicode +class ProgramEnrollment(TimeStampedModel): """ This is a model for Program Enrollments from the registrar service @@ -78,8 +78,20 @@ class ProgramEnrollment(TimeStampedModel): # pylint: disable=model-missing-unic def __str__(self): return '[ProgramEnrollment id={}]'.format(self.id) + def __repr__(self): + return ( + "" + ).format(self=self) -class ProgramCourseEnrollment(TimeStampedModel): # pylint: disable=model-missing-unicode + +class ProgramCourseEnrollment(TimeStampedModel): """ This is a model to represent a learner's enrollment in a course in the context of a program from the registrar service @@ -126,3 +138,14 @@ class ProgramCourseEnrollment(TimeStampedModel): # pylint: disable=model-missin def __str__(self): return '[ProgramCourseEnrollment id={}]'.format(self.id) + + def __repr__(self): + return ( + "" + " course_key={self.course_key}" + " status={self.status!r}" + ">" + ).format(self=self) diff --git a/lms/djangoapps/program_enrollments/tests/test_models.py b/lms/djangoapps/program_enrollments/tests/test_models.py index 5a742112c3..7610b3c7e5 100644 --- a/lms/djangoapps/program_enrollments/tests/test_models.py +++ b/lms/djangoapps/program_enrollments/tests/test_models.py @@ -3,7 +3,7 @@ Unit tests for ProgramEnrollment models. """ -from uuid import uuid4 +from uuid import UUID import ddt from django.db.utils import IntegrityError @@ -12,11 +12,12 @@ from edx_django_utils.cache import RequestCache from opaque_keys.edx.keys import CourseKey from course_modes.models import CourseMode -from lms.djangoapps.program_enrollments.models import ProgramCourseEnrollment, ProgramEnrollment -from openedx.core.djangoapps.catalog.tests.factories import generate_course_run_key from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory from student.tests.factories import CourseEnrollmentFactory, UserFactory +from ..models import ProgramEnrollment +from .factories import ProgramCourseEnrollmentFactory, ProgramEnrollmentFactory + class ProgramEnrollmentModelTests(TestCase): """ @@ -27,11 +28,11 @@ class ProgramEnrollmentModelTests(TestCase): Set up the test data used in the specific tests """ super(ProgramEnrollmentModelTests, self).setUp() - self.user = UserFactory.create() - self.program_uuid = uuid4() - self.other_program_uuid = uuid4() - self.curriculum_uuid = uuid4() - self.enrollment = ProgramEnrollment.objects.create( + self.user = UserFactory(username="rocko") + self.program_uuid = UUID("88888888-4444-2222-1111-000000000000") + self.other_program_uuid = UUID("88888888-4444-3333-1111-000000000000") + self.curriculum_uuid = UUID("77777777-4444-2222-1111-000000000000") + self.enrollment = ProgramEnrollmentFactory( user=self.user, external_user_key='abc', program_uuid=self.program_uuid, @@ -39,12 +40,24 @@ class ProgramEnrollmentModelTests(TestCase): status='enrolled' ) + def test_str_and_repr(self): + """ + Make sure str() and repr() work correctly on instances of this model. + """ + assert str(self.enrollment) == "[ProgramEnrollment id=1]" + assert repr(self.enrollment) == ( + " external_user_key='abc'" + " program_uuid=UUID('88888888-4444-2222-1111-000000000000')" + " curriculum_uuid=UUID('77777777-4444-2222-1111-000000000000')" + " status='enrolled'>" + ) + def test_unique_external_key_program_curriculum(self): """ A record with the same (external_user_key, program_uuid, curriculum_uuid) cannot be duplicated. """ with self.assertRaises(IntegrityError): - _ = ProgramEnrollment.objects.create( + _ = ProgramEnrollmentFactory( user=None, external_user_key='abc', program_uuid=self.program_uuid, @@ -57,7 +70,7 @@ class ProgramEnrollmentModelTests(TestCase): A record with the same (user, program_uuid, curriculum_uuid) cannot be duplicated. """ with self.assertRaises(IntegrityError): - _ = ProgramEnrollment.objects.create( + _ = ProgramEnrollmentFactory( user=self.user, external_user_key=None, program_uuid=self.program_uuid, @@ -104,18 +117,38 @@ class ProgramCourseEnrollmentModelTests(TestCase): """ super(ProgramCourseEnrollmentModelTests, self).setUp() RequestCache.clear_all_namespaces() - self.user = UserFactory.create() - self.program_uuid = uuid4() - self.program_enrollment = ProgramEnrollment.objects.create( + self.user = UserFactory(username="rocko") + self.program_uuid = UUID("88888888-4444-2222-1111-000000000000") + self.curriculum_uuid = UUID("77777777-4444-2222-1111-000000000000") + self.program_enrollment = ProgramEnrollmentFactory( user=self.user, external_user_key='abc', program_uuid=self.program_uuid, - curriculum_uuid=uuid4(), + curriculum_uuid=self.curriculum_uuid, status='enrolled' ) - self.course_key = CourseKey.from_string(generate_course_run_key()) + self.course_key = CourseKey.from_string("course-v1:blah+blah+blah") CourseOverviewFactory(id=self.course_key) + def test_str_and_repr(self): + """ + Make sure str() and repr() work correctly on instances of this model. + """ + pce = self._create_completed_program_course_enrollment() + assert str(pce) == "[ProgramCourseEnrollment id=1]" + # The course enrollment contains timestamp information, + # so to avoid dealing with that, let's just test the parts of the repr() + # that come before that. + assert ( + "" + " external_user_key='abc'" + " program_uuid=UUID('88888888-4444-2222-1111-000000000000')" + " curriculum_uuid=UUID('77777777-4444-2222-1111-000000000000')" + " status='enrolled'>" + " course_enrollment=<[CourseEnrollment] rocko: course-v1:blah+blah+blah" + ) in repr(pce) + def test_duplicate_enrollments_allowed(self): """ A record with the same (program_enrollment, course_enrollment) @@ -123,7 +156,7 @@ class ProgramCourseEnrollmentModelTests(TestCase): same course_enrollment """ pce = self._create_completed_program_course_enrollment() - ProgramCourseEnrollment.objects.create( + ProgramCourseEnrollmentFactory( program_enrollment=pce.program_enrollment, course_key="course-v1:dummy+value+101", course_enrollment=pce.course_enrollment, @@ -137,7 +170,7 @@ class ProgramCourseEnrollmentModelTests(TestCase): """ pce = self._create_waiting_program_course_enrollment() with self.assertRaises(IntegrityError): - ProgramCourseEnrollment.objects.create( + ProgramCourseEnrollmentFactory( program_enrollment=pce.program_enrollment, course_key=pce.course_key, course_enrollment=None, @@ -151,7 +184,7 @@ class ProgramCourseEnrollmentModelTests(TestCase): user=self.user, mode=CourseMode.MASTERS ) - program_course_enrollment = ProgramCourseEnrollment.objects.create( + program_course_enrollment = ProgramCourseEnrollmentFactory( program_enrollment=self.program_enrollment, course_key=self.course_key, course_enrollment=course_enrollment, @@ -161,7 +194,7 @@ class ProgramCourseEnrollmentModelTests(TestCase): def _create_waiting_program_course_enrollment(self): """ helper function create program course enrollment with no lms user """ - return ProgramCourseEnrollment.objects.create( + return ProgramCourseEnrollmentFactory( program_enrollment=self.program_enrollment, course_key=self.course_key, course_enrollment=None,