test_serializers.py 5.72 KB
Newer Older
1
"""
2
Test data created by CourseSerializer and CourseDetailSerializer
3 4
"""

5
from __future__ import unicode_literals
6

7 8
from datetime import datetime

9
import ddt
10
from nose.plugins.attrib import attr
11
from rest_framework.request import Request
12
from rest_framework.test import APIRequestFactory
13
from xblock.core import XBlock
14 15 16

from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
from openedx.core.djangoapps.models.course_details import CourseDetails
17
from xmodule.course_module import DEFAULT_START_DATE
18 19
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import check_mongo_calls
20

21 22
from ..serializers import CourseDetailSerializer, CourseSerializer
from .mixins import CourseApiFactoryMixin
23 24


25
@attr(shard=3)
26
@ddt.ddt
27
class TestCourseSerializer(CourseApiFactoryMixin, ModuleStoreTestCase):
28
    """
29
    Test CourseSerializer
30
    """
31
    expected_mongo_calls = 0
32
    maxDiff = 5000  # long enough to show mismatched dicts, in case of error
33
    serializer_class = CourseSerializer
34

35 36
    ENABLED_SIGNALS = ['course_published']

37
    def setUp(self):
38
        super(TestCourseSerializer, self).setUp()
39 40 41 42
        self.staff_user = self.create_user('staff', is_staff=True)
        self.honor_user = self.create_user('honor', is_staff=False)
        self.request_factory = APIRequestFactory()

43 44
        image_path = u'/c4x/edX/toy/asset/just_a_test.jpg'
        image_url = u'http://testserver' + image_path
45
        self.expected_data = {
46
            'id': u'edX/toy/2012_Fall',
47 48 49 50 51 52
            'name': u'Toy Course',
            'number': u'toy',
            'org': u'edX',
            'short_description': u'A course about toys.',
            'media': {
                'course_image': {
53
                    'uri': image_path,
54 55 56
                },
                'course_video': {
                    'uri': u'http://www.youtube.com/watch?v=test_youtube_id',
57 58 59 60 61
                },
                'image': {
                    'raw': image_url,
                    'small': image_url,
                    'large': image_url,
62 63 64 65 66
                }
            },
            'start': u'2015-07-17T12:00:00Z',
            'start_type': u'timestamp',
            'start_display': u'July 17, 2015',
67 68 69
            'end': u'2015-09-19T18:00:00Z',
            'enrollment_start': u'2015-06-15T00:00:00Z',
            'enrollment_end': u'2015-07-15T00:00:00Z',
70 71
            'blocks_url': u'http://testserver/api/courses/v1/blocks/?course_id=edX%2Ftoy%2F2012_Fall',
            'effort': u'6 hours',
72
            'pacing': 'instructor',
73
            'mobile_available': False,
74
            'hidden': False,
75
            'invitation_only': False,
76 77 78

            # 'course_id' is a deprecated field, please use 'id' instead.
            'course_id': u'edX/toy/2012_Fall',
79
        }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    def _get_request(self, user=None):
        """
        Build a Request object for the specified user.
        """
        if user is None:
            user = self.honor_user
        request = Request(self.request_factory.get('/'))
        request.user = user
        return request

    def _get_result(self, course):
        """
        Return the CourseSerializer for the specified course.
        """
        course_overview = CourseOverview.get_from_id(course.id)
        return self.serializer_class(course_overview, context={'request': self._get_request()}).data

    def test_basic(self):
99 100
        course = self.create_course()
        CourseDetails.update_about_video(course, 'test_youtube_id', self.staff_user.id)  # pylint: disable=no-member
101 102 103
        with check_mongo_calls(self.expected_mongo_calls):
            result = self._get_result(course)
        self.assertDictEqual(result, self.expected_data)
104

105 106 107 108 109 110 111 112 113
    def test_hidden(self):
        course = self.create_course(
            course=u'custom',
            start=datetime(2015, 3, 15),
            catalog_visibility=u'none'
        )
        result = self._get_result(course)
        self.assertEqual(result['hidden'], True)

114 115 116 117 118 119
    def test_advertised_start(self):
        course = self.create_course(
            course=u'custom',
            start=datetime(2015, 3, 15),
            advertised_start=u'The Ides of March'
        )
120
        result = self._get_result(course)
121 122 123 124 125 126
        self.assertEqual(result['course_id'], u'edX/custom/2012_Fall')
        self.assertEqual(result['start_type'], u'string')
        self.assertEqual(result['start_display'], u'The Ides of March')

    def test_empty_start(self):
        course = self.create_course(start=DEFAULT_START_DATE, course=u'custom')
127
        result = self._get_result(course)
128 129 130
        self.assertEqual(result['course_id'], u'edX/custom/2012_Fall')
        self.assertEqual(result['start_type'], u'empty')
        self.assertIsNone(result['start_display'])
131

132 133 134 135 136 137 138 139 140 141
    @ddt.unpack
    @ddt.data(
        (True, 'self'),
        (False, 'instructor'),
    )
    def test_pacing(self, self_paced, expected_pacing):
        course = self.create_course(self_paced=self_paced)
        result = self._get_result(course)
        self.assertEqual(result['pacing'], expected_pacing)

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

class TestCourseDetailSerializer(TestCourseSerializer):  # pylint: disable=test-inherits-tests
    """
    Test CourseDetailSerializer by rerunning all the tests
    in TestCourseSerializer, but with the
    CourseDetailSerializer serializer class.

    """
    # 1 mongo call is made to get the course About overview text.
    expected_mongo_calls = 1
    serializer_class = CourseDetailSerializer

    def setUp(self):
        super(TestCourseDetailSerializer, self).setUp()

        # update the expected_data to include the 'overview' data.
        about_descriptor = XBlock.load_class('about')
        overview_template = about_descriptor.get_template('overview.yaml')
        self.expected_data['overview'] = overview_template.get('data')