utils.py 4.09 KB
Newer Older
1 2 3 4 5 6 7 8
"""
Test utils for CCX
"""
import datetime
import pytz

from django.conf import settings

9 10 11 12 13
from student.roles import (
    CourseCcxCoachRole,
    CourseInstructorRole,
    CourseStaffRole
)
14
from student.tests.factories import (
15
    UserFactory
16 17 18 19 20 21 22 23 24 25 26
)
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.django_utils import (
    SharedModuleStoreTestCase,
    TEST_DATA_SPLIT_MODULESTORE
)
from xmodule.modulestore.tests.factories import (
    CourseFactory,
    ItemFactory,
)

27 28 29
from lms.djangoapps.ccx.overrides import override_field_for_ccx
from lms.djangoapps.ccx.tests.factories import CcxFactory

30 31 32 33 34 35 36 37 38 39 40 41

class CcxTestCase(SharedModuleStoreTestCase):
    """
    General test class to be used in other CCX tests classes.

    It creates a course that can be used as master course for CCXs.
    """
    MODULESTORE = TEST_DATA_SPLIT_MODULESTORE

    @classmethod
    def setUpClass(cls):
        super(CcxTestCase, cls).setUpClass()
42
        cls.course = course = CourseFactory.create(enable_ccx=True)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

        # Create a course outline
        cls.mooc_start = start = datetime.datetime(
            2010, 5, 12, 2, 42, tzinfo=pytz.UTC
        )
        cls.mooc_due = due = datetime.datetime(
            2010, 7, 7, 0, 0, tzinfo=pytz.UTC
        )

        cls.chapters = [
            ItemFactory.create(start=start, parent=course) for _ in xrange(2)
        ]
        cls.sequentials = flatten([
            [
                ItemFactory.create(parent=chapter) for _ in xrange(2)
            ] for chapter in cls.chapters
        ])
        cls.verticals = flatten([
            [
                ItemFactory.create(
                    start=start, due=due, parent=sequential, graded=True, format='Homework', category=u'vertical'
                ) for _ in xrange(2)
            ] for sequential in cls.sequentials
        ])

        # Trying to wrap the whole thing in a bulk operation fails because it
        # doesn't find the parents. But we can at least wrap this part...
        with cls.store.bulk_operations(course.id, emit_signals=False):
            blocks = flatten([  # pylint: disable=unused-variable
                [
                    ItemFactory.create(parent=vertical) for _ in xrange(2)
                ] for vertical in cls.verticals
            ])

    def setUp(self):
        """
        Set up tests
        """
        super(CcxTestCase, self).setUp()
        # Create instructor account
83
        self.coach = UserFactory.create(password="test")
84 85 86
        # create an instance of modulestore
        self.mstore = modulestore()

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    def make_staff(self):
        """
        create staff user.
        """
        staff = UserFactory.create(password="test")
        role = CourseStaffRole(self.course.id)
        role.add_users(staff)

        return staff

    def make_instructor(self):
        """
        create instructor user.
        """
        instructor = UserFactory.create(password="test")
        role = CourseInstructorRole(self.course.id)
        role.add_users(instructor)

        return instructor

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    def make_coach(self):
        """
        create coach user
        """
        role = CourseCcxCoachRole(self.course.id)
        role.add_users(self.coach)

    def make_ccx(self, max_students_allowed=settings.CCX_MAX_STUDENTS_ALLOWED):
        """
        create ccx
        """
        ccx = CcxFactory(course_id=self.course.id, coach=self.coach)
        override_field_for_ccx(ccx, self.course, 'max_student_enrollments_allowed', max_students_allowed)
        return ccx

    def get_outbox(self):
        """
        get fake outbox
        """
        from django.core import mail
        return mail.outbox


def flatten(seq):
    """
    For [[1, 2], [3, 4]] returns [1, 2, 3, 4].  Does not recurse.
    """
    return [x for sub in seq for x in sub]


def iter_blocks(course):
    """
    Returns an iterator over all of the blocks in a course.
    """
    def visit(block):
        """ get child blocks """
        yield block
        for child in block.get_children():
            for descendant in visit(child):  # wish they'd backport yield from
                yield descendant
    return visit(course)