def test_data_excluded_course_run(self):
        """
        If a course run is excluded on a program, that program should not be
        returned for that course run on the course run endpoint.
        """
        serializer = CourseRunWithProgramsSerializer(self.course_run, context=self.serializer_context)
        ProgramFactory(courses=[self.course_run.course], excluded_course_runs=[self.course_run])
        expected = CourseRunSerializer(self.course_run, context=self.serializer_context).data
        expected.update({
            'programs': [],
        })

        self.assertDictEqual(serializer.data, expected)
    def test_data(self):
        serializer = CourseRunWithProgramsSerializer(self.course_run, context=self.serializer_context)
        ProgramFactory(courses=[self.course_run.course])

        expected = CourseRunSerializer(self.course_run, context=self.serializer_context).data
        expected.update({
            'programs': NestedProgramSerializer(
                self.course_run.course.programs,
                many=True,
                context=self.serializer_context
            ).data,
        })

        self.assertDictEqual(serializer.data, expected)
    def test_data(self):
        serializer = CourseRunWithProgramsSerializer(
            self.course_run, context=self.serializer_context)
        ProgramFactory(courses=[self.course_run.course])

        expected = CourseRunSerializer(self.course_run,
                                       context=self.serializer_context).data
        expected.update({
            'programs':
            NestedProgramSerializer(self.course_run.course.programs,
                                    many=True,
                                    context=self.serializer_context).data,
        })

        self.assertDictEqual(serializer.data, expected)
    def get_expected_data(self, request, course_run):
        course = course_run.course
        serializer_context = {'request': request}
        expected = dict(
            CourseRunSerializer(course_run, context=serializer_context).data)
        expected.update({
            'subjects':
            self.serialize_items(course.subjects.all(), 'name'),
            'seats':
            self.serialize_seats(course_run),
            'owners':
            self.serialize_items(course.authoring_organizations.all(), 'key'),
            'sponsors':
            self.serialize_items(course.sponsoring_organizations.all(), 'key'),
            'prerequisites':
            self.serialize_items(course.prerequisites.all(), 'name'),
            'level_type':
            course_run.level_type.name if course_run.level_type else None,
            'expected_learning_items':
            self.serialize_items(course.expected_learning_items.all(),
                                 'value'),
            'course_key':
            course.key,
            'image':
            ImageField().to_representation(course_run.card_image_url),
        })

        # Remove fields found in CourseRunSerializer, but not in FlattenedCourseRunWithCourseSerializer.
        fields_to_remove = set(CourseRunSerializer.Meta.fields) - set(
            FlattenedCourseRunWithCourseSerializer.Meta.fields)
        for key in fields_to_remove:
            del expected[key]

        return expected
    def test_data_excluded_course_run(self):
        """
        If a course run is excluded on a program, that program should not be
        returned for that course run on the course run endpoint.
        """
        serializer = CourseRunWithProgramsSerializer(
            self.course_run, context=self.serializer_context)
        ProgramFactory(courses=[self.course_run.course],
                       excluded_course_runs=[self.course_run])
        expected = CourseRunSerializer(self.course_run,
                                       context=self.serializer_context).data
        expected.update({
            'programs': [],
        })

        self.assertDictEqual(serializer.data, expected)
    def get_expected_data(self, course, request):
        expected = super().get_expected_data(course, request)
        expected.update({
            'short_description':
            course.short_description,
            'full_description':
            course.full_description,
            'level_type':
            course.level_type.name,
            'subjects': [],
            'prerequisites': [],
            'expected_learning_items': [],
            'video':
            VideoSerializer(course.video).data,
            'sponsors':
            OrganizationSerializer(course.sponsoring_organizations,
                                   many=True).data,
            'modified':
            json_date_format(course.modified),  # pylint: disable=no-member
            'marketing_url':
            '{url}?{params}'.format(url=course.marketing_url,
                                    params=urlencode({
                                        'utm_source':
                                        request.user.username,
                                        'utm_medium':
                                        request.user.referral_tracking_id,
                                    })),
            'course_runs':
            CourseRunSerializer(course.course_runs,
                                many=True,
                                context={
                                    'request': request
                                }).data,
            'owners':
            OrganizationSerializer(course.authoring_organizations,
                                   many=True).data,
        })

        return expected