def test_external_course_page_faculty_subpage(): """ FacultyMembersPage should return expected values if associated with ExternalCoursePage """ external_course_page = ExternalCoursePageFactory.create() assert not external_course_page.faculty FacultyMembersPageFactory.create(parent=external_course_page, members=json.dumps( _get_faculty_members())) _assert_faculty_members(external_course_page)
def test_program_page_faculty_subpage(): """ FacultyMembersPage should return expected values if associated with ProgramPage """ program_page = ProgramPageFactory.create() assert not program_page.faculty FacultyMembersPageFactory.create(parent=program_page, members=json.dumps( _get_faculty_members())) _assert_faculty_members(program_page)
def test_instructors(has_page): """CourseRun.instructors should list instructors from the related CMS page, or provide an empty list""" faculty_names = ["Teacher One", "Teacher Two"] course_run = CourseRunFactory.create(course__page=None) if has_page: course_page = CoursePageFactory.create(course=course_run.course) FacultyMembersPageFactory.create( parent=course_page, **{ f"members__{idx}__member__name": name for idx, name in enumerate(faculty_names) }, ) assert course_run.instructors == ([{ "name": name } for name in faculty_names] if has_page else [])
def test_serialize_course_run(has_product): """Test CourseRun serialization""" faculty_names = ["Emma Jones", "Joe Smith"] course_run = CourseRunFactory.create() FacultyMembersPageFactory.create( parent=course_run.course.page, **{ f"members__{idx}__member__name": name for idx, name in enumerate(faculty_names) }, ) product_id = (ProductVersionFactory.create( product__content_object=course_run).product.id if has_product else None) course_run.refresh_from_db() data = CourseRunSerializer(course_run).data assert_drf_json_equal( data, { "title": course_run.title, "courseware_id": course_run.courseware_id, "run_tag": course_run.run_tag, "courseware_url": course_run.courseware_url, "start_date": drf_datetime(course_run.start_date), "end_date": drf_datetime(course_run.end_date), "enrollment_start": drf_datetime(course_run.enrollment_start), "enrollment_end": drf_datetime(course_run.enrollment_end), "expiration_date": drf_datetime(course_run.expiration_date), "current_price": course_run.current_price, "instructors": course_run.instructors, "id": course_run.id, "product_id": product_id, }, )
def test_serialize_program(mock_context, has_product): """Test Program serialization""" program = ProgramFactory.create() run1 = CourseRunFactory.create(course__program=program) course1 = run1.course run2 = CourseRunFactory.create(course__program=program) course2 = run2.course runs = ([run1, run2] + [CourseRunFactory.create(course=course1) for _ in range(2)] + [CourseRunFactory.create(course=course2) for _ in range(2)]) faculty_names = ["Teacher 1", "Teacher 2"] FacultyMembersPageFactory.create( parent=program.page, **{ f"members__{idx}__member__name": name for idx, name in enumerate(faculty_names) }, ) if has_product: ProductVersionFactory.create(product__content_object=program) topics = [ CourseTopic.objects.create(name=f"topic{num}") for num in range(3) ] course1.topics.set([topics[0], topics[1]]) course2.topics.set([topics[1], topics[2]]) data = ProgramSerializer(instance=program, context=mock_context).data assert_drf_json_equal( data, { "title": program.title, "readable_id": program.readable_id, "id": program.id, "description": program.page.description, "courses": [ CourseSerializer(instance=course, context={ **mock_context, "filter_products": False }).data for course in [course1, course2] ], "thumbnail_url": f"http://localhost:8053{program.page.thumbnail_image.file.url}", "current_price": program.current_price, "start_date": sorted(runs, key=lambda run: run.start_date) [0].start_date.strftime(datetime_format), "end_date": sorted(runs, key=lambda run: run.end_date)[-1].end_date.strftime( datetime_format), "enrollment_start": sorted(runs, key=lambda run: run.enrollment_start) [0].enrollment_start.strftime(datetime_format), "url": f"http://localhost{program.page.get_url()}", "instructors": [{ "name": name } for name in faculty_names], "topics": [{ "name": topic.name } for topic in topics], }, )