def test_lists_live_programs(self): """Live programs should show up""" prog = ProgramFactory.create(live=True) resp = self.client.get(reverse('program-list')) assert len(resp.json()) == 1 context = {"request": Mock(user=self.user)} data = ProgramSerializer(prog, context=context).data assert [data] == resp.json()
def test_program_no_programpage(self): """ Test ProgramSerializer without a program page """ data = ProgramSerializer(self.program, context=self.context).data assert data == { 'id': self.program.id, 'title': self.program.title, 'programpage_url': None, 'enrolled': False, 'total_courses': 0, }
def test_program_no_programpage(self): """ Test ProgramSerializer without a program page """ data = ProgramSerializer(self.program, context=self.context).data assert data == { 'id': self.program.id, 'title': self.program.title, 'programpage_url': None, 'enrolled': False, 'total_courses': 0, 'topics': [{'name': topic.name} for topic in self.program.topics.iterator()] }
def test_program_courses(self): """ Test ProgramSerializer with multiple courses """ CourseFactory.create_batch(5, program=self.program) data = ProgramSerializer(self.program, context=self.context).data assert data == { 'id': self.program.id, 'title': self.program.title, 'programpage_url': None, 'enrolled': False, 'total_courses': 5, }
def test_program_enrolled(self): """ Test ProgramSerializer with an enrolled user """ ProgramEnrollment.objects.create(user=self.user, program=self.program) data = ProgramSerializer(self.program, context=self.context).data assert data == { 'id': self.program.id, 'title': self.program.title, 'programpage_url': None, 'enrolled': True, 'total_courses': 0, }
def test_program_with_programpage(self): """ Test ProgramSerializer with a program page attached """ programpage = ProgramPageFactory.build(program=self.program) homepage = HomePage.objects.first() homepage.add_child(instance=programpage) data = ProgramSerializer(self.program, context=self.context).data assert data == { 'id': self.program.id, 'title': self.program.title, 'programpage_url': programpage.url, 'enrolled': False, 'total_courses': 0, } assert len(programpage.url) > 0
def programs_for_sign_up(programs): """formats program info for the signup dialogs""" return [ProgramSerializer().to_representation(p) for p in programs]
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], }, )