Commit 2e0a6619 by Nimisha Asthagiri

fixup! course_api optimize unit tests.

parent 77928c21
...@@ -6,22 +6,29 @@ from django.http import Http404, QueryDict ...@@ -6,22 +6,29 @@ from django.http import Http404, QueryDict
from urllib import urlencode from urllib import urlencode
from rest_framework.exceptions import PermissionDenied from rest_framework.exceptions import PermissionDenied
from opaque_keys.edx.locator import CourseLocator
from openedx.core.djangoapps.util.test_forms import FormTestMixin
from student.models import CourseEnrollment from student.models import CourseEnrollment
from student.tests.factories import UserFactory, CourseEnrollmentFactory from student.tests.factories import UserFactory, CourseEnrollmentFactory
from openedx.core.djangoapps.util.test_forms import FormTestMixin from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.factories import CourseFactory
from ..forms import BlockListGetForm from ..forms import BlockListGetForm
@ddt.ddt @ddt.ddt
class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase): class TestBlockListGetForm(FormTestMixin, SharedModuleStoreTestCase):
""" """
Tests for BlockListGetForm Tests for BlockListGetForm
""" """
FORM_CLASS = BlockListGetForm FORM_CLASS = BlockListGetForm
@classmethod
def setUpClass(cls):
super(TestBlockListGetForm, cls).setUpClass()
cls.course = CourseFactory.create()
def setUp(self): def setUp(self):
super(TestBlockListGetForm, self).setUp() super(TestBlockListGetForm, self).setUp()
...@@ -29,11 +36,10 @@ class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase): ...@@ -29,11 +36,10 @@ class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase):
self.student2 = UserFactory.create() self.student2 = UserFactory.create()
self.staff = UserFactory.create(is_staff=True) self.staff = UserFactory.create(is_staff=True)
self.course = CourseFactory.create()
usage_key = self.course.location
CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id) CourseEnrollmentFactory.create(user=self.student, course_id=self.course.id)
CourseEnrollmentFactory.create(user=self.student2, course_id=self.course.id) CourseEnrollmentFactory.create(user=self.student2, course_id=self.course.id)
usage_key = self.course.location
self.initial = {'requesting_user': self.student} self.initial = {'requesting_user': self.student}
self.form_data = QueryDict( self.form_data = QueryDict(
urlencode({ urlencode({
...@@ -69,7 +75,7 @@ class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase): ...@@ -69,7 +75,7 @@ class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase):
#-- usage key #-- usage key
def test_missing_usage_key(self): def test_no_usage_key_param(self):
self.form_data.pop('usage_key') self.form_data.pop('usage_key')
self.assert_error('usage_key', "This field is required.") self.assert_error('usage_key', "This field is required.")
...@@ -77,9 +83,13 @@ class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase): ...@@ -77,9 +83,13 @@ class TestBlockListGetForm(FormTestMixin, ModuleStoreTestCase):
self.form_data['usage_key'] = 'invalid_usage_key' self.form_data['usage_key'] = 'invalid_usage_key'
self.assert_error('usage_key', "'invalid_usage_key' is not a valid usage key.") self.assert_error('usage_key', "'invalid_usage_key' is not a valid usage key.")
def test_non_existent_usage_key(self):
self.form_data['usage_key'] = self.store.make_course_usage_key(CourseLocator('non', 'existent', 'course'))
self.assert_raises_permission_denied()
#-- user #-- user
def test_missing_user(self): def test_no_user_param(self):
self.form_data.pop('user') self.form_data.pop('user')
self.assert_raises_permission_denied() self.assert_raises_permission_denied()
......
...@@ -4,7 +4,8 @@ Tests for Course Blocks serializers ...@@ -4,7 +4,8 @@ Tests for Course Blocks serializers
from mock import MagicMock from mock import MagicMock
from student.tests.factories import UserFactory from student.tests.factories import UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import ToyCourseFactory
from lms.djangoapps.course_blocks.api import get_course_blocks, LMS_COURSE_TRANSFORMERS from lms.djangoapps.course_blocks.api import get_course_blocks, LMS_COURSE_TRANSFORMERS
from ..transformers.blocks_api import BlocksAPITransformer from ..transformers.blocks_api import BlocksAPITransformer
...@@ -12,14 +13,19 @@ from ..serializers import BlockSerializer, BlockDictSerializer ...@@ -12,14 +13,19 @@ from ..serializers import BlockSerializer, BlockDictSerializer
from .test_utils import deserialize_usage_key from .test_utils import deserialize_usage_key
class TestBlockSerializerBase(ModuleStoreTestCase): class TestBlockSerializerBase(SharedModuleStoreTestCase):
""" """
Base class for testing BlockSerializer and BlockDictSerializer Base class for testing BlockSerializer and BlockDictSerializer
""" """
@classmethod
def setUpClass(cls):
super(TestBlockSerializerBase, cls).setUpClass()
cls.course_key = ToyCourseFactory.create().id
def setUp(self): def setUp(self):
super(TestBlockSerializerBase, self).setUp() super(TestBlockSerializerBase, self).setUp()
self.course_key = self.create_toy_course()
self.user = UserFactory.create() self.user = UserFactory.create()
blocks_api_transformer = BlocksAPITransformer( blocks_api_transformer = BlocksAPITransformer(
block_types_to_count=['video'], block_types_to_count=['video'],
......
...@@ -3,44 +3,50 @@ Tests for Course Blocks views ...@@ -3,44 +3,50 @@ Tests for Course Blocks views
""" """
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from opaque_keys.edx.locator import CourseLocator
from student.models import CourseEnrollment from student.models import CourseEnrollment
from student.tests.factories import CourseEnrollmentFactory, UserFactory from student.tests.factories import CourseEnrollmentFactory, UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import ToyCourseFactory
from .test_utils import deserialize_usage_key from .test_utils import deserialize_usage_key
class TestCourseBlocksView(ModuleStoreTestCase): class TestCourseBlocksView(SharedModuleStoreTestCase):
""" """
Test class for CourseBlocks view Test class for CourseBlocks view
""" """
def setUp(self): @classmethod
super(TestCourseBlocksView, self).setUp() def setUpClass(cls):
super(TestCourseBlocksView, cls).setUpClass()
self.user = UserFactory.create() cls.course_key = ToyCourseFactory.create().id
self.client.login(username=self.user.username, password='test') cls.course_usage_key = cls.store.make_course_usage_key(cls.course_key)
self.course_key = self.create_toy_course() cls.non_orphaned_block_usage_keys = set(
self.course_usage_key = self.store.make_course_usage_key(self.course_key)
CourseEnrollmentFactory.create(user=self.user, course_id=self.course_key)
self.non_orphaned_block_usage_keys = set(
unicode(item.location) unicode(item.location)
for item in self.store.get_items(self.course_key) for item in cls.store.get_items(cls.course_key)
# remove all orphaned items in the course, except for the root 'course' block # remove all orphaned items in the course, except for the root 'course' block
if self.store.get_parent_location(item.location) or item.category == 'course' if cls.store.get_parent_location(item.location) or item.category == 'course'
) )
cls.url = reverse(
self.url = reverse(
'course_blocks', 'course_blocks',
kwargs={'usage_key_string': unicode(self.course_usage_key)} kwargs={'usage_key_string': unicode(cls.course_usage_key)}
) )
def verify_response(self, expected_status_code=200, params=None): def setUp(self):
super(TestCourseBlocksView, self).setUp()
self.user = UserFactory.create()
self.client.login(username=self.user.username, password='test')
CourseEnrollmentFactory.create(user=self.user, course_id=self.course_key)
def verify_response(self, expected_status_code=200, params=None, url=None):
query_params = {'user': self.user.username} query_params = {'user': self.user.username}
if params: if params:
query_params.update(params) query_params.update(params)
response = self.client.get(self.url, query_params) response = self.client.get(url or self.url, query_params)
self.assertEquals(response.status_code, expected_status_code) self.assertEquals(response.status_code, expected_status_code)
return response return response
...@@ -77,8 +83,12 @@ class TestCourseBlocksView(ModuleStoreTestCase): ...@@ -77,8 +83,12 @@ class TestCourseBlocksView(ModuleStoreTestCase):
self.verify_response(403) self.verify_response(403)
def test_non_existent_course(self): def test_non_existent_course(self):
self.store.delete_course(self.course_key, self.user) usage_key = self.store.make_course_usage_key(CourseLocator('non', 'existent', 'course'))
self.verify_response(404) url = reverse(
'course_blocks',
kwargs={'usage_key_string': unicode(usage_key)}
)
self.verify_response(403, url=url)
def test_basic(self): def test_basic(self):
response = self.verify_response() response = self.verify_response()
......
from openedx.core.lib.block_cache.block_structure import BlockStructureFactory from openedx.core.lib.block_cache.block_structure import BlockStructureFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import ToyCourseFactory
from ..student_view import StudentViewTransformer from ..student_view import StudentViewTransformer
...@@ -8,7 +9,7 @@ class TestStudentViewTransformer(ModuleStoreTestCase): ...@@ -8,7 +9,7 @@ class TestStudentViewTransformer(ModuleStoreTestCase):
def setUp(self): def setUp(self):
super(TestStudentViewTransformer, self).setUp() super(TestStudentViewTransformer, self).setUp()
self.course_key = self.create_toy_course() self.course_key = ToyCourseFactory.create().id
self.course_root_loc = self.store.make_course_usage_key(self.course_key) self.course_root_loc = self.store.make_course_usage_key(self.course_key)
self.block_structure = BlockStructureFactory.create_from_modulestore( self.block_structure = BlockStructureFactory.create_from_modulestore(
self.course_root_loc, self.store self.course_root_loc, self.store
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment