Files
edx-platform/common/djangoapps/course_about/tests/test_data.py
Calen Pennington b353ed2ea2 Better support specifying of modulestore configuration in test cases
The existing pattern of using `override_settings(MODULESTORE=...)` prevented
us from having more than one layer of subclassing in modulestore tests.

In a structure like:

    @override_settings(MODULESTORE=store_a)
    class BaseTestCase(ModuleStoreTestCase):
        def setUp(self):
            # use store

    @override_settings(MODULESTORE=store_b)
    class ChildTestCase(BaseTestCase):
        def setUp(self):
            # use store

In this case, the store actions performed in `BaseTestCase` on behalf of
`ChildTestCase` would still use `store_a`, even though the `ChildTestCase`
had specified to use `store_b`. This is because the `override_settings`
decorator would be the innermost wrapper around the `BaseTestCase.setUp` method,
no matter what `ChildTestCase` does.

To remedy this, we move the call to `override_settings` into the
`ModuleStoreTestCase.setUp` method, and use a cleanup to remove the override.
Subclasses can just defined the `MODULESTORE` class attribute to specify which
modulestore to use _for the entire `setUp` chain_.

[PLAT-419]
2015-02-04 09:09:14 -05:00

70 lines
2.5 KiB
Python

"""
Tests specific to the Data Aggregation Layer of the Course About API.
"""
import unittest
from django.test.utils import override_settings
from datetime import datetime
from django.conf import settings
from nose.tools import raises
from xmodule.modulestore.tests.django_utils import (
ModuleStoreTestCase, mixed_store_config
)
from xmodule.modulestore.tests.factories import CourseFactory
from student.tests.factories import UserFactory
from course_about import data
from course_about.errors import CourseNotFoundError
from xmodule.modulestore.django import modulestore
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
class CourseAboutDataTest(ModuleStoreTestCase):
"""
Test course enrollment data aggregation.
"""
USERNAME = "Bob"
EMAIL = "bob@example.com"
PASSWORD = "edx"
def setUp(self):
"""Create a course and user, then log in. """
super(CourseAboutDataTest, self).setUp()
self.course = CourseFactory.create()
self.user = UserFactory.create(username=self.USERNAME, email=self.EMAIL, password=self.PASSWORD)
self.client.login(username=self.USERNAME, password=self.PASSWORD)
def test_get_course_about_details(self):
course_info = data.get_course_about_details(unicode(self.course.id))
self.assertIsNotNone(course_info)
def test_get_course_about_valid_date(self):
module_store = modulestore()
self.course.start = datetime.now()
self.course.end = datetime.now()
self.course.announcement = datetime.now()
module_store.update_item(self.course, self.user.id)
course_info = data.get_course_about_details(unicode(self.course.id))
self.assertIsNotNone(course_info["start"])
self.assertIsNotNone(course_info["end"])
self.assertIsNotNone(course_info["announcement"])
def test_get_course_about_none_date(self):
module_store = modulestore()
self.course.start = None
self.course.end = None
self.course.announcement = None
module_store.update_item(self.course, self.user.id)
course_info = data.get_course_about_details(unicode(self.course.id))
self.assertIsNone(course_info["start"])
self.assertIsNone(course_info["end"])
self.assertIsNone(course_info["announcement"])
@raises(CourseNotFoundError)
def test_non_existent_course(self):
data.get_course_about_details("this/is/bananas")
@raises(CourseNotFoundError)
def test_invalid_key(self):
data.get_course_about_details("invalid:key:k")