def programs(): """Fixture for a set of Programs in the database""" programs = ProgramFactory.create_batch(3) for program in programs: ProductVersionFactory.create(product=ProductFactory( content_object=program)) return programs
def test_create_program_enrollments(user): """ create_program_enrollments should create or reactivate local enrollment records """ num_programs = 2 order = OrderFactory.create() company = CompanyFactory.create() programs = ProgramFactory.create_batch(num_programs) # Create an existing deactivate enrollment to test that it gets reactivated ProgramEnrollmentFactory.create( user=user, program=programs[0], order=order, change_status=ENROLL_CHANGE_STATUS_REFUNDED, active=False, ) successful_enrollments = create_program_enrollments(user, programs, order=order, company=company) assert len(successful_enrollments) == num_programs enrollments = ProgramEnrollment.objects.order_by("program__id").all() assert len(enrollments) == len(programs) for (program, enrollment) in zip(programs, enrollments): assert enrollment.change_status is None assert enrollment.active is True assert enrollment.program == program
def test_get_program_run_enrollments(user): """ Test that the get_program_run_enrollments helper method for CourseRunEnrollment returns the appropriate course run enrollments for a program """ programs = ProgramFactory.create_batch(2) program = programs[0] course_run_enrollments = CourseRunEnrollmentFactory.create_batch( 2, user=user, run__course__program=factory.Iterator([program, program, programs[1]]), ) expected_run_enrollments = set(course_run_enrollments[0:2]) assert (set(CourseRunEnrollment.get_program_run_enrollments( user, program)) == expected_run_enrollments)
def test_create_program_enrollments_creation_fail(mocker, user): """ create_program_enrollments should log a message and send an admin email if there's an error during the creation of local enrollment records """ programs = ProgramFactory.create_batch(2) enrollment = ProgramEnrollmentFactory.build(program=programs[1]) mocker.patch( "courses.api.ProgramEnrollment.all_objects.get_or_create", side_effect=[Exception(), (enrollment, True)], ) patched_log_exception = mocker.patch("courses.api.log.exception") patched_mail_api = mocker.patch("courses.api.mail_api") successful_enrollments = create_program_enrollments(user, programs, order=None, company=None) patched_log_exception.assert_called_once() patched_mail_api.send_enrollment_failure_message.assert_called_once() assert successful_enrollments == [enrollment]